diff --git a/src/datamodel_code_generator/parser/jsonschema.py b/src/datamodel_code_generator/parser/jsonschema.py index 19395294c..63fb0ac80 100644 --- a/src/datamodel_code_generator/parser/jsonschema.py +++ b/src/datamodel_code_generator/parser/jsonschema.py @@ -1834,19 +1834,23 @@ def _get_inherited_field_type( # noqa: PLR0912 except Exception: # pragma: no cover # noqa: BLE001, S112 continue + result: DataType | None = None if parent_schema.properties: prop_schema = parent_schema.properties.get(prop_name) if isinstance(prop_schema, JsonSchemaObject): result = self._build_lightweight_type(prop_schema) - if result is not None: - return result + # In case of a missing type, continue searching up the inheritance chain + if result is not None and not (result.type == ANY or self._is_list_with_any_item_type(result)): + return result + parent_result: DataType | None = None if parent_schema.allOf: grandparent_refs = [self.model_resolver.add_ref(item.ref) for item in parent_schema.allOf if item.ref] if grandparent_refs: - result = self._get_inherited_field_type(prop_name, grandparent_refs, visited) - if result is not None: - return result + parent_result = self._get_inherited_field_type(prop_name, grandparent_refs, visited) + if parent_result is not None: + return parent_result + return result return None diff --git a/tests/data/expected/main/openapi/allof_partial_override_array_items.py b/tests/data/expected/main/openapi/allof_partial_override_array_items.py index 39eae6248..07c21b2c6 100644 --- a/tests/data/expected/main/openapi/allof_partial_override_array_items.py +++ b/tests/data/expected/main/openapi/allof_partial_override_array_items.py @@ -7,7 +7,11 @@ from pydantic import BaseModel -class Thing(BaseModel): +class Entity(BaseModel): + type_list: list[str] | None = None + + +class Thing(Entity): type: str | None = 'playground:Thing' type_list: list[str] | None = ['playground:Thing'] diff --git a/tests/data/openapi/allof_partial_override_array_items.yaml b/tests/data/openapi/allof_partial_override_array_items.yaml index c26e3e0c3..fd3832aa9 100644 --- a/tests/data/openapi/allof_partial_override_array_items.yaml +++ b/tests/data/openapi/allof_partial_override_array_items.yaml @@ -1,7 +1,16 @@ openapi: "3.0.0" components: schemas: + Entity: + type: object + properties: + type_list: + type: array + items: + type: string Thing: + allOf: + - $ref: "#/components/schemas/Entity" type: object properties: type: @@ -12,7 +21,7 @@ components: default: - playground:Thing items: - type: string + title: A type entry (default playground:Thing) Person: allOf: - $ref: "#/components/schemas/Thing" @@ -24,4 +33,4 @@ components: default: - playground:Person items: - title: A type entry + title: A type entry (default playground:Person)