Skip to content

Conversation

@raymondbutcher
Copy link

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:

class Person(BaseModel):
    first_name: Annotated[str | None, Field(alias='firstName')] = None
    last_name: Annotated[str | None, Field(alias='lastName')] = None

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:

@ilovelinux
Copy link
Collaborator

This PR closes the following issues:

Please link this PR with the issue as described here. Thanks!

gaborbernat and others added 2 commits November 3, 2025 06:06
@gaborbernat gaborbernat enabled auto-merge (squash) November 3, 2025 14:07
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 3, 2025

CodSpeed Performance Report

Merging #2520 will not alter performance

Comparing raymondbutcher:required-nullable-annotated (c23fa2f) with main (000b177)

Summary

✅ 30 untouched
⏩ 3 skipped1

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@koxudaxi
Copy link
Owner

@raymondbutcher
Thank you for creating the PR.
Could you plesae fix the unittest ?

    raise rewrite_traceback_stack(source=source)
  File "/Users/runner/work/datamodel-code-generator/datamodel-code-generator/.tox/3.11/lib/python3.11/site-packages/datamodel_code_generator/model/template/pydantic_v2/BaseModel.jinja2", line 35, in template
    {%- if not field.required and (field.represented_default != 'None' or not field.strip_default_none) or field.data_type.is_optional)
    ^^^^^^^^^^^^^^^^^^^^^^^^^
jinja2.exceptions.TemplateSyntaxError: unexpected ')'```

{{ 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)
Copy link
Collaborator

@ilovelinux ilovelinux Nov 16, 2025

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

Suggested change
{%- 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)

@ilovelinux ilovelinux linked an issue Nov 17, 2025 that may be closed by this pull request
@codecov
Copy link

codecov bot commented Nov 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.59%. Comparing base (000b177) to head (c23fa2f).

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           
Flag Coverage Δ
unittests 99.59% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@koxudaxi koxudaxi requested a review from ilovelinux November 29, 2025 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: All of the fields have a None default

4 participants