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

Cache operation body definition #1626

Merged
merged 1 commit into from
Jan 26, 2023
Merged
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
35 changes: 16 additions & 19 deletions connexion/operations/swagger2.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,8 @@ def __init__(
self._parameters += path_parameters

self._responses = operation.get("responses", {})
logger.debug(self._responses)

logger.debug("consumes: %s", self.consumes)
logger.debug("produces: %s", self.produces)
self._body_definitions = {}

@classmethod
def from_spec(cls, spec, api, path, method, resolver, *args, **kwargs):
Expand Down Expand Up @@ -230,24 +228,23 @@ def body_definition(self, content_type: str = None) -> dict:
The body complete definition for this operation.

**There can be one "body" parameter at most.**

:rtype: dict
"""
# TODO: cache
if content_type in FORM_CONTENT_TYPES:
form_parameters = [p for p in self.parameters if p["in"] == "formData"]
body_definition = self._transform_form(form_parameters)
else:
body_parameters = [p for p in self.parameters if p["in"] == "body"]
if len(body_parameters) > 1:
raise InvalidSpecification(
"{method} {path} There can be one 'body' parameter at most".format(
method=self.method, path=self.path
if self._body_definitions.get(content_type) is None:
if content_type in FORM_CONTENT_TYPES:
form_parameters = [p for p in self.parameters if p["in"] == "formData"]
_body_definition = self._transform_form(form_parameters)
else:
body_parameters = [p for p in self.parameters if p["in"] == "body"]
if len(body_parameters) > 1:
raise InvalidSpecification(
"{method} {path} There can be one 'body' parameter at most".format(
method=self.method, path=self.path
)
)
)
body_parameter = body_parameters[0] if body_parameters else {}
body_definition = self._transform_json(body_parameter)
return body_definition
body_parameter = body_parameters[0] if body_parameters else {}
_body_definition = self._transform_json(body_parameter)
self._body_definitions[content_type] = _body_definition
return self._body_definitions[content_type]

def _transform_json(self, body_parameter: dict) -> dict:
"""Translate Swagger2 json parameters into OpenAPI 3 jsonschema spec."""
Expand Down