-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ingestion): properly detect optional fields in avro schemas (#2343)
Co-authored-by: thomas.larsson <[email protected]>
- Loading branch information
1 parent
4a47abf
commit 4215dcd
Showing
2 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import unittest | ||
|
||
from datahub.ingestion.extractor.schema_util import avro_schema_to_mce_fields | ||
|
||
EXAMPLE_EVENT_OPTIONAL_FIELD_VIA_UNION_TYPE = """ | ||
{ | ||
"type": "record", | ||
"name": "some.event.name", | ||
"namespace": "some.event.namespace", | ||
"fields": [ | ||
{ | ||
"name": "my.field", | ||
"type": ["null", "string"], | ||
"doc": "some.doc" | ||
} | ||
] | ||
} | ||
""" | ||
|
||
EXAMPLE_EVENT_OPTIONAL_FIELD_VIA_UNION_TYPE_NULL_ISNT_FIRST_IN_UNION = """ | ||
{ | ||
"type": "record", | ||
"name": "some.event.name", | ||
"namespace": "some.event.namespace", | ||
"fields": [ | ||
{ | ||
"name": "my.field", | ||
"type": ["string", "null"], | ||
"doc": "some.doc" | ||
} | ||
] | ||
} | ||
""" | ||
|
||
EXAMPLE_EVENT_OPTIONAL_FIELD_VIA_PRIMITIVE_TYPE = """ | ||
{ | ||
"type": "record", | ||
"name": "some.event.name", | ||
"namespace": "some.event.namespace", | ||
"fields": [ | ||
{ | ||
"name": "my.field", | ||
"type": "null", | ||
"doc": "some.doc" | ||
} | ||
] | ||
} | ||
""" | ||
|
||
|
||
class SchemaUtilTest(unittest.TestCase): | ||
def test_avro_schema_to_mce_fields_events_with_nullable_fields(self): | ||
|
||
events = [ | ||
EXAMPLE_EVENT_OPTIONAL_FIELD_VIA_UNION_TYPE, | ||
EXAMPLE_EVENT_OPTIONAL_FIELD_VIA_UNION_TYPE_NULL_ISNT_FIRST_IN_UNION, | ||
EXAMPLE_EVENT_OPTIONAL_FIELD_VIA_PRIMITIVE_TYPE, | ||
] | ||
|
||
for event in events: | ||
fields = avro_schema_to_mce_fields(event) | ||
self.assertEqual(1, len(fields)) | ||
self.assertTrue(fields[0].nullable) |