Skip to content

Commit 57f732b

Browse files
authored
fix: Support discriminators that map multiple keys to the same schema component BNCH-37105 (#108)
1 parent cef60fe commit 57f732b

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

openapi_python_client/parser/properties/__init__.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -432,15 +432,7 @@ def build_union_property(
432432
*, data: oai.Schema, name: str, required: bool, schemas: Schemas, parent_name: str
433433
) -> Tuple[Union[UnionProperty, PropertyError], Schemas]:
434434
sub_properties: List[Property] = []
435-
inverted_mappings = {}
436-
for k, v in (data.discriminator.mapping if data.discriminator else {}).items():
437-
class_name = Reference.from_ref(v).class_name
438-
if class_name in inverted_mappings:
439-
raise ArgumentError(
440-
f"Mapping more than one name to a class is currently not supported (class: {class_name})."
441-
)
442-
inverted_mappings[Reference.from_ref(v).class_name] = k
443-
discriminator_mappings: Dict[str, Property] = {}
435+
reference_name_to_subprop = {}
444436
for sub_prop_data in chain(data.anyOf, data.oneOf):
445437
sub_prop, schemas = property_from_data(
446438
name=name, required=required, data=sub_prop_data, schemas=schemas, parent_name=parent_name
@@ -450,9 +442,13 @@ def build_union_property(
450442

451443
sub_properties.append(sub_prop)
452444
if data.discriminator is not None:
453-
discriminated_by = inverted_mappings.get(sub_prop.reference.class_name)
454-
if discriminated_by is not None:
455-
discriminator_mappings[discriminated_by] = sub_prop
445+
reference_name_to_subprop[sub_prop.reference.class_name] = sub_prop
446+
447+
discriminator_mappings: Dict[str, Property] = {}
448+
if data.discriminator is not None:
449+
for k, v in (data.discriminator.mapping if data.discriminator else {}).items():
450+
ref_class_name = Reference.from_ref(v).class_name
451+
discriminator_mappings[k] = reference_name_to_subprop[ref_class_name]
456452

457453
default = convert_chain((prop._type_string for prop in sub_properties), data.default)
458454
return (

0 commit comments

Comments
 (0)