diff --git a/.all-contributorsrc b/.all-contributorsrc index 6712911bdc..5522246c73 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -782,7 +782,19 @@ "profile": "https://jaisarita.vercel.app/", "contributions": [ "code", - "design" + "design", + "doc", + "maintenance" + ] + }, + { + "login": "Harshil-Jani", + "name": "Harshil Jani", + "avatar_url": "https://avatars.githubusercontent.com/u/79367883?v=4", + "profile": "http://harshil.prose.sh", + "contributions": [ + "code", + "test" ] } ], diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000000..cab14c8a62 --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,14 @@ +## Description + + +## Related Issue + + +## Checklist +- [ ] The code follows the project's coding standards and is properly linted (`npm run lint`). +- [ ] Tests have been added or updated to cover the changes. +- [ ] Documentation has been updated to reflect the changes. +- [ ] All tests pass successfully locally.(`npm run test`). + +## Additional Notes + diff --git a/CODEOWNERS b/CODEOWNERS index ee0ed91e3c..ea345570f9 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -9,6 +9,10 @@ # Core Champions that does a little of everything * @magicmatatjahu @jonaslagoni @kennethaasan @asyncapi-bot-eve + +# Website Champions +/modelina-website @devilkiller-ag + # Documentation champions /docs diff --git a/README.md b/README.md index f1a2f32ec2..947ab99ae3 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ [![Discussions](https://img.shields.io/github/discussions/asyncapi/modelina)](https://github.com/asyncapi/modelina/discussions) [![Website](https://img.shields.io/website?label=website&url=https%3A%2F%2Fwww.modelina.org)](https://www.modelina.org) [![Playground](https://img.shields.io/website?label=playground&url=https%3A%2F%2Fwww.modelina.org%2Fplayground)](https://www.modelina.org/playground) -[![All Contributors](https://img.shields.io/badge/all_contributors-72-orange.svg?style=flat-square)](#contributors-) +[![All Contributors](https://img.shields.io/badge/all_contributors-73-orange.svg?style=flat-square)](#contributors-) Your one-stop tool for generating accurate and well-tested models for representing the message payloads. Use it as a tool in your development workflow, or a library in a larger integrations, entirely in your control. @@ -417,7 +417,8 @@ Thanks go out to these wonderful people ([emoji key](https://allcontributors.org Nilkanth Parmar
Nilkanth Parmar

💻 ⚠️ 💡 📖 - Ashmit JaiSarita Gupta
Ashmit JaiSarita Gupta

💻 🎨 + Ashmit JaiSarita Gupta
Ashmit JaiSarita Gupta

💻 🎨 📖 🚧 + Harshil Jani
Harshil Jani

