Skip to content

Commit

Permalink
fix: support dash in property names (Pydantic generator) (#1955)
Browse files Browse the repository at this point in the history
Co-authored-by: Anders Hellerup Madsen <[email protected]>
  • Loading branch information
ahem and Anders Hellerup Madsen authored Apr 23, 2024
1 parent 78e3409 commit 025c267
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
exports[`Should be able to render python models and should log expected output to console: class-model 1`] = `
Array [
"class Root(BaseModel):
optionalField: Optional[str] = Field(alias='''this field is optional''', default=None)
requiredField: str = Field(alias='''this field is required''')
optionalField: Optional[str] = Field(description='''this field is optional''', default=None)
requiredField: str = Field(description='''this field is required''')
noDescription: Optional[str] = Field(default=None)
options: Optional[Options] = Field(default=None)
contentType: Optional[str] = Field(default=None, alias='''content-type''')
",
]
`;
Expand Down
3 changes: 2 additions & 1 deletion examples/generate-python-pydantic-models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const jsonSchemaDraft7 = {
$id: 'options',
type: ['integer', 'boolean', 'string'],
enum: [123, 213, true, 'Run']
}
},
'content-type': { type: 'string' }
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/generators/python/constrainer/PropertyKeyConstrainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export type PropertyKeyConstraintOptions = {

export const DefaultPropertyKeyConstraints: PropertyKeyConstraintOptions = {
NO_SPECIAL_CHAR: (value: string) => {
//Exclude ` ` because it gets formatted by NAMING_FORMATTER
//Exclude ` ` and `-` because they gets formatted by NAMING_FORMATTER
//Exclude '_' because they are allowed
return FormatHelpers.replaceSpecialCharacters(value, {
exclude: [' ', '_'],
exclude: [' ', '-', '_'],
separator: '_'
});
},
Expand Down
28 changes: 18 additions & 10 deletions src/generators/python/presets/Pydantic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,25 @@ const PYTHON_PYDANTIC_CLASS_PRESET: ClassPresetType<PythonOptions> = {
type = `Optional[${type}]`;
}

const alias = params.property.property.originalInput['description']
? `alias='''${params.property.property.originalInput['description']}'''`
: '';
const defaultValue = params.property.required ? '' : 'default=None';

if (alias && defaultValue) {
return `${propertyName}: ${type} = Field(${alias}, ${defaultValue})`;
} else if (alias) {
return `${propertyName}: ${type} = Field(${alias})`;
const decoratorArgs: string[] = [];

if (params.property.property.originalInput['description']) {
decoratorArgs.push(
`description='''${params.property.property.originalInput['description']}'''`
);
}
if (!params.property.required) {
decoratorArgs.push('default=None');
}
return `${propertyName}: ${type} = Field(${defaultValue})`;
if (
params.property.propertyName !== params.property.unconstrainedPropertyName
) {
decoratorArgs.push(
`alias='''${params.property.unconstrainedPropertyName}'''`
);
}

return `${propertyName}: ${type} = Field(${decoratorArgs.join(', ')})`;
},
ctor: () => '',
getter: () => '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`PYTHON_PYDANTIC_PRESET should render pydantic for class 1`] = `
"class Test(BaseModel):
prop: Optional[str] = Field(alias='''test
prop: Optional[str] = Field(description='''test
multi
line
description''', default=None)
Expand Down

0 comments on commit 025c267

Please sign in to comment.