Skip to content

Commit acc20fa

Browse files
committed
schema - extending a discriminated union yields new classes
1 parent 279c8d6 commit acc20fa

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/aiopenapi3/openapi.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,16 @@ def _init_schema_types(self, only_required: bool) -> None:
627627
for j in b._model_types:
628628
types[j.__name__] = j
629629

630+
# as previous .get_type() may have created new models, we need to reindex
631+
for name, schema in list(types.items()):
632+
if not is_basemodel(schema):
633+
continue
634+
thes = byname.get(name, None)
635+
if thes is not None:
636+
for v in byid[id(thes)]._model_types:
637+
if v.__name__ not in types:
638+
types[v.__name__] = v
639+
630640
# print(f"{len(types)}")
631641
for name, schema in types.items():
632642
if not is_basemodel(schema):
@@ -637,6 +647,7 @@ def _init_schema_types(self, only_required: bool) -> None:
637647
thes = byname.get(name, None)
638648
if thes is not None:
639649
for v in byid[id(thes)]._model_types:
650+
assert v.__name__ in types, v.__name__
640651
v.model_rebuild(_types_namespace={"__types": types})
641652
except Exception as e:
642653
raise e

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,3 +566,8 @@ def with_schema_anyOf():
566566
@pytest.fixture
567567
def with_schema_title_name_collision():
568568
yield _get_parsed_yaml("schema-title-name-collision.yaml")
569+
570+
571+
@pytest.fixture
572+
def with_schema_discriminated_union_extends():
573+
yield _get_parsed_yaml("schema-discriminated-union-extends.yaml")
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
openapi: 3.1.0
2+
3+
info:
4+
title: title collision
5+
version: 2.2.7
6+
7+
servers:
8+
- url: /
9+
10+
paths: {}
11+
12+
13+
components:
14+
schemas:
15+
A:
16+
type: object
17+
additionalProperties: false
18+
properties:
19+
a:
20+
type: integer
21+
B:
22+
type: object
23+
additionalProperties: false
24+
properties:
25+
b:
26+
type: integer
27+
28+
AB:
29+
type: object
30+
additionalProperties: false
31+
properties:
32+
type:
33+
type: string
34+
const: ""
35+
oneOf:
36+
- $ref: "#/components/schemas/A"
37+
- $ref: "#/components/schemas/B"
38+
discriminator:
39+
propertyName: type
40+
mapping:
41+
A: "#/components/schemas/A"
42+
B: "#/components/schemas/B"

0 commit comments

Comments
 (0)