-
-
Notifications
You must be signed in to change notification settings - Fork 426
Add --jsonschema-version and --openapi-version CLI options #2917
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
Conversation
|
Warning Rate limit exceeded@koxudaxi has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 1 minutes and 19 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (14)
📝 WalkthroughWalkthroughThis pull request introduces version detection capabilities for JSON Schema and OpenAPI specifications by adding runtime detection functions, new enum types, and exception classes. The changes propagate these new versioning features through configuration classes, CLI arguments, and type definitions across the codebase. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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 |
|
📚 Docs Preview: https://pr-2917.datamodel-code-generator.pages.dev |
CodSpeed Performance ReportMerging #2917 will improve performance by 18.93%Comparing
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | WallTime | test_perf_stripe_style_pydantic_v2 |
2 s | 1.7 s | +17.41% |
| ⚡ | WallTime | test_perf_multiple_files_input |
3.7 s | 3.1 s | +18.46% |
| ⚡ | WallTime | test_perf_duplicate_names |
991 ms | 842.2 ms | +17.67% |
| ⚡ | WallTime | test_perf_deep_nested |
6 s | 5.1 s | +16.35% |
| ⚡ | WallTime | test_perf_graphql_style_pydantic_v2 |
820 ms | 700 ms | +17.14% |
| ⚡ | WallTime | test_perf_kubernetes_style_pydantic_v2 |
2.6 s | 2.2 s | +17.28% |
| ⚡ | WallTime | test_perf_complex_refs |
2 s | 1.7 s | +17.58% |
| ⚡ | WallTime | test_perf_aws_style_openapi_pydantic_v2 |
1.9 s | 1.7 s | +17.01% |
| ⚡ | WallTime | test_perf_large_models_pydantic_v2 |
3.6 s | 3.1 s | +18.93% |
| ⚡ | WallTime | test_perf_all_options_enabled |
6.7 s | 5.8 s | +16.27% |
| ⚡ | WallTime | test_perf_openapi_large |
2.9 s | 2.5 s | +16.16% |
Footnotes
-
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. ↩
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.
Actionable comments posted: 0
🧹 Nitpick comments (2)
tests/main/jsonschema/test_main_jsonschema.py (1)
7908-8015: Version-detection and exception tests look correct; verify they’re not duplicated elsewhere in this file
- The assertions for
detect_jsonschema_versionanddetect_openapi_versionline up with the current implementations (including default behaviors for missing/non‑string fields), and the exception tests validate both attributes and message text. From a correctness standpoint these tests are solid.- However, the PR summary indicates there may already be an existing group of schema-version tests in this module; these new classes appear to re-test the same behaviors. If that’s the case, consider:
- Removing the older duplicates, or
- Moving these unit-style tests into a smaller dedicated module (e.g.
tests/test_schema_version_detection.py) to keep this already-large file from growing further.src/datamodel_code_generator/__init__.py (1)
309-320: Consider validating swagger field value for robustness.The function checks only for the presence of the
swaggerkey without validating its value. OpenAPI 2.0 specs should haveswagger: "2.0". While rare, a malformed spec withswagger: nullor other unexpected values would incorrectly be detected as V20.🔎 Suggested improvement
def detect_openapi_version(data: dict[str, Any]) -> OpenAPIVersion: """Detect OpenAPI version from openapi/swagger field.""" - if "swagger" in data: + swagger = data.get("swagger") + if isinstance(swagger, str) and swagger.startswith("2."): return OpenAPIVersion.V20 openapi = data.get("openapi", "")
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (11)
src/datamodel_code_generator/__init__.pysrc/datamodel_code_generator/__main__.pysrc/datamodel_code_generator/_types/generate_config_dict.pysrc/datamodel_code_generator/_types/parser_config_dicts.pysrc/datamodel_code_generator/arguments.pysrc/datamodel_code_generator/cli_options.pysrc/datamodel_code_generator/config.pysrc/datamodel_code_generator/enums.pytests/data/expected/main/input_model/config_class.pytests/main/jsonschema/test_main_jsonschema.pytests/main/test_public_api_signature_baseline.py
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2026-01-02T08:25:19.839Z
Learnt from: koxudaxi
Repo: koxudaxi/datamodel-code-generator PR: 2890
File: tests/data/expected/main/jsonschema/ref_nullable_with_constraint.py:14-15
Timestamp: 2026-01-02T08:25:19.839Z
Learning: The datamodel-code-generator currently generates RootModel subclasses with an explicit `root` field annotation (e.g., `class StringType(RootModel[str]): root: str`). This is existing behavior of the code generator and should not be flagged as an issue introduced by new changes.
Applied to files:
tests/data/expected/main/input_model/config_class.py
🧬 Code graph analysis (9)
tests/data/expected/main/input_model/config_class.py (2)
src/datamodel_code_generator/enums.py (2)
JsonSchemaVersion(243-254)OpenAPIVersion(257-267)src/datamodel_code_generator/model/type_alias.py (1)
TypeAlias(37-42)
src/datamodel_code_generator/config.py (1)
src/datamodel_code_generator/enums.py (2)
JsonSchemaVersion(243-254)OpenAPIVersion(257-267)
src/datamodel_code_generator/_types/parser_config_dicts.py (1)
src/datamodel_code_generator/enums.py (2)
JsonSchemaVersion(243-254)OpenAPIVersion(257-267)
tests/main/test_public_api_signature_baseline.py (1)
src/datamodel_code_generator/enums.py (2)
JsonSchemaVersion(243-254)OpenAPIVersion(257-267)
src/datamodel_code_generator/__main__.py (1)
src/datamodel_code_generator/enums.py (2)
JsonSchemaVersion(243-254)OpenAPIVersion(257-267)
tests/main/jsonschema/test_main_jsonschema.py (2)
src/datamodel_code_generator/enums.py (2)
JsonSchemaVersion(243-254)OpenAPIVersion(257-267)src/datamodel_code_generator/__init__.py (4)
detect_jsonschema_version(293-306)detect_openapi_version(309-320)UnsupportedVersionError(377-385)SchemaValidationError(388-395)
src/datamodel_code_generator/__init__.py (1)
src/datamodel_code_generator/enums.py (2)
JsonSchemaVersion(243-254)OpenAPIVersion(257-267)
src/datamodel_code_generator/_types/generate_config_dict.py (1)
src/datamodel_code_generator/enums.py (2)
JsonSchemaVersion(243-254)OpenAPIVersion(257-267)
src/datamodel_code_generator/arguments.py (1)
src/datamodel_code_generator/enums.py (2)
JsonSchemaVersion(243-254)OpenAPIVersion(257-267)
🪛 Ruff (0.14.10)
src/datamodel_code_generator/__init__.py
1046-1046: Unused noqa directive (unused: F822)
Remove unused noqa directive
(RUF100)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
- GitHub Check: 3.10 on macOS
- GitHub Check: 3.13 on macOS
- GitHub Check: 3.11 on macOS
- GitHub Check: 3.10 on Windows
- GitHub Check: 3.14 on macOS
- GitHub Check: 3.14 on Windows
- GitHub Check: 3.11 on Windows
- GitHub Check: 3.13 on Windows
- GitHub Check: 3.12 on Windows
- GitHub Check: benchmarks
- GitHub Check: Analyze (python)
🔇 Additional comments (20)
src/datamodel_code_generator/enums.py (2)
243-254: LGTM: JsonSchemaVersion enum covers major JSON Schema drafts.The enum appropriately includes the main JSON Schema drafts. The naming correctly reflects the specification conventions (earlier drafts use "draft-XX" while newer ones use year-month format).
257-268: LGTM: OpenAPIVersion enum covers major OpenAPI versions.The enum appropriately includes OpenAPI 2.0 (Swagger) and OpenAPI 3.x versions. The docstring helpfully notes that different versions have different semantics.
src/datamodel_code_generator/_types/parser_config_dicts.py (1)
50-51: LGTM: Version fields properly added to ParserConfigDict.The new fields are correctly typed with NotRequired and reference the appropriate enum types. The placement near the top of the configuration dictionary is logical for version-related settings.
tests/main/test_public_api_signature_baseline.py (2)
65-66: LGTM: Baseline signature updated with backward-compatible defaults.The version parameters are correctly added with
.Autodefaults, ensuring backward compatibility. The placement afterinput_file_typeis logical since version detection would occur after determining the input format.
207-208: LGTM: Parser baseline signature consistently updated.The parser baseline is updated consistently with the generate function baseline, maintaining the same default values and parameter ordering.
src/datamodel_code_generator/cli_options.py (1)
55-57: LGTM: Version options added to manual documentation set.The new CLI options are appropriately marked for manual documentation. Note that
CLI_OPTION_METAentries are not added for these options, which means they won't appear in auto-generated documentation categories. This may be intentional during initial implementation.src/datamodel_code_generator/_types/generate_config_dict.py (1)
46-47: LGTM: Version fields properly added to GenerateConfigDict.The changes mirror the updates in ParserConfigDict, maintaining consistency across configuration types. The fields are correctly typed and appropriately placed with other input-related configuration.
tests/data/expected/main/input_model/config_class.py (1)
80-82: LGTM! Type aliases and TypedDict fields are correctly defined.The new
JsonSchemaVersionandOpenAPIVersiontype aliases correctly mirror the enum values fromsrc/datamodel_code_generator/enums.py. The fields are appropriately marked asNotRequiredin theGenerateConfigTypedDict, allowing these configuration options to be optional.Also applies to: 101-102, 128-129
src/datamodel_code_generator/arguments.py (2)
29-29: LGTM! Imports are correctly placed.The imports of
JsonSchemaVersionandOpenAPIVersionare properly positioned in alphabetical order within the existing enum imports.Also applies to: 33-33
151-168: LGTM! CLI options are well-designed with clear help text.The
--jsonschema-versionand--openapi-versionoptions are properly configured:
- Placed in the appropriate
base_optionsgroup- Choices are correctly derived from the enum values
- Help text clearly explains the 'auto' detection behavior and when to override explicitly
- Both options align with the PR objectives to provide comprehensive version support
src/datamodel_code_generator/config.py (3)
25-25: LGTM! Imports are correctly placed.The imports of
JsonSchemaVersionandOpenAPIVersionare properly positioned in alphabetical order within the existing enum imports.Also applies to: 29-29
83-84: LGTM! Configuration fields are well-positioned and correctly defined.The
jsonschema_versionandopenapi_versionfields are appropriately added toGenerateConfig:
- Positioned logically with other input-related configuration (after
input_file_type)- Default values use
.Autoenum members, consistent with the CLI documentation stating 'auto' as default behavior- Type annotations are correct and align with the enum definitions
231-232: LGTM! Parser configuration fields mirror GenerateConfig.The
jsonschema_versionandopenapi_versionfields inParserConfigare consistent with theGenerateConfigadditions:
- Same type annotations and default values
- Positioned early in the parser configuration, which is appropriate for version detection that may affect parsing behavior
- Maintains consistency across configuration classes
src/datamodel_code_generator/__main__.py (3)
66-70: LGTM!The new enum imports are correctly placed in alphabetical order within the existing import block.
496-497: LGTM!The new version fields are properly typed with sensible
Autodefaults, and are logically placed alongside theinput_file_typefield.
945-946: The version parameters are properly propagated. BothGenerateConfigandParserConfigdefinejsonschema_versionandopenapi_versionfields, and the_create_parser_config()function explicitly filters and includes any fields fromGenerateConfigthat exist in the target parser config class. Since all parser configs (JSONSchemaParserConfig,OpenAPIParserConfig,GraphQLParserConfig) inherit fromParserConfig, the version parameters are correctly forwarded through the configuration chain to each parser instance.src/datamodel_code_generator/__init__.py (4)
46-50: LGTM!Imports are correctly added from
datamodel_code_generator.enumsand maintain alphabetical ordering.
293-306: LGTM!The detection logic correctly parses common JSON Schema draft identifiers from
$schemaURLs. The default to Draft07 is a reasonable choice for unspecified or unrecognized schemas.
373-400: LGTM!The exception hierarchy is well-designed with proper inheritance from the base
Errorclass. The classes appropriately store input parameters as instance attributes for programmatic access, and theVersionMismatchWarningas aUserWarningsubclass allows standard warning filtering.
1030-1048: LGTM!All new public exports are correctly added to
__all__in alphabetical order, maintaining consistency with the existing entries.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2917 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 93 93
Lines 16865 16912 +47
Branches 1952 1958 +6
=========================================
+ Hits 16865 16912 +47
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:
|
0b417f4 to
27f10b4
Compare
Fixes: #1592
Related: #1042
Summary by CodeRabbit
Release Notes
New Features
--jsonschema-versionand--openapi-versionto explicitly specify schema versions during code generationImprovements
✏️ Tip: You can customize this high-level summary in your review settings.