Skip to content

Commit 8c7a9b2

Browse files
authored
fix: handle type definitions from grand(grand...) parent schemas (#2861)
update related tests for Entity and Thing schemas
1 parent 63d8804 commit 8c7a9b2

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

src/datamodel_code_generator/parser/jsonschema.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,19 +1823,23 @@ def _get_inherited_field_type( # noqa: PLR0912
18231823
except Exception: # pragma: no cover # noqa: BLE001, S112
18241824
continue
18251825

1826+
result: DataType | None = None
18261827
if parent_schema.properties:
18271828
prop_schema = parent_schema.properties.get(prop_name)
18281829
if isinstance(prop_schema, JsonSchemaObject):
18291830
result = self._build_lightweight_type(prop_schema)
1830-
if result is not None:
1831-
return result
1831+
# In case of a missing type, continue searching up the inheritance chain
1832+
if result is not None and not (result.type == ANY or self._is_list_with_any_item_type(result)):
1833+
return result
18321834

1835+
parent_result: DataType | None = None
18331836
if parent_schema.allOf:
18341837
grandparent_refs = [self.model_resolver.add_ref(item.ref) for item in parent_schema.allOf if item.ref]
18351838
if grandparent_refs:
1836-
result = self._get_inherited_field_type(prop_name, grandparent_refs, visited)
1837-
if result is not None:
1838-
return result
1839+
parent_result = self._get_inherited_field_type(prop_name, grandparent_refs, visited)
1840+
if parent_result is not None:
1841+
return parent_result
1842+
return result
18391843

18401844
return None
18411845

tests/data/expected/main/openapi/allof_partial_override_array_items.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
from pydantic import BaseModel
88

99

10-
class Thing(BaseModel):
10+
class Entity(BaseModel):
11+
type_list: list[str] | None = None
12+
13+
14+
class Thing(Entity):
1115
type: str | None = 'playground:Thing'
1216
type_list: list[str] | None = ['playground:Thing']
1317

tests/data/openapi/allof_partial_override_array_items.yaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
openapi: "3.0.0"
22
components:
33
schemas:
4+
Entity:
5+
type: object
6+
properties:
7+
type_list:
8+
type: array
9+
items:
10+
type: string
411
Thing:
12+
allOf:
13+
- $ref: "#/components/schemas/Entity"
514
type: object
615
properties:
716
type:
@@ -12,7 +21,7 @@ components:
1221
default:
1322
- playground:Thing
1423
items:
15-
type: string
24+
title: A type entry (default playground:Thing)
1625
Person:
1726
allOf:
1827
- $ref: "#/components/schemas/Thing"
@@ -24,4 +33,4 @@ components:
2433
default:
2534
- playground:Person
2635
items:
27-
title: A type entry
36+
title: A type entry (default playground:Person)

0 commit comments

Comments
 (0)