From 1643da1d83425caaef70b77ef6520738a61f784a Mon Sep 17 00:00:00 2001 From: Harshil Jani Date: Sat, 16 Dec 2023 04:03:47 +0530 Subject: [PATCH] fix: set default to None for optional fields - fix in Pydantic Python Generator for optional values. - default=None should be added in `Field()` if the property is optional. - updated the test snapshot to compare the new default value argument. Signed-off-by: Harshil Jani --- .../__snapshots__/index.spec.ts.snap | 6 +++--- src/generators/python/presets/Pydantic.ts | 18 +++++++++++------- .../__snapshots__/Pydantic.spec.ts.snap | 4 ++-- 3 files changed, 16 insertions(+), 12 deletions(-) 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 39488f65e4..19d319f61c 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,10 @@ 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''') + optionalField: Optional[str] = Field(alias='''this field is optional''', default=None) requiredField: str = Field(alias='''this field is required''') - noDescription: Optional[str] = Field() - options: Optional[Options] = Field() + noDescription: Optional[str] = Field(default=None) + options: Optional[Options] = Field(default=None) ", ] `; diff --git a/src/generators/python/presets/Pydantic.ts b/src/generators/python/presets/Pydantic.ts index 7d787bb18e..b74ef6510a 100644 --- a/src/generators/python/presets/Pydantic.ts +++ b/src/generators/python/presets/Pydantic.ts @@ -18,14 +18,18 @@ const PYTHON_PYDANTIC_CLASS_PRESET: ClassPresetType = { ); }, property(params) { - const type = params.property.required - ? params.property.property.type - : `Optional[${params.property.property.type}]`; - const alias = params.property.property.originalInput['description'] - ? `alias='''${params.property.property.originalInput['description']}'''` - : ''; + const { propertyName, required, property } = params.property; + const type = required ? property.type : `Optional[${property.type}]`; + const description = property.originalInput['description']; + const alias = description ? `alias='''${description}'''` : ''; + const defaultValue = required ? '' : 'default=None'; - return `${params.property.propertyName}: ${type} = Field(${alias})`; + if (alias && defaultValue) { + return `${propertyName}: ${type} = Field(${alias}, ${defaultValue})`; + } else if (alias) { + return `${propertyName}: ${type} = Field(${alias})`; + } + return `${propertyName}: ${type} = Field(${defaultValue})`; }, 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 7745d87060..a569da8c3b 100644 --- a/test/generators/python/presets/__snapshots__/Pydantic.spec.ts.snap +++ b/test/generators/python/presets/__snapshots__/Pydantic.spec.ts.snap @@ -5,7 +5,7 @@ exports[`PYTHON_PYDANTIC_PRESET should render pydantic for class 1`] = ` prop: Optional[str] = Field(alias='''test multi line - description''') - additionalProperties: Optional[dict[Any, Any]] = Field() + description''', default=None) + additionalProperties: Optional[dict[Any, Any]] = Field(default=None) " `;