Skip to content

Commit

Permalink
fix(ingest/json-schema): convert non-string enums to strings (#8479)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-awd authored Aug 1, 2023
1 parent 547e1f4 commit 2e2a674
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,13 @@ def _field_from_primitive(
isPartOfKey=field_path.is_key_schema,
)
elif datahub_field_type in [EnumTypeClass]:
# Convert enums to string representation
schema_enums = list(map(json.dumps, schema["enum"]))
yield SchemaField(
fieldPath=field_path.expand_type("enum", schema).as_string(),
type=type_override or SchemaFieldDataTypeClass(type=EnumTypeClass()),
nativeDataType="Enum",
description=f"one of {','.join(schema['enum'])}",
description=f"One of: {', '.join(schema_enums)}",
nullable=nullable,
jsonProps=JsonSchemaTranslator._get_jsonprops_for_any_schema(
schema, required=required
Expand Down
13 changes: 13 additions & 0 deletions metadata-ingestion/tests/unit/schema/test_json_schema_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,19 @@ def test_required_field():
assert json.loads(fields[0].jsonProps or "{}")["required"] is False


def test_non_str_enums():
schema = {
"$id": "test",
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {"bar": {"description": "Mixed enum", "enum": ["baz", 1, None]}},
}

fields = list(JsonSchemaTranslator.get_fields_from_schema(schema))
expected_field_paths: List[str] = ["[version=2.0].[type=object].[type=enum].bar"]
assert_field_paths_match(fields, expected_field_paths)
assert fields[0].description == 'One of: "baz", 1, null'


def test_anyof_with_properties():
# We expect the event / timestamp fields to be included in both branches of the anyOf.

Expand Down

0 comments on commit 2e2a674

Please sign in to comment.