Skip to content

Commit

Permalink
Cache operation body definition (#1626)
Browse files Browse the repository at this point in the history
Fixes a small todo I left in the code before to cache the body
definition on the operation.
  • Loading branch information
RobbeSneyders committed Jan 26, 2023
1 parent 1211d1b commit 769120e
Showing 1 changed file with 16 additions and 19 deletions.
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

0 comments on commit 769120e

Please sign in to comment.