From 75643da1af3813881b80d6d706648862fc0baea4 Mon Sep 17 00:00:00 2001 From: p1c2u Date: Mon, 3 Feb 2020 20:51:15 +0000 Subject: [PATCH] Free-form objects unmarshal --- openapi_core/unmarshalling/schemas/unmarshallers.py | 7 ++++++- tests/unit/unmarshalling/test_unmarshal.py | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/openapi_core/unmarshalling/schemas/unmarshallers.py b/openapi_core/unmarshalling/schemas/unmarshallers.py index 1a00026..c275a39 100644 --- a/openapi_core/unmarshalling/schemas/unmarshallers.py +++ b/openapi_core/unmarshalling/schemas/unmarshallers.py @@ -6,6 +6,7 @@ from six import iteritems from openapi_core.extensions.models.factories import ModelFactory from openapi_core.schema.schemas.enums import SchemaFormat, SchemaType +from openapi_core.schema.schemas.models import Schema from openapi_core.schema.schemas.types import NoValue from openapi_core.schema_validator._types import ( is_array, is_bool, is_integer, @@ -194,11 +195,15 @@ class ObjectUnmarshaller(ComplexUnmarshaller): extra_props = set(value_props_names) - set(all_props_names) properties = {} - if self.schema.additional_properties is not True: + if isinstance(self.schema.additional_properties, Schema): for prop_name in extra_props: prop_value = value[prop_name] properties[prop_name] = self.unmarshallers_factory.create( self.schema.additional_properties)(prop_value) + elif self.schema.additional_properties is True: + for prop_name in extra_props: + prop_value = value[prop_name] + properties[prop_name] = prop_value for prop_name, prop in iteritems(all_props): try: diff --git a/tests/unit/unmarshalling/test_unmarshal.py b/tests/unit/unmarshalling/test_unmarshal.py index 7c88c95..ae6c88c 100644 --- a/tests/unit/unmarshalling/test_unmarshal.py +++ b/tests/unit/unmarshalling/test_unmarshal.py @@ -416,3 +416,16 @@ class TestSchemaUnmarshallerCall(object): def test_schema_any(self, unmarshaller_factory): schema = Schema() assert unmarshaller_factory(schema)('string') == 'string' + + @pytest.mark.parametrize('value', [ + {'additional': 1}, + {'foo': 'bar', 'bar': 'foo'}, + {'additional': {'bar': 1}}, + ]) + @pytest.mark.parametrize('additional_properties', [True, Schema()]) + def test_schema_free_form_object( + self, value, additional_properties, unmarshaller_factory): + schema = Schema('object', additional_properties=additional_properties) + + result = unmarshaller_factory(schema)(value) + assert result == value