From f0586bfb9bd6a976544424f5d4d56ca722222e6b Mon Sep 17 00:00:00 2001 From: Kasper Revsbech Date: Wed, 6 Dec 2023 14:12:14 +0100 Subject: [PATCH 1/2] paths.py: Operation: allow openapi content type containing encoding Commit 26bdafb12cbc60c6ae6d9d8b4ce5c17e04e0bcf5 allows the server to write encoding in the `Content-Type` header field. However, some frameworks also put the encoding in the openapi spec This commit ensure to parse the response in that case --- openapi3/paths.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/openapi3/paths.py b/openapi3/paths.py index f8d0f2e..3e5774b 100644 --- a/openapi3/paths.py +++ b/openapi3/paths.py @@ -362,6 +362,15 @@ def request(self, base_url, security={}, data=None, parameters={}, verify=True, return content_type = result.headers["Content-Type"] + expected_media = expected_response.content.get(content_type, None) + + if expected_media is not None: + if content_type.lower().startswith("application/json"): + return expected_media.schema.model(result.json()) + else: + raise NotImplementedError() + + # Handle if received content type does not match expected content type excatly if ';' in content_type: # if the content type that came in included an encoding, we'll ignore # it for now (requests has already parsed it for us) and only look at From 8e1ecb85ee1b2f39af64770ff44fe17ad1b1e4f8 Mon Sep 17 00:00:00 2001 From: Kasper Revsbech Date: Wed, 3 Jan 2024 13:37:15 +0100 Subject: [PATCH 2/2] paths.py: _request_handle_body : allow application/json contenxt type containing encoding --- openapi3/paths.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openapi3/paths.py b/openapi3/paths.py index 3e5774b..5c14e6d 100644 --- a/openapi3/paths.py +++ b/openapi3/paths.py @@ -267,7 +267,8 @@ def _request_handle_parameters(self, parameters={}): self._request.url = self._request.url.format(**path_parameters) def _request_handle_body(self, data): - if "application/json" in self.requestBody.content: + is_json = [k for k in self.requestBody.content.keys() if k.startswith('application/json')] + if is_json: if isinstance(data, dict) or isinstance(data, list): body = json.dumps(data)