Merge pull request #194 from p1c2u/fix/free-form-objects-unmarshal-fix

Free-form objects unmarshal
This commit is contained in:
A 2020-02-03 21:06:17 +00:00 committed by GitHub
commit f0759b05a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View file

@ -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:

View file

@ -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