Skip to content

Commit e844a7d

Browse files
committed
add runtime type for wrapped records
1 parent ffa54ac commit e844a7d

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/py_avro_schema/_schemas.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ def _wrap_as_record(self, inner_schema: JSONObj, names: NamesType) -> JSONType:
320320
{"name": REF_DATA_KEY, "type": inner_schema},
321321
],
322322
}
323+
if Option.ADD_RUNTIME_TYPE_FIELD in self.options:
324+
record_schema["fields"].append({"name": RUNTIME_TYPE_KEY, "type": ["null", "string"]})
325+
323326
if self.namespace:
324327
record_schema["namespace"] = self.namespace
325328
return record_schema

tests/test_avro_schema.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,17 @@ class PyType:
8080
],
8181
}
8282
assert_schema(PyType, expected, options=pas.Option.ADD_RUNTIME_TYPE_FIELD)
83+
84+
85+
def test_add_type_field_on_wrapped_record():
86+
py_type = list[str]
87+
expected = {
88+
"type": "record",
89+
"name": "StrList",
90+
"fields": [
91+
{"name": "__id", "type": ["null", "long"], "default": None},
92+
{"name": "__data", "type": {"type": "array", "items": "string"}},
93+
{"name": "_runtime_type", "type": ["null", "string"]},
94+
],
95+
}
96+
assert_schema(py_type, expected, options=pas.Option.WRAP_INTO_RECORDS | pas.Option.ADD_RUNTIME_TYPE_FIELD)

0 commit comments

Comments
 (0)