Add support for setting strict_nullable from CLI#493
Conversation
`datamodel-code-generator` supports the OpenAPI 3.0 strict handling of nullable properties. Allow that to be enforced in code generation from the command line
for more information, see https://pre-commit.ci
|
I see that this is a slightly different solution from the one proopsed in #474. Happy to have either merged. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (12)
✅ Files skipped from review due to trivial changes (6)
WalkthroughAdds a new CLI flag Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant CLI
participant Generator
participant OpenAPIParser
participant Filesystem
User->>CLI: run with --strict-nullable
CLI->>Generator: call generate_code(strict_nullable=True, ...)
Generator->>OpenAPIParser: parse spec (strict_nullable=True)
OpenAPIParser-->>Generator: return models/templates using strict nullable rules
Generator->>Filesystem: write generated files (models.py, main.py, ...)
Filesystem-->>User: generated project files
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #493 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 17 17
Lines 1199 1204 +5
Branches 126 126
=========================================
+ Hits 1199 1204 +5
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:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
docs/cli-reference.md (1)
92-99: LGTM — new section is consistent with the rest of the reference.Optional nit: the inline help string copied across the snapshots ("Strictly follow nullable attributes in OpenAPI schemas.") is a bit ambiguous about what happens to fields without an explicit
nullableattribute — the expanded prose here ("Respect explicit OpenAPI nullable flags when generating models.") is clearer. If you want to tighten the CLI help string, the source lives infastapi_code_generator/cli.pyand the snapshot inREADME.md/docs/index.mdwill follow.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@docs/cli-reference.md` around lines 92 - 99, Update the CLI help string for the --strict-nullable option in fastapi_code_generator/cli.py (look for the add_argument call or the argument named "--strict-nullable" / dest "strict_nullable") to a clearer message such as "Respect explicit OpenAPI nullable flags when generating models." so it accurately indicates behavior for fields without an explicit nullable attribute; after updating, regenerate the README.md/docs/index.md CLI snapshot so documentation matches the new help text.fastapi_code_generator/cli.py (1)
93-97: Help text drifts from config metadata description.The Typer
helphere says “Strictly follow nullable attributes in OpenAPI schemas.”, while the matching field infastapi_code_generator/config.py(and the rendered docs indocs/llms-full.txt“Tested CLI Scenarios”, plus thecli_docmarker on the new test) describes it as “Respect explicit OpenAPI nullable flags when generating models.” Both are ultimately surfaced to users (one in--help, the other in the generated docs/scenarios). Consider aligning them so the CLI help and the docs say the same thing.♻️ Proposed alignment (either direction works)
strict_nullable: bool = typer.Option( False, "--strict-nullable", - help="Strictly follow nullable attributes in OpenAPI schemas.", + help="Respect explicit OpenAPI nullable flags when generating models.", ),🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@fastapi_code_generator/cli.py` around lines 93 - 97, The Typer option help text for strict_nullable (the typer.Option declared in cli.py) is inconsistent with the config field description in fastapi_code_generator/config.py and the docs; update the help string for strict_nullable to match the canonical phrasing used in the config/docs (e.g., "Respect explicit OpenAPI nullable flags when generating models.") so CLI --help and generated docs stay aligned; confirm the symbol strict_nullable and its typer.Option call are the only occurrences to change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@tests/data/expected/openapi/default_template/nullable_test_strict/models.py`:
- Around line 19-21: The fixture description for the field represented by the
nickname: Optional[str] = Field(None, description="User's nickname (implicitly
nullable)") is misleading under --strict-nullable because absence from required
makes it optional/absent, not nullable; update the source YAML/fixture
description for the nickname property to explicitly state it's optional but not
nullable under strict mode (e.g., "User's nickname (optional, not nullable under
strict mode)") so the generated model's Field(... description=...) accurately
reflects strict-nullable semantics.
---
Nitpick comments:
In `@docs/cli-reference.md`:
- Around line 92-99: Update the CLI help string for the --strict-nullable option
in fastapi_code_generator/cli.py (look for the add_argument call or the argument
named "--strict-nullable" / dest "strict_nullable") to a clearer message such as
"Respect explicit OpenAPI nullable flags when generating models." so it
accurately indicates behavior for fields without an explicit nullable attribute;
after updating, regenerate the README.md/docs/index.md CLI snapshot so
documentation matches the new help text.
In `@fastapi_code_generator/cli.py`:
- Around line 93-97: The Typer option help text for strict_nullable (the
typer.Option declared in cli.py) is inconsistent with the config field
description in fastapi_code_generator/config.py and the docs; update the help
string for strict_nullable to match the canonical phrasing used in the
config/docs (e.g., "Respect explicit OpenAPI nullable flags when generating
models.") so CLI --help and generated docs stay aligned; confirm the symbol
strict_nullable and its typer.Option call are the only occurrences to change.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 8d62d1d2-ef25-44d1-ab7f-435176510115
📒 Files selected for processing (15)
README.mddocs/cli-reference.mddocs/index.mddocs/llms-full.txtdocs/supported_formats.mdfastapi_code_generator/_types/generate_config_dict.pyfastapi_code_generator/cli.pyfastapi_code_generator/config.pyfastapi_code_generator/prompt_data.pytests/data/expected/openapi/default_template/nullable_test/main.pytests/data/expected/openapi/default_template/nullable_test/models.pytests/data/expected/openapi/default_template/nullable_test_strict/main.pytests/data/expected/openapi/default_template/nullable_test_strict/models.pytests/data/openapi/default_template/nullable_test.yamltests/main/test_main.py
Breaking Change AnalysisResult: No breaking changes detected Reasoning: The This analysis was performed by repository automation using PR labels and the |
datamodel-code-generatorsupports the OpenAPI 3.0 strict handling of nullable properties. Allow that to be enforced in code generation from the command lineProbably addresses issue #473
Summary by CodeRabbit
New Features
Documentation
Tests