Skip to content

Commit

Permalink
Make sure anyOf schemas are converted
Browse files Browse the repository at this point in the history
  • Loading branch information
judahrand committed Jan 19, 2020
1 parent 6c6c773 commit 3226584
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion singer/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
'format',
'type',
'additionalProperties',
'anyOf',
'patternProperties',
]

Expand Down Expand Up @@ -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]
Expand All @@ -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'] = {
Expand All @@ -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]
Expand Down
26 changes: 26 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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())

Expand All @@ -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))

Expand All @@ -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)))

Expand Down

0 comments on commit 3226584

Please sign in to comment.