Skip to content

Fix #124: Checking "additionalProperties" in "oneOf" items. #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions openapi_core/schema/schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ def get_cast_mapping(self, custom_formatters=None, strict=True):

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):
"""Cast value to schema type"""
if value is None:
Expand Down Expand Up @@ -311,7 +318,9 @@ def _unmarshal_properties(self, value, one_of_schema=None,

value_props_names = value.keys()
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)

properties = {}
Expand Down Expand Up @@ -543,7 +552,9 @@ def _validate_properties(self, value, one_of_schema=None,

value_props_names = value.keys()
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)

if self.additional_properties is not True:
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/schema/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,39 @@ def test_object_no_one_of(self, value):
with pytest.raises(NoOneOfSchema):
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(), ])
def test_object_default_property(self, value):
schema = Schema('object', default='value1')
Expand Down