diff --git a/examples/generate-python-pydantic-models/__snapshots__/index.spec.ts.snap b/examples/generate-python-pydantic-models/__snapshots__/index.spec.ts.snap index 19d319f61c..2a856c63b3 100644 --- a/examples/generate-python-pydantic-models/__snapshots__/index.spec.ts.snap +++ b/examples/generate-python-pydantic-models/__snapshots__/index.spec.ts.snap @@ -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''') ", ] `; diff --git a/examples/generate-python-pydantic-models/index.ts b/examples/generate-python-pydantic-models/index.ts index f4cf95b252..270e3aaacc 100644 --- a/examples/generate-python-pydantic-models/index.ts +++ b/examples/generate-python-pydantic-models/index.ts @@ -26,7 +26,8 @@ const jsonSchemaDraft7 = { $id: 'options', type: ['integer', 'boolean', 'string'], enum: [123, 213, true, 'Run'] - } + }, + 'content-type': { type: 'string' } } }; diff --git a/src/generators/python/constrainer/PropertyKeyConstrainer.ts b/src/generators/python/constrainer/PropertyKeyConstrainer.ts index c86f4d8223..8ce976c2d8 100644 --- a/src/generators/python/constrainer/PropertyKeyConstrainer.ts +++ b/src/generators/python/constrainer/PropertyKeyConstrainer.ts @@ -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: '_' }); }, diff --git a/src/generators/python/presets/Pydantic.ts b/src/generators/python/presets/Pydantic.ts index 39e2b7f052..90621732ab 100644 --- a/src/generators/python/presets/Pydantic.ts +++ b/src/generators/python/presets/Pydantic.ts @@ -33,17 +33,25 @@ const PYTHON_PYDANTIC_CLASS_PRESET: ClassPresetType = { 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: () => '', diff --git a/test/generators/python/presets/__snapshots__/Pydantic.spec.ts.snap b/test/generators/python/presets/__snapshots__/Pydantic.spec.ts.snap index 5c77bd41f9..4cba95efa5 100644 --- a/test/generators/python/presets/__snapshots__/Pydantic.spec.ts.snap +++ b/test/generators/python/presets/__snapshots__/Pydantic.spec.ts.snap @@ -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)