Skip to content

Commit 025c267

Browse files
ahemAnders Hellerup Madsen
and
Anders Hellerup Madsen
authored
fix: support dash in property names (Pydantic generator) (#1955)
Co-authored-by: Anders Hellerup Madsen <ahem@github.com>
1 parent 78e3409 commit 025c267

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

examples/generate-python-pydantic-models/__snapshots__/index.spec.ts.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
exports[`Should be able to render python models and should log expected output to console: class-model 1`] = `
44
Array [
55
"class Root(BaseModel):
6-
optionalField: Optional[str] = Field(alias='''this field is optional''', default=None)
7-
requiredField: str = Field(alias='''this field is required''')
6+
optionalField: Optional[str] = Field(description='''this field is optional''', default=None)
7+
requiredField: str = Field(description='''this field is required''')
88
noDescription: Optional[str] = Field(default=None)
99
options: Optional[Options] = Field(default=None)
10+
contentType: Optional[str] = Field(default=None, alias='''content-type''')
1011
",
1112
]
1213
`;

examples/generate-python-pydantic-models/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ const jsonSchemaDraft7 = {
2626
$id: 'options',
2727
type: ['integer', 'boolean', 'string'],
2828
enum: [123, 213, true, 'Run']
29-
}
29+
},
30+
'content-type': { type: 'string' }
3031
}
3132
};
3233

src/generators/python/constrainer/PropertyKeyConstrainer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ export type PropertyKeyConstraintOptions = {
2525

2626
export const DefaultPropertyKeyConstraints: PropertyKeyConstraintOptions = {
2727
NO_SPECIAL_CHAR: (value: string) => {
28-
//Exclude ` ` because it gets formatted by NAMING_FORMATTER
28+
//Exclude ` ` and `-` because they gets formatted by NAMING_FORMATTER
2929
//Exclude '_' because they are allowed
3030
return FormatHelpers.replaceSpecialCharacters(value, {
31-
exclude: [' ', '_'],
31+
exclude: [' ', '-', '_'],
3232
separator: '_'
3333
});
3434
},

src/generators/python/presets/Pydantic.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,25 @@ const PYTHON_PYDANTIC_CLASS_PRESET: ClassPresetType<PythonOptions> = {
3333
type = `Optional[${type}]`;
3434
}
3535

36-
const alias = params.property.property.originalInput['description']
37-
? `alias='''${params.property.property.originalInput['description']}'''`
38-
: '';
39-
const defaultValue = params.property.required ? '' : 'default=None';
40-
41-
if (alias && defaultValue) {
42-
return `${propertyName}: ${type} = Field(${alias}, ${defaultValue})`;
43-
} else if (alias) {
44-
return `${propertyName}: ${type} = Field(${alias})`;
36+
const decoratorArgs: string[] = [];
37+
38+
if (params.property.property.originalInput['description']) {
39+
decoratorArgs.push(
40+
`description='''${params.property.property.originalInput['description']}'''`
41+
);
42+
}
43+
if (!params.property.required) {
44+
decoratorArgs.push('default=None');
4545
}
46-
return `${propertyName}: ${type} = Field(${defaultValue})`;
46+
if (
47+
params.property.propertyName !== params.property.unconstrainedPropertyName
48+
) {
49+
decoratorArgs.push(
50+
`alias='''${params.property.unconstrainedPropertyName}'''`
51+
);
52+
}
53+
54+
return `${propertyName}: ${type} = Field(${decoratorArgs.join(', ')})`;
4755
},
4856
ctor: () => '',
4957
getter: () => '',

test/generators/python/presets/__snapshots__/Pydantic.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`PYTHON_PYDANTIC_PRESET should render pydantic for class 1`] = `
44
"class Test(BaseModel):
5-
prop: Optional[str] = Field(alias='''test
5+
prop: Optional[str] = Field(description='''test
66
multi
77
line
88
description''', default=None)

0 commit comments

Comments
 (0)