Skip to content

Conversation

@koxudaxi
Copy link
Owner

@koxudaxi koxudaxi commented Jan 4, 2026

Fixes: #1592
Related: #1042

Summary by CodeRabbit

Release Notes

  • New Features

    • Added command-line options --jsonschema-version and --openapi-version to explicitly specify schema versions during code generation
    • Implemented automatic detection of JSON Schema and OpenAPI versions from input data
  • Improvements

    • Enhanced error handling with new exception types for unsupported versions, validation errors, and version mismatches to provide clearer feedback when schema version issues occur

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 4, 2026

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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.

📥 Commits

Reviewing files that changed from the base of the PR and between 0b417f4 and b5ca474.

⛔ Files ignored due to path filters (1)
  • tests/data/openapi/openapi_version_nullable.yaml is excluded by !tests/data/**/*.yaml and included by none
📒 Files selected for processing (14)
  • src/datamodel_code_generator/__init__.py
  • src/datamodel_code_generator/__main__.py
  • src/datamodel_code_generator/_types/generate_config_dict.py
  • src/datamodel_code_generator/_types/parser_config_dicts.py
  • src/datamodel_code_generator/arguments.py
  • src/datamodel_code_generator/cli_options.py
  • src/datamodel_code_generator/config.py
  • src/datamodel_code_generator/enums.py
  • src/datamodel_code_generator/parser/base.py
  • src/datamodel_code_generator/parser/openapi.py
  • tests/data/expected/main/input_model/config_class.py
  • tests/data/expected/main/openapi/openapi_version_nullable_v31.py
  • tests/main/openapi/test_main_openapi.py
  • tests/main/test_public_api_signature_baseline.py
📝 Walkthrough

Walkthrough

This 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

Cohort / File(s) Summary
Core Version Detection & Exceptions
src/datamodel_code_generator/__init__.py
Added new enums (JsonSchemaVersion, OpenAPIVersion), detection functions (detect_jsonschema_version, detect_openapi_version), and exception hierarchy (SchemaVersionError, UnsupportedVersionError, SchemaValidationError, VersionMismatchWarning); exported all via all and getattr.
Enum Definitions
src/datamodel_code_generator/enums.py
Introduced JsonSchemaVersion and OpenAPIVersion enums with version member definitions; added to public all list.
Configuration Classes
src/datamodel_code_generator/config.py, src/datamodel_code_generator/__main__.py
Extended GenerateConfig, ParserConfig, and main.Config with new fields: jsonschema_version and openapi_version (both defaulting to Auto); threaded parameters through run_generate_from_config to generate().
Type Definitions
src/datamodel_code_generator/_types/generate_config_dict.py, src/datamodel_code_generator/_types/parser_config_dicts.py
Added NotRequired fields jsonschema_version and openapi_version to GenerateConfigDict and ParserConfigDict TypedDicts.
CLI Integration
src/datamodel_code_generator/arguments.py, src/datamodel_code_generator/cli_options.py
Added --jsonschema-version and --openapi-version CLI arguments with choices from enums; marked options as manually documented in MANUAL_DOCS.
Test Baseline & Test Data
tests/main/test_public_api_signature_baseline.py, tests/data/expected/main/input_model/config_class.py
Updated baseline function/parser signatures to accept jsonschema_version and openapi_version parameters; added TypeAlias definitions and GenerateConfig fields in test data.
Test Coverage
tests/main/jsonschema/test_main_jsonschema.py
Added TestSchemaVersionDetection class with 11 test methods covering detect_jsonschema_version and detect_openapi_version; added TestSchemaVersionExceptions class with tests for UnsupportedVersionError and SchemaValidationError (note: tests appear duplicated in file).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • PR #2832: Updates public API signature baseline tests that directly correspond to the new jsonschema_version and openapi_version keyword-only parameters added to generate() and Parser in this PR.
  • PR #2878: Refactors generate() and Parser to use config objects directly, overlapping with this PR's threading of version configuration fields through the call stack.
  • PR #2877: Refactors parser constructors to accept \*\*options: Unpack[ParserConfigDict], directly impacted by this PR's addition of version fields to ParserConfigDict.

Suggested labels

