Merge pull request #125 from diogobaeder/master

Fix #124: Checking "additionalProperties" in "oneOf" items.
This commit is contained in:
A 2019-03-26 13:35:16 +00:00 committed by GitHub
commit f274836c4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 2 deletions

View file

@ -161,6 +161,13 @@ class Schema(object):
return defaultdict(lambda: lambda x: x, mapping) return defaultdict(lambda: lambda x: x, mapping)
def are_additional_properties_allowed(self, one_of_schema=None):
return (
(self.additional_properties is not False) and
(one_of_schema is None or
one_of_schema.additional_properties is not False)
)
def cast(self, value, custom_formatters=None, strict=True): def cast(self, value, custom_formatters=None, strict=True):
"""Cast value to schema type""" """Cast value to schema type"""
if value is None: if value is None:
@ -311,7 +318,9 @@ class Schema(object):
value_props_names = value.keys() value_props_names = value.keys()
extra_props = set(value_props_names) - set(all_props_names) extra_props = set(value_props_names) - set(all_props_names)
if extra_props and self.additional_properties is False: extra_props_allowed = self.are_additional_properties_allowed(
one_of_schema)
if extra_props and not extra_props_allowed:
raise UndefinedSchemaProperty(extra_props) raise UndefinedSchemaProperty(extra_props)
properties = {} properties = {}
@ -543,7 +552,9 @@ class Schema(object):
value_props_names = value.keys() value_props_names = value.keys()
extra_props = set(value_props_names) - set(all_props_names) extra_props = set(value_props_names) - set(all_props_names)
if extra_props and self.additional_properties is False: extra_props_allowed = self.are_additional_properties_allowed(
one_of_schema)
if extra_props and not extra_props_allowed:
raise UndefinedSchemaProperty(extra_props) raise UndefinedSchemaProperty(extra_props)
if self.additional_properties is not True: if self.additional_properties is not True:

View file

@ -711,6 +711,39 @@ class TestSchemaValidate(object):
with pytest.raises(NoOneOfSchema): with pytest.raises(NoOneOfSchema):
schema.validate(value) schema.validate(value)
@pytest.mark.parametrize('value', [
Model({
'foo': u("FOO"),
}),
Model({
'foo': u("FOO"),
'bar': u("BAR"),
}),
])
def test_unambiguous_one_of(self, value):
one_of = [
Schema(
'object',
properties={
'foo': Schema('string'),
},
additional_properties=False,
required=['foo'],
),
Schema(
'object',
properties={
'foo': Schema('string'),
'bar': Schema('string'),
},
additional_properties=False,
required=['foo', 'bar'],
),
]
schema = Schema('object', one_of=one_of)
schema.validate(value)
@pytest.mark.parametrize('value', [Model(), ]) @pytest.mark.parametrize('value', [Model(), ])
def test_object_default_property(self, value): def test_object_default_property(self, value):
schema = Schema('object', default='value1') schema = Schema('object', default='value1')