Skip to content

Commit 2e2a674

Browse files
authored
fix(ingest/json-schema): convert non-string enums to strings (#8479)
1 parent 547e1f4 commit 2e2a674

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

metadata-ingestion/src/datahub/ingestion/extractor/json_schema_util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,13 @@ def _field_from_primitive(
254254
isPartOfKey=field_path.is_key_schema,
255255
)
256256
elif datahub_field_type in [EnumTypeClass]:
257+
# Convert enums to string representation
258+
schema_enums = list(map(json.dumps, schema["enum"]))
257259
yield SchemaField(
258260
fieldPath=field_path.expand_type("enum", schema).as_string(),
259261
type=type_override or SchemaFieldDataTypeClass(type=EnumTypeClass()),
260262
nativeDataType="Enum",
261-
description=f"one of {','.join(schema['enum'])}",
263+
description=f"One of: {', '.join(schema_enums)}",
262264
nullable=nullable,
263265
jsonProps=JsonSchemaTranslator._get_jsonprops_for_any_schema(
264266
schema, required=required

metadata-ingestion/tests/unit/schema/test_json_schema_util.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,19 @@ def test_required_field():
712712
assert json.loads(fields[0].jsonProps or "{}")["required"] is False
713713

714714

715+
def test_non_str_enums():
716+
schema = {
717+
"$id": "test",
718+
"$schema": "http://json-schema.org/draft-06/schema#",
719+
"properties": {"bar": {"description": "Mixed enum", "enum": ["baz", 1, None]}},
720+
}
721+
722+
fields = list(JsonSchemaTranslator.get_fields_from_schema(schema))
723+
expected_field_paths: List[str] = ["[version=2.0].[type=object].[type=enum].bar"]
724+
assert_field_paths_match(fields, expected_field_paths)
725+
assert fields[0].description == 'One of: "baz", 1, null'
726+
727+
715728
def test_anyof_with_properties():
716729
# We expect the event / timestamp fields to be included in both branches of the anyOf.
717730

0 commit comments

Comments
 (0)