safe-to-fix

Suggested reviewers

  • ilovelinux

Poem

🐰 A detective's delight in schema so bright,
JsonSchema versions now caught in our sight,
OpenAPI tales from Swagger to three-point-one,
Auto-detection makes versioning fun!
With exceptions in place, our code won't break stride,
Version clarity's our feature to guide. 🔍✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR implements CLI options for schema version selection but does not address the core objective of issue #1592, which requires comprehensive documentation of supported input formats and versions. Add documentation detailing support status, known limitations, and workarounds for JsonSchema drafts (Draft 7, 2019-09, 2020-12) and OpenAPI versions (3.0, 3.1) as specified in issue #1592.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly and specifically describes the main change: adding two new CLI options for schema version detection.
Out of Scope Changes check ✅ Passed The changes are focused on adding CLI options and supporting infrastructure (enums, exception classes, configuration fields) which align with the stated PR objective.
Docstring Coverage ✅ Passed Docstring coverage is 91.30% which is sufficient. The required threshold is 80.00%.

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 and usage tips.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 4, 2026

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

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 4, 2026

CodSpeed Performance Report

Merging #2917 will improve performance by 18.93%

Comparing feature/schema-version-options (b5ca474) with main (960f7f9)

⚠️ 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.

Summary

⚡ 11 improvements
⏩ 98 skipped1

Benchmarks breakdown

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

  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.

Copy link

@coderabbitai coderabbitai bot left a 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_version and detect_openapi_version line 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 swagger key without validating its value. OpenAPI 2.0 specs should have swagger: "2.0". While rare, a malformed spec with swagger: null or 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

📥 Commits

Reviewing files that changed from the base of the PR and between 960f7f9 and 0b417f4.

📒 Files selected for processing (11)
  • src/datamodel_code_generator/__init__.py
  • src/datamodel_code_generator/__main__.py
  • src/datamodel_code_generator/_types/generate_config_dict.py
  • src/datamodel_code_generator/_types/parser_config_dicts.py
  • src/datamodel_code_generator/arguments.py
  • src/datamodel_code_generator/cli_options.py
  • src/datamodel_code_generator/config.py
  • src/datamodel_code_generator/enums.py
  • tests/data/expected/main/input_model/config_class.py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/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 .Auto defaults, ensuring backward compatibility. The placement after input_file_type is 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_META entries 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 JsonSchemaVersion and OpenAPIVersion type aliases correctly mirror the enum values from src/datamodel_code_generator/enums.py. The fields are appropriately marked as NotRequired in the GenerateConfig TypedDict, 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 JsonSchemaVersion and OpenAPIVersion are 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-version and --openapi-version options are properly configured:

  • Placed in the appropriate base_options group
  • 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 JsonSchemaVersion and OpenAPIVersion are 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_version and openapi_version fields are appropriately added to GenerateConfig:

  • Positioned logically with other input-related configuration (after input_file_type)
  • Default values use .Auto enum 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_version and openapi_version fields in ParserConfig are consistent with the GenerateConfig additions:

  • 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 Auto defaults, and are logically placed alongside the input_file_type field.


945-946: The version parameters are properly propagated. Both GenerateConfig and ParserConfig define jsonschema_version and openapi_version fields, and the _create_parser_config() function explicitly filters and includes any fields from GenerateConfig that exist in the target parser config class. Since all parser configs (JSONSchemaParserConfig, OpenAPIParserConfig, GraphQLParserConfig) inherit from ParserConfig, 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.enums and maintain alphabetical ordering.


293-306: LGTM!

The detection logic correctly parses common JSON Schema draft identifiers from $schema URLs. 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 Error class. The classes appropriately store input parameters as instance attributes for programmatic access, and the VersionMismatchWarning as a UserWarning subclass 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
Copy link

codecov bot commented Jan 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (1b931d5) to head (b5ca474).
⚠️ Report is 1 commits behind head on main.

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     
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 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 force-pushed the feature/schema-version-options branch from 0b417f4 to 27f10b4 Compare January 4, 2026 06:12
@koxudaxi koxudaxi closed this Jan 4, 2026
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.

Comprehensive Documentation of Supported Input Formats

2 participants