Skip to content

Commit 47ace37

Browse files
committed
model - discriminated unions with parent properties
apply parent properties first, so children can overwrite the discriminator with their Literal value
1 parent 142b64a commit 47ace37

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

aiopenapi3/model.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ def from_schema_type(
155155
else:
156156
classinfo.root = _t
157157
elif type == "object":
158+
# this is a anyOf/oneOf - the parent may have properties which will collide with __root__
159+
# so - add the parent properties to this model
160+
if extra:
161+
Model.annotationsof(extra, discriminators, schemanames, classinfo)
162+
Model.fieldof(extra, classinfo)
163+
158164
if hasattr(schema, "anyOf") and schema.anyOf:
159165
assert all(schema.anyOf)
160166
t = tuple(
@@ -229,11 +235,6 @@ def get_patternProperties(self_):
229235
Model.annotationsof(i, discriminators, schemanames, classinfo, fwdref=True)
230236
Model.fieldof(i, classinfo)
231237

232-
# this is a anyOf/oneOf - the parent may have properties which will collide with __root__
233-
# so - add the parent properties to this model
234-
if extra:
235-
Model.annotationsof(extra, discriminators, schemanames, classinfo)
236-
Model.fieldof(extra, classinfo)
237238
elif type == "array":
238239
classinfo.root = Model.typeof(schema, _type="array")
239240

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ def with_schema_discriminated_union_warning(openapi_version):
307307
yield _get_parsed_yaml("schema-discriminated-union-warning.yaml", openapi_version)
308308

309309

310+
@pytest.fixture
311+
def with_schema_discriminated_union_merge(openapi_version):
312+
yield _get_parsed_yaml("schema-discriminated-union-merge.yaml", openapi_version)
313+
314+
310315
@pytest.fixture
311316
def with_schema_discriminated_union_discriminator_name(openapi_version):
312317
yield _get_parsed_yaml("schema-discriminated-union-discriminator-name.yaml", openapi_version)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
openapi: "3.1.0"
2+
info:
3+
version: 1.0.0
4+
title: enum test
5+
6+
components:
7+
schemas:
8+
CustomContextVariable:
9+
additionalProperties: false
10+
discriminator:
11+
mapping:
12+
user: '#/components/schemas/UserContextVariable'
13+
propertyName: type
14+
oneOf:
15+
- $ref: '#/components/schemas/UserContextVariable'
16+
properties:
17+
type:
18+
description: Type of custom context variable.
19+
type: string
20+
required:
21+
- type
22+
type: object
23+
UserContextVariable:
24+
description: A [user](https://developer.atlassian.com/cloud/jira/platform/jira-expressions-type-reference#user)
25+
specified as an Atlassian account ID.
26+
properties:
27+
accountId:
28+
description: The account ID of the user.
29+
type: string
30+
type:
31+
description: Type of custom context variable.
32+
type: string
33+
required:
34+
- accountId
35+
- type
36+
type: object

tests/schema_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,15 @@ def test_schema_discriminated_union_warnings(with_schema_discriminated_union_war
346346
api = OpenAPI("/", s)
347347

348348

349+
def test_schema_discriminated_union_merge(with_schema_discriminated_union_merge, openapi_version):
350+
from aiopenapi3.errors import DiscriminatorWarning
351+
352+
with pytest.warns(
353+
DiscriminatorWarning, match=r"Discriminated Union member \S+ without const/enum key property \S+"
354+
):
355+
api = OpenAPI("/", with_schema_discriminated_union_merge)
356+
357+
349358
def test_schema_discriminated_union_deep(with_schema_discriminated_union_deep):
350359
api = OpenAPI("/", with_schema_discriminated_union_deep)
351360
Dog = api.components.schemas["Dog"].get_type()

0 commit comments

Comments
 (0)