-
-
Notifications
You must be signed in to change notification settings - Fork 176
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
Rework components interface #381
Changes from 3 commits
46b2b5e
94f340d
570de0b
31f0303
0d82009
1f75a40
4f0df4c
8ca47ed
f582df8
c478a10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,15 +108,7 @@ def to_dict(self): | |
security_key: self._security_schemes, | ||
} | ||
|
||
def schema( | ||
self, | ||
name, | ||
properties=None, | ||
enum=None, | ||
description=None, | ||
extra_fields=None, | ||
**kwargs | ||
): | ||
def schema(self, name, definition=None, **kwargs): | ||
"""Add a new definition to the spec. | ||
|
||
.. note:: | ||
|
@@ -138,70 +130,67 @@ def schema( | |
raise DuplicateComponentNameError( | ||
'Another schema with name "{}" is already registered.'.format(name) | ||
) | ||
ret = {} | ||
definition = definition or {} | ||
ret = definition.copy() | ||
# Execute all helpers from plugins | ||
for plugin in self._plugins: | ||
try: | ||
ret.update(plugin.schema_helper(name, definition=ret, **kwargs) or {}) | ||
ret.update(plugin.schema_helper(name, definition, **kwargs) or {}) | ||
except PluginMethodNotImplementedError: | ||
continue | ||
if properties: | ||
ret["properties"] = properties | ||
if enum: | ||
ret["enum"] = enum | ||
if description: | ||
ret["description"] = description | ||
if extra_fields: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, nice. This cleans things up quite a bit.
Update: ended up modifying the section to show how to add additional properties to auto-generated properties. |
||
ret.update(extra_fields) | ||
self._schemas[name] = ret | ||
return self | ||
|
||
def parameter(self, param_id, location, **kwargs): | ||
def parameter(self, param_id, location, param=None, **kwargs): | ||
""" Add a parameter which can be referenced. | ||
|
||
:param str param_id: identifier by which parameter may be referenced. | ||
:param str location: location of the parameter. | ||
:param dict kwargs: parameter fields. | ||
:param dict param: parameter fields. | ||
:param dict kwargs: plugin-specific arguments | ||
""" | ||
if param_id in self._parameters: | ||
raise DuplicateComponentNameError( | ||
'Another parameter with name "{}" is already registered.'.format( | ||
param_id | ||
) | ||
) | ||
ret = kwargs.copy() | ||
param = param or {} | ||
ret = param.copy() | ||
ret.setdefault("name", param_id) | ||
ret["in"] = location | ||
# Execute all helpers from plugins | ||
for plugin in self._plugins: | ||
try: | ||
ret.update(plugin.parameter_helper(**kwargs) or {}) | ||
ret.update(plugin.parameter_helper(param, **kwargs) or {}) | ||
except PluginMethodNotImplementedError: | ||
continue | ||
self._parameters[param_id] = ret | ||
return self | ||
|
||
def response(self, ref_id, **kwargs): | ||
def response(self, ref_id, resp=None, **kwargs): | ||
"""Add a response which can be referenced. | ||
|
||
:param str ref_id: ref_id to use as reference | ||
:param dict kwargs: response fields | ||
:param dict resp: response fields | ||
:param dict kwargs: plugin-specific arguments | ||
""" | ||
if ref_id in self._responses: | ||
raise DuplicateComponentNameError( | ||
'Another response with name "{}" is already registered.'.format(ref_id) | ||
) | ||
ret = kwargs.copy() | ||
resp = resp or {} | ||
ret = resp.copy() | ||
# Execute all helpers from plugins | ||
for plugin in self._plugins: | ||
try: | ||
ret.update(plugin.response_helper(**kwargs) or {}) | ||
ret.update(plugin.response_helper(resp, **kwargs) or {}) | ||
except PluginMethodNotImplementedError: | ||
continue | ||
self._responses[ref_id] = ret | ||
return self | ||
|
||
def security_scheme(self, sec_id, **kwargs): | ||
def security_scheme(self, sec_id, sec_scheme): | ||
"""Add a security scheme which can be referenced. | ||
|
||
:param str sec_id: sec_id to use as reference | ||
|
@@ -213,7 +202,7 @@ def security_scheme(self, sec_id, **kwargs): | |
sec_id | ||
) | ||
) | ||
self._security_schemes[sec_id] = kwargs | ||
self._security_schemes[sec_id] = sec_scheme | ||
return self | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than use an OAS 2 term, "definition", what if we use a consistent parameter name for all the component methods? "component", perhaps? "obj" could also work, since OAS calls these "Objects", but that could be conflated with Python's "object".