Skip to content

Commit 10c9570

Browse files
authored
Merge pull request python-openapi#125 from diogobaeder/master
Fix python-openapi#124: Checking "additionalProperties" in "oneOf" items.
2 parents 8fdc6f3 + 8a483c6 commit 10c9570

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

openapi_core/schema/schemas/models.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ def get_cast_mapping(self, custom_formatters=None, strict=True):
161161

162162
return defaultdict(lambda: lambda x: x, mapping)
163163

164+
def are_additional_properties_allowed(self, one_of_schema=None):
165+
return (
166+
(self.additional_properties is not False) and
167+
(one_of_schema is None or
168+
one_of_schema.additional_properties is not False)
169+
)
170+
164171
def cast(self, value, custom_formatters=None, strict=True):
165172
"""Cast value to schema type"""
166173
if value is None:
@@ -311,7 +318,9 @@ def _unmarshal_properties(self, value, one_of_schema=None,
311318

312319
value_props_names = value.keys()
313320
extra_props = set(value_props_names) - set(all_props_names)
314-
if extra_props and self.additional_properties is False:
321+
extra_props_allowed = self.are_additional_properties_allowed(
322+
one_of_schema)
323+
if extra_props and not extra_props_allowed:
315324
raise UndefinedSchemaProperty(extra_props)
316325

317326
properties = {}
@@ -543,7 +552,9 @@ def _validate_properties(self, value, one_of_schema=None,
543552

544553
value_props_names = value.keys()
545554
extra_props = set(value_props_names) - set(all_props_names)
546-
if extra_props and self.additional_properties is False:
555+
extra_props_allowed = self.are_additional_properties_allowed(
556+
one_of_schema)
557+
if extra_props and not extra_props_allowed:
547558
raise UndefinedSchemaProperty(extra_props)
548559

549560
if self.additional_properties is not True:

tests/unit/schema/test_schemas.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,39 @@ def test_object_no_one_of(self, value):
711711
with pytest.raises(NoOneOfSchema):
712712
schema.validate(value)
713713

714+
@pytest.mark.parametrize('value', [
715+
Model({
716+
'foo': u("FOO"),
717+
}),
718+
Model({
719+
'foo': u("FOO"),
720+
'bar': u("BAR"),
721+
}),
722+
])
723+
def test_unambiguous_one_of(self, value):
724+
one_of = [
725+
Schema(
726+
'object',
727+
properties={
728+
'foo': Schema('string'),
729+
},
730+
additional_properties=False,
731+
required=['foo'],
732+
),
733+
Schema(
734+
'object',
735+
properties={
736+
'foo': Schema('string'),
737+
'bar': Schema('string'),
738+
},
739+
additional_properties=False,
740+
required=['foo', 'bar'],
741+
),
742+
]
743+
schema = Schema('object', one_of=one_of)
744+
745+
schema.validate(value)
746+
714747
@pytest.mark.parametrize('value', [Model(), ])
715748
def test_object_default_property(self, value):
716749
schema = Schema('object', default='value1')

0 commit comments

Comments
 (0)