Skip to content

Commit 9071de0

Browse files
committed
handle a case where there's multiple values mapped to same type
1 parent de1ddf3 commit 9071de0

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

end_to_end_tests/baseline_openapi_3.0.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2823,7 +2823,8 @@
28232823
"propertyName": "modelType",
28242824
"mapping": {
28252825
"type1": "#/components/schemas/ADiscriminatedUnionType1",
2826-
"type2": "#/components/schemas/ADiscriminatedUnionType2"
2826+
"type2": "#/components/schemas/ADiscriminatedUnionType2",
2827+
"type2-another-value": "#/components/schemas/ADiscriminatedUnionType2"
28272828
}
28282829
},
28292830
"oneOf": [

end_to_end_tests/baseline_openapi_3.1.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2817,7 +2817,8 @@ info:
28172817
"propertyName": "modelType",
28182818
"mapping": {
28192819
"type1": "#/components/schemas/ADiscriminatedUnionType1",
2820-
"type2": "#/components/schemas/ADiscriminatedUnionType2"
2820+
"type2": "#/components/schemas/ADiscriminatedUnionType2",
2821+
"type2-another-value": "#/components/schemas/ADiscriminatedUnionType2"
28212822
}
28222823
},
28232824
"oneOf": [

end_to_end_tests/golden-record/my_test_api_client/models/model_with_discriminated_union.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,17 @@ def _parse_2(data: object) -> ADiscriminatedUnionType2:
7878

7979
return componentsschemas_a_discriminated_union_type_1
8080

81+
def _parse_3(data: object) -> ADiscriminatedUnionType2:
82+
if not isinstance(data, dict):
83+
raise TypeError()
84+
componentsschemas_a_discriminated_union_type_1 = ADiscriminatedUnionType2.from_dict(data)
85+
86+
return componentsschemas_a_discriminated_union_type_1
87+
8188
_discriminator_mapping = {
8289
"type1": _parse_1,
8390
"type2": _parse_2,
91+
"type2-another-value": _parse_3,
8492
}
8593
if _parse_fn := _discriminator_mapping.get(_discriminator_value):
8694
return cast(

openapi_python_client/parser/properties/union.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ def _get_model_name(model: ModelProperty) -> str | None:
308308
detail=f'Discriminator mapping referred to "{name}" which is not one of the schema variants',
309309
)
310310
mapping[discriminator_value] = mapped_model
311-
unspecified_models.remove(mapped_model)
311+
if mapped_model in unspecified_models:
312+
# could've already been removed if more than one value is mapped to the same model
313+
unspecified_models.remove(mapped_model)
312314
for model in unspecified_models:
313315
if name := _get_model_name(model):
314316
mapping[name] = model

0 commit comments

Comments
 (0)