💻 ⚠️ diff --git a/docs/constraints/CSharp.md b/docs/constraints/CSharp.md index 32ca6904d7..17e1d5be9e 100644 --- a/docs/constraints/CSharp.md +++ b/docs/constraints/CSharp.md @@ -3,7 +3,7 @@ These are the C# specific constraints applied to the MetaModel before reaching the presets. [Read here to get a more general idea on the overall process](../input-processing.md) of converting a MetaModel to a ConstrainedMetaModel. ## Model Naming -These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| @@ -14,7 +14,7 @@ These are the constraints that is applied to model naming. The `Rule key` is wha |NAMING_FORMATTER|Must be formatted equally|Model name is formatted using pascal case| ## Property naming -These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| |NO_SPECIAL_CHAR|No special characters| Special characters are replaced by their name, for example `!` is replaced with `exclamation`. For csharp `_` and `$` are an exception to this rule. | @@ -26,7 +26,7 @@ These are the constraints that is applied to object properties and the naming of ## Enum key constraints -These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| diff --git a/docs/constraints/Dart.md b/docs/constraints/Dart.md index dbd317ba41..d545a6f9bd 100644 --- a/docs/constraints/Dart.md +++ b/docs/constraints/Dart.md @@ -3,7 +3,7 @@ These are the Dart specific constraints applied to the MetaModel before reaching the presets. [Read here to get a more general idea on the overall process](../input-processing.md) of converting a MetaModel to a ConstrainedMetaModel. ## Model Naming -These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| @@ -14,7 +14,7 @@ These are the constraints that is applied to model naming. The `Rule key` is wha |NAMING_FORMATTER|Must be formatted equally|Model name is formatted using pascal case| ## Property naming -These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| |NO_SPECIAL_CHAR|No special characters| Special characters are replaced by their name, for example `!` is replaced with `exclamation`. For dart `_` and `$` are an exception to this rule. | @@ -26,7 +26,7 @@ These are the constraints that is applied to object properties and the naming of ## Enum key constraints -These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| diff --git a/docs/constraints/Go.md b/docs/constraints/Go.md index 9c819fe526..ef693d8383 100644 --- a/docs/constraints/Go.md +++ b/docs/constraints/Go.md @@ -3,7 +3,7 @@ These are the Go specific constraints applied to the MetaModel before reaching the presets. [Read here to get a more general idea on the overall process](../input-processing.md) of converting a MetaModel to a ConstrainedMetaModel. ## Model Naming -These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| @@ -14,7 +14,7 @@ These are the constraints that is applied to model naming. The `Rule key` is wha |NAMING_FORMATTER|Must be formatted equally|Model name is formatted using pascal case| ## Property naming -These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| |NO_SPECIAL_CHAR|No special characters| Special characters are replaced by their name, for example `!` is replaced with `exclamation`. For go `_` and `$` are an exception to this rule. | @@ -26,7 +26,7 @@ These are the constraints that is applied to object properties and the naming of ## Enum key constraints -These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| diff --git a/docs/constraints/Java.md b/docs/constraints/Java.md index 1a3072f9f4..29f0ff2f82 100644 --- a/docs/constraints/Java.md +++ b/docs/constraints/Java.md @@ -3,7 +3,7 @@ These are the Java specific constraints applied to the MetaModel before reaching the presets. [Read here to get a more general idea on the overall process](../input-processing.md) of converting a MetaModel to a ConstrainedMetaModel. ## Model Naming -These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| @@ -14,7 +14,7 @@ These are the constraints that is applied to model naming. The `Rule key` is wha |NAMING_FORMATTER|Must be formatted equally|Model name is formatted using pascal case| ## Property naming -These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| |NO_SPECIAL_CHAR|No special characters| Special characters are replaced by their name, for example `!` is replaced with `exclamation`. For java `_` and `$` are an exception to this rule. | @@ -26,7 +26,7 @@ These are the constraints that is applied to object properties and the naming of ## Enum key constraints -These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| diff --git a/docs/constraints/JavaScript.md b/docs/constraints/JavaScript.md index ce3c090fbe..742a580086 100644 --- a/docs/constraints/JavaScript.md +++ b/docs/constraints/JavaScript.md @@ -3,7 +3,7 @@ These are the JavaScript specific constraints applied to the MetaModel before reaching the presets. [Read here to get a more general idea on the overall process](../input-processing.md) of converting a MetaModel to a ConstrainedMetaModel. ## Model Naming -These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| @@ -14,7 +14,7 @@ These are the constraints that is applied to model naming. The `Rule key` is wha |NAMING_FORMATTER|Must be formatted equally|Model name is formatted using pascal case| ## Property naming -These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| |NO_SPECIAL_CHAR|No special characters| Special characters are replaced by their name, for example `!` is replaced with `exclamation`. For javascript `_` and `$` are an exception to this rule. | @@ -26,7 +26,7 @@ These are the constraints that is applied to object properties and the naming of ## Enum key constraints -These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| diff --git a/docs/constraints/Python.md b/docs/constraints/Python.md new file mode 100644 index 0000000000..3aad65b5d8 --- /dev/null +++ b/docs/constraints/Python.md @@ -0,0 +1,41 @@ +# Python Constraints + +These are the Python specific constraints applied to the MetaModel before reaching the presets. [Read here to get a more general idea on the overall process](../input-processing.md) of converting a MetaModel to a ConstrainedMetaModel. + +## Model Naming +These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). + +|Rule key|Rule|Resolution| +|---|---|---| +|NO_SPECIAL_CHAR|No special characters| Special characters are replaced by their name, for example `!` is replaced with `exclamation`. For python `_` is an exception to this rule. | +|NO_NUMBER_START_CHAR|No numbers as starting characters|Default behavior is pre pending `number_` in front of the first character| +|NO_EMPTY_VALUE|No empty values|Default behavior is to use `empty` as name. | +|NO_RESERVED_KEYWORDS|No reserved keywords|Python has a list of reserved keywords ([see the full list here](../../src/generators/python/Constants.ts))| +|NAMING_FORMATTER|Must be formatted equally|Model name is formatted using pascal case| + +## Property naming +These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). +|Rule key|Rule|Resolution| +|---|---|---| +|NO_SPECIAL_CHAR|No special characters| Special characters are replaced by their name, for example `!` is replaced with `exclamation`. For python `_` is an exception to this rule. | +|NO_NUMBER_START_CHAR|No numbers as starting characters|Default behavior is pre pending `number_` in front of the first character| +|NO_EMPTY_VALUE|No empty values|Default behavior is to use `empty` as name. | +|NO_RESERVED_KEYWORDS|No reserved keywords|Python has a list of reserved keywords ([see the full list here](../../src/generators/python/Constants.ts))| +|NAMING_FORMATTER|Must be formatted equally|Property name is formatted using camel case| +|NO_DUPLICATE_PROPERTIES|No duplicate properties|If any of the above constraints changes the property name, we must make sure that no duplicates exist within the same object. If any is encountered `reserved_` is pre-pended. This is done recursively until no duplicates are found.| + + +## Enum key constraints +These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). + +|Rule key|Rule|Resolution| +|---|---|---| +|NO_SPECIAL_CHAR|No special characters| Special characters are replaced by their name, for example `!` is replaced with `exclamation`. For python `_` is an exception to this rule. | +|NO_NUMBER_START_CHAR|No numbers as starting characters|Default behavior is pre pending `number_` in front of the first character| +|NO_EMPTY_VALUE|No empty values|Default behavior is to use `empty` as name. | +|NO_RESERVED_KEYWORDS|No reserved keywords|Python has a list of reserved keywords ([see the full list here](../../src/generators/python/Constants.ts))| +|NAMING_FORMATTER|Must be formatted equally|Enum name is formatted using constant case| +|NO_DUPLICATE_KEYS|No duplicate enum keys|If any of the above constraints changes the enum key, we must make sure that no duplicates exist within the same enum. If any is encountered `reserved_` is pre-pended. This is done recursively until no duplicates are found.| + +## Constant +These are the constraints that are applied to constants. Currently, there are no hooks one can overwrite inside it. diff --git a/docs/constraints/README.md b/docs/constraints/README.md index 9100960a4d..5d52fc4841 100644 --- a/docs/constraints/README.md +++ b/docs/constraints/README.md @@ -12,6 +12,8 @@ class &name {} class 1name {} ``` +## Customization + There are many rules as such, but to get the full description about the default constraints here: - [C#](./constraints/CSharp.md) @@ -21,6 +23,7 @@ There are many rules as such, but to get the full description about the default - [JavaScript](./constraints/JavaScript.md) - [Rust](./constraints/Rust.md) - [TypeScript](./constraints/TypeScript.md) +- [Python](./constraints/Python.md) Even though there are many of these constraints, there might be reasons you want to customize the behavior to make it suit your use-case. Therefore each of the constraint rules can be overwritten completely and allow for you to implement your own behavior. @@ -28,7 +31,7 @@ We define these as two types, either you only want to change part of the logic, - [Overwriting the formatter](../examples/overwrite-naming-formatting) and keep the rest of the constraints as is. - [Overwriting the entire naming constraint logic](../examples/overwrite-default-constraint) keeping none of the existing functionality which handles edge cases. It is recommended to **NOT** use this if it can be avoided, as you will limit yourself to what inputs can be generated to models. So make sure you know what you are doing :laughing: -# Type mapping +## Type mapping To make it easier to use the meta models in presets and generators, we need to figure out the types for each model. This is to enable you to access the types from a property rather then calling a function. This is especially relevant because Modelina cannot fit all use-cases out of the box, and we therefore strive to make it tailorable to what ever your needs may be. The type mapping is one of those things that enable you to fine tune the types for your purpose. Of course it's not all output formats that have a type such as JavaScript, therefore these are only used for strongly typed outputs. diff --git a/docs/constraints/Rust.md b/docs/constraints/Rust.md index e30e88effe..611532c9c5 100644 --- a/docs/constraints/Rust.md +++ b/docs/constraints/Rust.md @@ -3,7 +3,7 @@ These are the Rust specific constraints applied to the MetaModel before reaching the presets. [Read here to get a more general idea on the overall process](../input-processing.md) of converting a MetaModel to a ConstrainedMetaModel. ## Model Naming -These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| @@ -14,7 +14,7 @@ These are the constraints that is applied to model naming. The `Rule key` is wha |NAMING_FORMATTER|Must be formatted equally|Model name is formatted using pascal case| ## Property naming -These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| |NO_SPECIAL_CHAR|No special characters| Special characters are replaced by their name, for example `!` is replaced with `exclamation`. For Rust `_`, ` ` and `$` are an exception to this rule. | @@ -25,7 +25,7 @@ These are the constraints that is applied to object properties and the naming of |NO_DUPLICATE_PROPERTIES|No duplicate properties|If any of the above constraints changes the property name, we must make sure that no duplicates exist within the same object. If any is encountered `reserved_` is pre-pended. This is done recursively until no duplicates are found.| ## Enum key constraints -These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| diff --git a/docs/constraints/TypeScript.md b/docs/constraints/TypeScript.md index 3cc175fa0b..85fc33b9cc 100644 --- a/docs/constraints/TypeScript.md +++ b/docs/constraints/TypeScript.md @@ -3,7 +3,7 @@ These are the TypeScript specific constraints applied to the MetaModel before reaching the presets. [Read here to get a more general idea on the overall process](../input-processing.md) of converting a MetaModel to a ConstrainedMetaModel. ## Model Naming -These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| @@ -14,7 +14,7 @@ These are the constraints that is applied to model naming. The `Rule key` is wha |NAMING_FORMATTER|Must be formatted equally|Model name is formatted using pascal case| ## Property naming -These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| |NO_SPECIAL_CHAR|No special characters| Special characters are replaced by their name, for example `!` is replaced with `exclamation`. For typescript `_` and `$` are an exception to this rule. | @@ -26,7 +26,7 @@ These are the constraints that is applied to object properties and the naming of ## Enum key constraints -These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](../constraints.md#Customization). +These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). |Rule key|Rule|Resolution| |---|---|---| 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/package-lock.json b/package-lock.json index 6178eca89a..9574e6f9b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@asyncapi/modelina", - "version": "2.0.13", + "version": "2.0.14", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@asyncapi/modelina", - "version": "2.0.13", + "version": "2.0.14", "license": "Apache-2.0", "dependencies": { "@apidevtools/json-schema-ref-parser": "^9.0.9", diff --git a/package.json b/package.json index 1740361360..375294a7f8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@asyncapi/modelina", - "version": "2.0.13", + "version": "2.0.14", "description": "Library for generating data models based on inputs such as AsyncAPI, OpenAPI, or JSON Schema documents", "license": "Apache-2.0", "homepage": "https://www.modelina.org", 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) " `;