Skip to content

feat: handle allof when required linked to parent object (#97) #154

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
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
16 changes: 13 additions & 3 deletions openapi_spec_validator/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,33 @@ class SchemaValidator(object):
def __init__(self, dereferencer):
self.dereferencer = dereferencer

def _nested_properties(self, schema):
schema_deref = self.dereferencer.dereference(schema)
return schema_deref.get("properties", {}).keys()

@wraps_errors
def iter_errors(self, schema, require_properties=True):
schema_deref = self.dereferencer.dereference(schema)
if not isinstance(schema_deref, dict):
return

nested_properties = []
if 'allOf' in schema_deref:
for inner_schema in schema_deref['allOf']:
for err in self.iter_errors(
inner_schema,
require_properties=False
inner_schema,
require_properties=False
):
yield err
nested_properties = nested_properties + list(self._nested_properties(inner_schema))

required = schema_deref.get('required', [])
properties = schema_deref.get('properties', {}).keys()
extra_properties = list(set(required) - set(properties))
if 'allOf' in schema_deref:
extra_properties = list(set(required) - set(properties) - set(nested_properties))
else:
extra_properties = list(set(required) - set(properties))

if extra_properties and require_properties:
yield ExtraParametersError(
"Required list has not defined properties: {0}".format(
Expand Down
46 changes: 46 additions & 0 deletions tests/integration/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,52 @@ def test_allow_allof_required_no_properties(self, validator_v30):
errors_list = list(errors)
assert errors_list == []

def test_allow_allof_when_required_is_linked_to_the_parent_object(self, validator_v30):
spec = {
'openapi': '3.0.1',
'info': {
'title': 'Test Api',
'version': '0.0.1',
},
'paths': {},
'components': {
'schemas': {
'Address': {
'type': 'object',
'properties': {
'SubdivisionCode': {
'type': 'string',
'description': 'State or region'
},
'Town': {
'type': 'string',
'description': 'Town or city'
},
'CountryCode': {
'type': 'string',
}
}
},
'AddressCreation': {
'required': [
'CountryCode',
'Town'
],
'type': 'object',
'allOf': [
{
'$ref': '#/components/schemas/Address'
}
]
}
}
}
}

errors = validator_v30.iter_errors(spec)
errors_list = list(errors)
assert errors_list == []

def test_extra_parameters_in_required(self, validator_v30):
spec = {
'openapi': '3.0.0',
Expand Down