Skip to content

Use semantic None checks in templates#3480

Closed
koxudaxi wants to merge 1 commit into
mainfrom
fix/template-none-default-checks
Closed

Use semantic None checks in templates#3480
koxudaxi wants to merge 1 commit into
mainfrom
fix/template-none-default-checks

Conversation

@koxudaxi

@koxudaxi koxudaxi commented Jun 23, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • Tests
    • Added comprehensive test coverage for strip-default-none behavior with root models and schema ID templates.
    • Expanded test coverage for custom template support with semantic configurations.
    • Added test cases for multiple Pydantic model types including BaseModel and dataclass variants across different versions.

@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 14fd89bc-c1cf-4b22-b367-d7fda67ac188

📥 Commits

Reviewing files that changed from the base of the PR and between ea7a3aa and f968fdc.

⛔ Files ignored due to path filters (8)
  • src/datamodel_code_generator/model/template/dataclass.jinja2 is excluded by none and included by none
  • src/datamodel_code_generator/model/template/pydantic_v2/BaseModel.jinja2 is excluded by none and included by none
  • src/datamodel_code_generator/model/template/pydantic_v2/RootModel.jinja2 is excluded by none and included by none
  • src/datamodel_code_generator/model/template/pydantic_v2/dataclass.jinja2 is excluded by none and included by none
  • tests/data/jsonschema/strip_default_none_root_string_none.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/strip_default_none_semantic.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/templates_extensions/pydantic_v2/BaseModel.jinja2 is excluded by none and included by none
  • tests/data/templates_schema_id/pydantic_v2/BaseModel.jinja2 is excluded by none and included by none
📒 Files selected for processing (7)
  • tests/data/expected/main/jsonschema/strip_default_none_root_string_none.py
  • tests/data/expected/main/jsonschema/strip_default_none_semantic_schema_id.py
  • tests/data/expected/main/openapi/strip_default_none_semantic_custom_template_extensions.py
  • tests/data/expected/main/openapi/strip_default_none_semantic_pydantic_v2.py
  • tests/data/expected/main/openapi/strip_default_none_semantic_pydantic_v2_dataclass.py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/main/openapi/test_main_openapi.py

📝 Walkthrough

Walkthrough

Adds five expected output fixture files and four new or extended test cases for the --strip-default-none flag. The fixtures cover pydantic v2 BaseModel, dataclass, RootModel, and custom-template scenarios in both JSON Schema and OpenAPI contexts. Tests assert that string 'None' defaults are preserved while null defaults are stripped.

Changes

strip-default-none test fixtures and regression tests

Layer / File(s) Summary
Expected output fixture files
tests/data/expected/main/jsonschema/strip_default_none_root_string_none.py, tests/data/expected/main/jsonschema/strip_default_none_semantic_schema_id.py, tests/data/expected/main/openapi/strip_default_none_semantic_pydantic_v2.py, tests/data/expected/main/openapi/strip_default_none_semantic_pydantic_v2_dataclass.py, tests/data/expected/main/openapi/strip_default_none_semantic_custom_template_extensions.py
Five new canonical fixture files defining expected generated models: a RootModel[str] with root defaulting to 'None'; a BaseModel with __path__ and three optional fields; pydantic v2 BaseModel and dataclass variants of SemanticNoneModel; and a custom-template extensions variant — all retaining string 'None' defaults while stripping actual null defaults.
Regression and E2E tests
tests/main/jsonschema/test_main_jsonschema.py, tests/main/openapi/test_main_openapi.py
Adds two new jsonschema tests (root model string-None preservation; schema_id custom template with cache isolation) and extends the openapi parametrize table with pydantic_v2.BaseModel and pydantic_v2.dataclass entries, plus a new isolated-cache custom-template test.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐇 Hopping through the schemas, checking every None,
A string that says 'None' — not null, just a pun!
The fixtures are planted, the asserts all gleam,
Default stripping logic now lives in a dream.
With pydantic v2 and dataclass in tow,
The rabbit stamps "LGTM" and off the tests go! 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Use semantic None checks in templates' directly describes the main objective of the PR, which is to implement semantic None default value checking within Jinja templates for code generation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/template-none-default-checks

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

📚 Docs Preview: https://pr-3480.datamodel-code-generator.pages.dev

@codspeed-hq

codspeed-hq Bot commented Jun 23, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

✅ 11 untouched benchmarks
⏩ 98 skipped benchmarks1


Comparing fix/template-none-default-checks (f968fdc) with main (ea7a3aa)

Open in CodSpeed

Footnotes

  1. 98 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.

@codecov

codecov Bot commented Jun 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (ea7a3aa) to head (f968fdc).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3480   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          151       151           
  Lines        32498     32510   +12     
  Branches      3783      3783           
=========================================
+ Hits         32498     32510   +12     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

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

☔ View full report in Codecov by Harness.
📢 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 closed this Jun 23, 2026
@gaborbernat gaborbernat deleted the fix/template-none-default-checks branch July 10, 2026 18:39
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.

1 participant