-
-
Notifications
You must be signed in to change notification settings - Fork 408
Nullable required fields should not have default value #2520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Nullable required fields should not have default value #2520
Conversation
src/datamodel_code_generator/model/template/pydantic_v2/BaseModel.jinja2
Outdated
Show resolved
Hide resolved
|
This PR closes the following issues: Please link this PR with the issue as described here. Thanks! |
…del.jinja2 Co-authored-by: Antonio Spadaro <ilovelinux@users.noreply.github.com>
CodSpeed Performance ReportMerging #2520 will not alter performanceComparing Summary
Footnotes
|
|
@raymondbutcher |
| {{ field.name }}: {{ field.type_hint }} | ||
| {%- endif %} | ||
| {%- if not (field.required or (field.represented_default == 'None' and field.strip_default_none)) or field.data_type.is_optional | ||
| {%- if not field.required and (field.represented_default != 'None' or not field.strip_default_none) or field.data_type.is_optional) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My fault, I forgot a parenthesis.
Should fix #2520 (comment)
Please check the logic compared to your original proposal
| {%- if not field.required and (field.represented_default != 'None' or not field.strip_default_none) or field.data_type.is_optional) | |
| {%- if not field.required and (field.represented_default != 'None' or not field.strip_default_none or field.data_type.is_optional) |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2520 +/- ##
=======================================
Coverage 99.59% 99.59%
=======================================
Files 73 73
Lines 7742 7744 +2
Branches 905 905
=======================================
+ Hits 7711 7713 +2
Misses 14 14
Partials 17 17
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Hi. Thanks for the project!
My OpenAPI spec has some fields as both nullable and required, but they are generating models with default values like this:
Those default values make the fields optional, so it's not working as expected.
This fix also highlighted what looks to be a bug in an existing test that is checking for an incorrect result: