pydantic-avro 0.9.0__py3-none-any.whl → 0.9.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pydantic_avro/to_avro/types.py +48 -4
- {pydantic_avro-0.9.0.dist-info → pydantic_avro-0.9.1.dist-info}/METADATA +1 -1
- {pydantic_avro-0.9.0.dist-info → pydantic_avro-0.9.1.dist-info}/RECORD +6 -6
- {pydantic_avro-0.9.0.dist-info → pydantic_avro-0.9.1.dist-info}/LICENSE +0 -0
- {pydantic_avro-0.9.0.dist-info → pydantic_avro-0.9.1.dist-info}/WHEEL +0 -0
- {pydantic_avro-0.9.0.dist-info → pydantic_avro-0.9.1.dist-info}/entry_points.txt +0 -0
pydantic_avro/to_avro/types.py
CHANGED
|
@@ -137,11 +137,14 @@ class AvroTypeConverter:
|
|
|
137
137
|
r = field_props["allOf"][0]["$ref"]
|
|
138
138
|
if ("prefixItems" in field_props or ("minItems" in field_props and "maxItems" in field_props)) and t == "array":
|
|
139
139
|
t = "tuple"
|
|
140
|
-
|
|
141
140
|
u = field_props.get("anyOf")
|
|
141
|
+
o = field_props.get("oneOf")
|
|
142
|
+
discriminator = field_props.get("discriminator")
|
|
142
143
|
|
|
143
144
|
if u is not None:
|
|
144
|
-
return self._union_to_avro(u, avro_type_dict)
|
|
145
|
+
return self._union_to_avro(u, avro_type_dict, discriminator)
|
|
146
|
+
elif o is not None:
|
|
147
|
+
return self._union_to_avro(o, avro_type_dict, discriminator)
|
|
145
148
|
elif r is not None:
|
|
146
149
|
return self._handle_references(r, avro_type_dict)
|
|
147
150
|
elif t is None:
|
|
@@ -229,8 +232,13 @@ class AvroTypeConverter:
|
|
|
229
232
|
value_type = self._get_avro_type_dict(a)["type"]
|
|
230
233
|
return {"type": "map", "values": value_type}
|
|
231
234
|
|
|
232
|
-
def _union_to_avro(self, field_props: list, avro_type_dict: dict) -> dict:
|
|
233
|
-
"""Returns a type of a union field"""
|
|
235
|
+
def _union_to_avro(self, field_props: list, avro_type_dict: dict, discriminator: Optional[dict] = None) -> dict:
|
|
236
|
+
"""Returns a type of a union field, including discriminated unions"""
|
|
237
|
+
# Handle discriminated unions
|
|
238
|
+
if discriminator is not None:
|
|
239
|
+
return self._discriminated_union_to_avro(field_props, avro_type_dict, discriminator)
|
|
240
|
+
|
|
241
|
+
# Standard union handling (unchanged)
|
|
234
242
|
avro_type_dict["type"] = []
|
|
235
243
|
for union_element in field_props:
|
|
236
244
|
t = self._get_avro_type_dict(union_element)
|
|
@@ -277,3 +285,39 @@ class AvroTypeConverter:
|
|
|
277
285
|
tn = tn["type"]
|
|
278
286
|
avro_type_dict["type"] = {"type": "array", "items": tn}
|
|
279
287
|
return avro_type_dict
|
|
288
|
+
|
|
289
|
+
def _discriminated_union_to_avro(self, variants: list, avro_type_dict: dict, discriminator: dict) -> dict:
|
|
290
|
+
"""Handles discriminated unions with a discriminator field"""
|
|
291
|
+
discriminator_property = discriminator.get("propertyName", "type")
|
|
292
|
+
variant_types = []
|
|
293
|
+
|
|
294
|
+
# Process each variant in the union
|
|
295
|
+
for variant in variants:
|
|
296
|
+
# Get the discriminator value for this variant
|
|
297
|
+
disc_value = None
|
|
298
|
+
if "properties" in variant and discriminator_property in variant.get("properties", {}):
|
|
299
|
+
prop = variant["properties"][discriminator_property]
|
|
300
|
+
if "const" in prop:
|
|
301
|
+
disc_value = prop["const"]
|
|
302
|
+
elif "enum" in prop and len(prop["enum"]) == 1:
|
|
303
|
+
disc_value = prop["enum"][0]
|
|
304
|
+
|
|
305
|
+
# If we can't determine the discriminator value, generate a regular record
|
|
306
|
+
if disc_value is None:
|
|
307
|
+
variant_type = self._get_avro_type_dict(variant)
|
|
308
|
+
else:
|
|
309
|
+
# Create a named record for this variant
|
|
310
|
+
variant_name = f"{disc_value}Variant"
|
|
311
|
+
if "$ref" in variant:
|
|
312
|
+
ref_schema = get_definition(variant["$ref"], self.root_schema)
|
|
313
|
+
variant_fields = self.fields_to_avro_dicts(ref_schema)
|
|
314
|
+
else:
|
|
315
|
+
variant_fields = self.fields_to_avro_dicts(variant)
|
|
316
|
+
|
|
317
|
+
variant_type = {"type": {"type": "record", "name": variant_name, "fields": variant_fields}}
|
|
318
|
+
|
|
319
|
+
variant_types.append(variant_type["type"])
|
|
320
|
+
|
|
321
|
+
# Set the union of all variant types
|
|
322
|
+
avro_type_dict["type"] = variant_types
|
|
323
|
+
return avro_type_dict
|
|
@@ -9,9 +9,9 @@ pydantic_avro/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
|
9
9
|
pydantic_avro/to_avro/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
10
10
|
pydantic_avro/to_avro/base.py,sha256=Y8QOGTpXO3BwJ6arrRK-k2JniK5kLTvlDpQ0PyZojmg,1409
|
|
11
11
|
pydantic_avro/to_avro/config.py,sha256=R9HLkWiXs7YZ02te8CQt5Kf2rbsE8Fx-hjqv3tBSFJU,152
|
|
12
|
-
pydantic_avro/to_avro/types.py,sha256
|
|
13
|
-
pydantic_avro-0.9.
|
|
14
|
-
pydantic_avro-0.9.
|
|
15
|
-
pydantic_avro-0.9.
|
|
16
|
-
pydantic_avro-0.9.
|
|
17
|
-
pydantic_avro-0.9.
|
|
12
|
+
pydantic_avro/to_avro/types.py,sha256=-qemQC_vs-xy7VJDTQ1y1k7K1J6Qv3pTLILqp17MO2A,12731
|
|
13
|
+
pydantic_avro-0.9.1.dist-info/entry_points.txt,sha256=gwHiQfbGLO8Np2sa1bZ_bpxU7sEufx6IachViBE_Fnw,66
|
|
14
|
+
pydantic_avro-0.9.1.dist-info/LICENSE,sha256=gBlYCG1yxb0vGlsmek0lMPVOK5YDxQope4F54jzeqoY,1069
|
|
15
|
+
pydantic_avro-0.9.1.dist-info/WHEEL,sha256=vxFmldFsRN_Hx10GDvsdv1wroKq8r5Lzvjp6GZ4OO8c,88
|
|
16
|
+
pydantic_avro-0.9.1.dist-info/METADATA,sha256=xNTEqdyWeKcmYCG5xabukyW4fFQKY05EZqpR_rx0qEQ,2945
|
|
17
|
+
pydantic_avro-0.9.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|