Skip to content

Commit 15df8cb

Browse files
author
Sigurd Spieckermann
committed
Unmarshal "allOf" with "oneOf/anyOf/allOf" sub-schemas
1 parent 6c98890 commit 15df8cb

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

src/oas/schema/unmarshalers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ def _unmarshal(self, instance, schema):
4242
if 'allOf' in schema:
4343
sub_schemas = iter(schema['allOf'])
4444
result = self._unmarshal(instance, next(sub_schemas))
45-
# If the first sub-schema of ``allOf`` specifies an object, also
46-
# unmarshal the remaining sub-schemas and merge the results.
47-
if schema['allOf'][0].get('type', 'object') == 'object':
45+
# If ``result``` is a dict, then the sub-schema specify objects, so
46+
# also unmarshal the remaining sub-schemas and merge the results.
47+
if isinstance(result, dict):
4848
for sub_schema in sub_schemas:
4949
for k, v in iteritems(
5050
self._unmarshal(instance, sub_schema)

tests/schema/test_unmarshalers.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,36 @@ def test_unmarshal_all_of_primitive_string_enum():
245245
assert unmarshaled == instance
246246

247247

248+
@pytest.mark.parametrize('schema_type', ['oneOf', 'anyOf'])
249+
def test_unmarshal_all_of_one_of_or_any_of(schema_type):
250+
schema = {
251+
'allOf': [
252+
{schema_type: [{'type': 'string'}, {'type': 'number'}]},
253+
{'type': 'string'},
254+
]
255+
}
256+
instance = 'a'
257+
unmarshaled = SchemaUnmarshaler().unmarshal(instance, schema)
258+
assert unmarshaled == instance
259+
260+
261+
def test_unmarshal_all_of_all_of():
262+
schema = {
263+
'allOf': [
264+
{
265+
'allOf': [
266+
{'type': 'string'},
267+
{'type': 'string', 'enum': ['a', 'b']},
268+
]
269+
},
270+
{'type': 'string', 'enum': ['a']},
271+
]
272+
}
273+
instance = 'a'
274+
unmarshaled = SchemaUnmarshaler().unmarshal(instance, schema)
275+
assert unmarshaled == instance
276+
277+
248278
@pytest.mark.parametrize('schema_type', ['oneOf', 'anyOf'])
249279
def test_unmarshal_one_of_or_any_of(schema_type):
250280
schema = {

0 commit comments

Comments
 (0)