diff --git a/datacontract/export/avro_converter.py b/datacontract/export/avro_converter.py index a9696a33..2adf8f89 100644 --- a/datacontract/export/avro_converter.py +++ b/datacontract/export/avro_converter.py @@ -4,7 +4,7 @@ def to_avro_schema(model_name, model) -> dict: - return to_avro_record(model_name, model.fields, model.description) + return to_avro_record(model_name, model.fields, model.description, model.namespace) def to_avro_schema_json(model_name, model) -> str: @@ -12,10 +12,12 @@ def to_avro_schema_json(model_name, model) -> str: return json.dumps(schema, indent=2, sort_keys=False) -def to_avro_record(name, fields, description) -> dict: +def to_avro_record(name, fields, description, namespace) -> dict: schema = {"type": "record", "name": name} if description is not None: schema["doc"] = description + if namespace is not None: + schema["namespace"] = namespace schema["fields"] = to_avro_fields(fields) return schema @@ -35,7 +37,7 @@ def to_avro_field(field, field_name): return avro_field -def to_avro_type(field: Field, field_name: str) -> str: +def to_avro_type(field: Field, field_name: str) -> str | dict: if field.type is None: return "null" if field.type in ["string", "varchar", "text"]: @@ -60,7 +62,7 @@ def to_avro_type(field: Field, field_name: str) -> str: elif field.type in ["time"]: return "long" elif field.type in ["object", "record", "struct"]: - return to_avro_record(field_name, field.fields, field.description) + return to_avro_record(field_name, field.fields, field.description, None) elif field.type in ["binary"]: return "bytes" elif field.type in ["array"]: diff --git a/tests/fixtures/kafka-avro-remote/datacontract.yaml b/tests/fixtures/kafka-avro-remote/datacontract.yaml index b740e1c0..75062f76 100644 --- a/tests/fixtures/kafka-avro-remote/datacontract.yaml +++ b/tests/fixtures/kafka-avro-remote/datacontract.yaml @@ -14,6 +14,7 @@ models: orders: type: table description: My Model + namespace: com.example.checkout fields: ordertime: type: bigint diff --git a/tests/test_export_avro.py b/tests/test_export_avro.py index 63b9a8cc..07c4bcf6 100644 --- a/tests/test_export_avro.py +++ b/tests/test_export_avro.py @@ -62,6 +62,7 @@ def test_to_avro_schema(): } ], "name": "orders", + "namespace": "com.example.checkout", "doc": "My Model", "type": "record" }