From 322658432f46230370ff1b8b320664ccb5f0c07d Mon Sep 17 00:00:00 2001 From: Jude188 Date: Fri, 17 Jan 2020 17:50:41 +0000 Subject: [PATCH] Make sure anyOf schemas are converted --- singer/schema.py | 7 ++++++- tests/test_schema.py | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/singer/schema.py b/singer/schema.py index b4da4ac..7f87a98 100644 --- a/singer/schema.py +++ b/singer/schema.py @@ -18,7 +18,6 @@ 'format', 'type', 'additionalProperties', - 'anyOf', 'patternProperties', ] @@ -82,6 +81,9 @@ def to_dict(self): if self.items is not None: result['items'] = self.items.to_dict() # pylint: disable=no-member + if self.anyOf is not None: + result['anyOf'] = [_.to_dict() for _ in self.anyOf] # pylint: disable=no-member + for key in STANDARD_KEYS: if self.__dict__.get(key) is not None: result[key] = self.__dict__[key] @@ -97,6 +99,7 @@ def from_dict(cls, data, **schema_defaults): kwargs = schema_defaults.copy() properties = data.get('properties') items = data.get('items') + anyOf = data.get('anyOf') if properties is not None: kwargs['properties'] = { @@ -105,6 +108,8 @@ def from_dict(cls, data, **schema_defaults): } if items is not None: kwargs['items'] = Schema.from_dict(items, **schema_defaults) + if anyOf is not None: + kwargs['anyOf'] = [Schema.from_dict(_, **schema_defaults) for _ in anyOf] for key in STANDARD_KEYS: if key in data: kwargs[key] = data[key] diff --git a/tests/test_schema.py b/tests/test_schema.py index fa28bac..d3dee62 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -30,6 +30,24 @@ class TestSchema(unittest.TestCase): 'additionalProperties': True, } + anyOf_dict = { + 'anyOf': [ + { + 'type': 'object', + 'properties': { + 'a_string': string_dict, + 'an_array': array_dict + }, + 'inclusion': 'whatever', + 'additionalProperties': True, + }, + { + 'type': 'integer', + 'maximum': 1000000 + } + ] + } + # Schema object forms of the same schemas as above string_obj = Schema(type='string', maxLength=32) @@ -43,6 +61,8 @@ class TestSchema(unittest.TestCase): inclusion='whatever', additionalProperties=True) + anyOf_obj = Schema(anyOf=[object_obj, integer_obj]) + def test_string_to_dict(self): self.assertEquals(self.string_dict, self.string_obj.to_dict()) @@ -55,6 +75,9 @@ def test_array_to_dict(self): def test_object_to_dict(self): self.assertEquals(self.object_dict, self.object_obj.to_dict()) + def test_anyOf_to_dict(self): + self.assertEquals(self.anyOf_dict, self.anyOf_obj.to_dict()) + def test_string_from_dict(self): self.assertEquals(self.string_obj, Schema.from_dict(self.string_dict)) @@ -67,6 +90,9 @@ def test_array_from_dict(self): def test_object_from_dict(self): self.assertEquals(self.object_obj, Schema.from_dict(self.object_dict)) + def test_anyOf_from_dict(self): + self.assertEquals(self.anyOf_obj, Schema.from_dict(self.anyOf_dict)) + def test_repr_atomic(self): self.assertEquals(self.string_obj, eval(repr(self.string_obj)))