Skip to content

Commit 4d99cbe

Browse files
committed
Move additional props check to separate method
1 parent 14196b6 commit 4d99cbe

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

openapi_core/schema/schemas/models.py

Lines changed: 13 additions & 12 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,12 +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-
no_more_properties_allowed = (
315-
(self.additional_properties is False) or
316-
(one_of_schema is not None and
317-
one_of_schema.additional_properties is False)
318-
)
319-
if extra_props and no_more_properties_allowed:
321+
extra_props_allowed = self.are_additional_properties_allowed(
322+
one_of_schema)
323+
if extra_props and not extra_props_allowed:
320324
raise UndefinedSchemaProperty(extra_props)
321325

322326
properties = {}
@@ -548,12 +552,9 @@ def _validate_properties(self, value, one_of_schema=None,
548552

549553
value_props_names = value.keys()
550554
extra_props = set(value_props_names) - set(all_props_names)
551-
no_more_properties_allowed = (
552-
(self.additional_properties is False) or
553-
(one_of_schema is not None and
554-
one_of_schema.additional_properties is False)
555-
)
556-
if extra_props and no_more_properties_allowed:
555+
extra_props_allowed = self.are_additional_properties_allowed(
556+
one_of_schema)
557+
if extra_props and not extra_props_allowed:
557558
raise UndefinedSchemaProperty(extra_props)
558559

559560
if self.additional_properties is not True:

tests/unit/schema/test_schemas.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -713,31 +713,31 @@ def test_object_no_one_of(self, value):
713713

714714
@pytest.mark.parametrize('value', [
715715
Model({
716-
u'foo': u'FOO',
716+
'foo': u("FOO"),
717717
}),
718718
Model({
719-
u'foo': u'FOO',
720-
u'bar': u'BAR',
719+
'foo': u("FOO"),
720+
'bar': u("BAR"),
721721
}),
722722
])
723723
def test_unambiguous_one_of(self, value):
724724
one_of = [
725725
Schema(
726726
'object',
727727
properties={
728-
u'foo': Schema('string'),
728+
'foo': Schema('string'),
729729
},
730730
additional_properties=False,
731-
required=[u'foo'],
731+
required=['foo'],
732732
),
733733
Schema(
734734
'object',
735735
properties={
736-
u'foo': Schema('string'),
737-
u'bar': Schema('string'),
736+
'foo': Schema('string'),
737+
'bar': Schema('string'),
738738
},
739739
additional_properties=False,
740-
required=[u'foo', u'bar'],
740+
required=['foo', 'bar'],
741741
),
742742
]
743743
schema = Schema('object', one_of=one_of)

0 commit comments

Comments
 (0)