Skip to content
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

Deep update components instead of overwriting components for OpenAPI 3 #222

Merged
merged 2 commits into from
Jun 9, 2018
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
21 changes: 16 additions & 5 deletions apispec/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,26 @@ def to_dict(self):
ret['swagger'] = self.openapi_version.vstring
ret['definitions'] = self._definitions
ret['parameters'] = self._parameters
ret.update(self.options)

elif self.openapi_version.version[0] == 3:
ret['openapi'] = self.openapi_version.vstring
ret['components'] = {
'schemas': self._definitions,
'parameters': self._parameters,
}
options = self.options.copy()
components = options.pop('components', {})

# deep update components object
definitions = components.pop('schemas', {})
definitions.update(self._definitions)
parameters = components.pop('parameters', {})
parameters.update(self._parameters)

ret['components'] = dict(
schemas=definitions,
parameters=parameters,
**components
)
ret.update(options)

ret.update(self.options)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ret.update(self.options) can be kept factorized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I explicitly pass components in options, this line of code would shadow ret['components'] that is generated by codes above. And that is why I have to differentiate the behaviors between OpenAPI 2 and 3.

return ret

def to_yaml(self):
Expand Down
39 changes: 38 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,28 @@ def spec():

@pytest.fixture()
def spec_3():
components = {
'securitySchemes': {
'bearerAuth':
dict(type='http', scheme='bearer', bearerFormat='JWT')
},
'schemas': {
'ErrorResponse': {
'type': 'object',
'properties': {
'ok': {
'type': 'boolean', 'description': 'status indicator', 'example': False
}
},
'required': ['ok']
}
}
}
return APISpec(
title='Swagger Petstore',
version='1.0.0',
info={'description': description},
security=[{'apiKey': []}],
components=components,
openapi_version='3.0.0'
)

Expand Down Expand Up @@ -64,6 +81,26 @@ def test_swagger_metadata(self, spec):
assert metadata['info']['version'] == '1.0.0'
assert metadata['info']['description'] == description

def test_swagger_metadata_v3(self, spec_3):
metadata = spec_3.to_dict()
security_schemes = {'bearerAuth': dict(type='http', scheme='bearer', bearerFormat='JWT')}
assert metadata['components']['securitySchemes'] == security_schemes
assert metadata['components']['schemas'].get('ErrorResponse', False)
assert metadata['info']['title'] == 'Swagger Petstore'
assert metadata['info']['version'] == '1.0.0'
assert metadata['info']['description'] == description

def test_swagger_metadata_merge_v3(self, spec_3):
properties = {
'ok': {
'type': 'boolean', 'description': 'property description', 'example': True
}
}
spec_3.definition('definition', properties=properties, description='definiton description')
metadata = spec_3.to_dict()
assert metadata['components']['schemas'].get('ErrorResponse', False)
assert metadata['components']['schemas'].get('definition', False)


class TestTags:

Expand Down