github koxudaxi/datamodel-code-generator 0.53.0

12 hours ago

Breaking Changes

Custom Template Update Required

  • Parser subclass signature change - The Parser base class now requires two generic type parameters: Parser[ParserConfigT, SchemaFeaturesT] instead of just Parser[ParserConfigT]. Custom parser subclasses must be updated to include the second type parameter. (#2929)
    # Before
    class MyCustomParser(Parser["MyParserConfig"]):
        ...
    # After
    class MyCustomParser(Parser["MyParserConfig", "JsonSchemaFeatures"]):
        ...
  • New abstract schema_features property required - Custom parser subclasses must now implement the schema_features abstract property that returns a JsonSchemaFeatures (or subclass) instance. (#2929)
    from functools import cached_property
    from datamodel_code_generator.parser.schema_version import JsonSchemaFeatures
    from datamodel_code_generator.enums import JsonSchemaVersion
    class MyCustomParser(Parser["MyParserConfig", "JsonSchemaFeatures"]):
        @cached_property
        def schema_features(self) -> JsonSchemaFeatures:
            return JsonSchemaFeatures.from_version(JsonSchemaVersion.Draft202012)
  • Parser _create_default_config refactored to use class variable - Subclasses that override _create_default_config should now set the _config_class_name class variable instead. The base implementation uses this variable to dynamically instantiate the correct config class. (#2929)
    # Before
    @classmethod
    def _create_default_config(cls, options: MyConfigDict) -> MyParserConfig:
        # custom implementation...
    # After
    _config_class_name: ClassVar[str] = "MyParserConfig"
    # No need to override _create_default_config if using standard config creation
  • Template condition for default values changed - If you use custom Jinja2 templates based on BaseModel_root.jinja2 or RootModel.jinja2, the condition for including default values has changed from field.required to (field.required and not field.has_default). Update your custom templates if you override these files. (#2960)

Code Generation Changes

  • RootModel default values now included in generated code - Previously, default values defined in JSON Schema or OpenAPI specifications for root models were not being applied to the generated Pydantic code. Now these defaults are correctly included. For example, a schema defining a root model with default: 1 will generate __root__: int = 1 (Pydantic v1) or root: int = 1 (Pydantic v2) instead of just __root__: int or root: int. This may affect code that relied on the previous behavior where RootModel fields had no default values. (#2960)
  • Required fields with list defaults now use default_factory - Previously, required fields with list-type defaults (like __root__: list[ID] = ['abc', 'efg']) were generated with direct list assignments. Now they correctly use Field(default_factory=lambda: ...) which follows Python best practices for mutable defaults. This changes the structure of generated code for root models and similar patterns with list defaults. (#2958)
    Before:
    class Family(BaseModel):
        __root__: list[ID] = ['abc', 'efg']
    After:
    class Family(BaseModel):
        __root__: list[ID] = Field(
            default_factory=lambda: [ID.parse_obj(v) for v in ['abc', 'efg']]
        )

What's Changed

  • Separate pytest-benchmark into dedicated benchmark dependency group by @koxudaxi in #2937
  • Support ClassVar for Pydantic v2 by @ubaumann in #2920
  • Add schema version detection and feature flags by @koxudaxi in #2924
  • Fix MRO ordering for multiple inheritance in GraphQL and JSON Schema/OpenAPI by @koxudaxi in #2941
  • Add schema_features property to parsers for version detection by @koxudaxi in #2929
  • Fix $ref handling in request-response mode for readOnly/writeOnly schemas by @koxudaxi in #2942
  • Ensure codecov upload runs even when coverage check fails by @koxudaxi in #2944
  • Add FeatureMetadata to schema feature classes for doc generation by @koxudaxi in #2945
  • Add schema-docs auto-generation with pre-commit and CI by @koxudaxi in #2949
  • Add comprehensive feature metadata to schema version dataclasses by @koxudaxi in #2946
  • fix: move UnionMode import outside TYPE_CHECKING for Pydantic runtime… by @phil65 in #2950
  • Fix IndexError when using --reuse-scope=tree with single file output by @koxudaxi in #2954
  • Add --use-closed-typed-dict option to control PEP 728 TypedDict generation by @koxudaxi in #2956
  • Fix RootModel default value not being applied by @koxudaxi in #2960
  • Fix required list fields ignoring empty default values by @koxudaxi in #2958
  • Add GenerateConfig lazy import from top-level module by @koxudaxi in #2961
  • Fix allOf array property merging to preserve child $ref by @koxudaxi in #2962
  • Fix array RootModel default value handling in parser by @koxudaxi in #2963
  • Fix bug in handling of graphql empty list defaults by @rpmcginty in #2948

New Contributors

Full Changelog: 0.52.2...0.53.0

Don't miss a new datamodel-code-generator release

NewReleases is sending notifications on new releases.