Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed May 21, 2024
1 parent a994e02 commit f364c88
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/generators/python/PythonDependencyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class PythonDependencyManager extends AbstractDependencyManager {
const importMap: Record<string, string[]> = {};
const dependenciesToRender = [];
for (const dependency of individualDependencies) {
const regex = /from ([A-Za-z0-9]+) import ([A-Za-z0-9,\s]+)/g;
const regex = /from ([A-Za-z0-9]+) import ([A-Za-z0-9_\-,\s]+)/g;
const matches = regex.exec(dependency);

if (!matches) {
Expand Down
58 changes: 57 additions & 1 deletion src/generators/python/presets/Pydantic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ConstrainedDictionaryModel,
ConstrainedObjectPropertyModel,
ConstrainedUnionModel
} from '../../../models';
import { PythonOptions } from '../PythonGenerator';
Expand Down Expand Up @@ -64,7 +65,62 @@ const PYTHON_PYDANTIC_CLASS_PRESET: ClassPresetType<PythonOptions> = {
},
ctor: () => '',
getter: () => '',
setter: () => ''
setter: () => '',
additionalContent: ({ content, model, renderer }) => {
const allProperties = Object.keys(model.properties);
let dictionaryModel: ConstrainedObjectPropertyModel | undefined;
for (const property of Object.values(model.properties)) {
if (
property.property instanceof ConstrainedDictionaryModel &&
property.property.serializationType === 'unwrap'
) {
dictionaryModel = property;
}
}
const shouldHaveFunctions = dictionaryModel !== undefined;
if (!shouldHaveFunctions) {
return content;
}

renderer.dependencyManager.addDependency(
'from pydantic import model_serializer, model_validator'
);
// eslint-disable-next-line prettier/prettier
return `@model_serializer(mode='wrap')
def custom_serializer(self, handler):
serialized_self = handler(self)
${dictionaryModel?.propertyName} = getattr(self, "${dictionaryModel?.propertyName}")
if ${dictionaryModel?.propertyName} is not None:
for key, value in ${dictionaryModel?.propertyName}.items():
# Never overwrite existing values, to avoid clashes
if not hasattr(serialized_self, key):
serialized_self[key] = value
return serialized_self
@model_validator(mode='before')
@classmethod
def unwrap_${dictionaryModel?.propertyName}(cls, data):
json_properties = list(data.keys())
known_object_properties = [${allProperties
.map((value) => `'${value}'`)
.join(', ')}]
unknown_object_properties = [element for element in json_properties if element not in known_object_properties]
# Ignore attempts that validate regular models, only when unknown input is used we add unwrap extensions
if len(unknown_object_properties) == 0:
return data
known_json_properties = [${Object.values(model.properties)
.map((value) => `'${value.unconstrainedPropertyName}'`)
.join(', ')}]
${dictionaryModel?.propertyName} = {}
for obj_key in list(data.keys()):
if not known_json_properties.__contains__(obj_key):
${dictionaryModel?.propertyName}[obj_key] = data.pop(obj_key, None)
data['${dictionaryModel?.unconstrainedPropertyName}'] = ${dictionaryModel?.propertyName}
return data
${content}`;
}
};

export const PYTHON_PYDANTIC_PRESET: PythonPreset<PythonOptions> = {
Expand Down
4 changes: 2 additions & 2 deletions test/generators/python/PythonDependencyManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ describe('PythonDependencyManager', () => {
test('should render unique dependency', () => {
const dependencyManager = new PythonDependencyManager(
PythonGenerator.defaultOptions,
['from x import y', 'from x import y2']
['from x import y', 'from x import y2', 'from x import y_2']
);
expect(dependencyManager.renderDependencies()).toEqual([
'from x import y, y2'
'from x import y, y2, y_2'
]);
});
test('should render __future__ dependency first', () => {
Expand Down

0 comments on commit f364c88

Please sign in to comment.