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
Changes from 1 commit
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