Skip to content

Commit

Permalink
feat: support all application/json-like content types
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael SMADJA committed Mar 15, 2024
1 parent 6c344e2 commit 979831a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
31 changes: 23 additions & 8 deletions src/OpenApiLibCore/openapi_libcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"""

import json as _json
import re
import sys
from copy import deepcopy
from dataclasses import Field, dataclass, field, make_dataclass
Expand Down Expand Up @@ -849,6 +850,7 @@ def get_request_data(self, endpoint: str, method: str) -> RequestData:
has_body=False,
)
content_schema = resolve_schema(self.get_content_schema(body_spec))
headers.update({"content-type": self.get_content_type(body_spec)})
dto_data = self.get_json_data_for_dto_class(
schema=content_schema,
dto_class=dto_class,
Expand Down Expand Up @@ -919,16 +921,29 @@ def get_request_parameters(
@staticmethod
def get_content_schema(body_spec: Dict[str, Any]) -> Dict[str, Any]:
"""Get the content schema from the requestBody spec."""
content_types = body_spec["content"].keys()
if "application/json" not in content_types:
# At present no supported for other types.
raise NotImplementedError(
f"Only content type 'application/json' is supported. "
f"Content types definded in the spec are '{content_types}'."
)
content_schema = body_spec["content"]["application/json"]["schema"]
content_type = OpenApiLibCore.get_content_type(body_spec)
content_schema = body_spec["content"][content_type]["schema"]
return resolve_schema(content_schema)

@staticmethod
def get_content_type(body_spec: Dict[str, Any]) -> str:
"""Get and validate the first supported content type from the requested body spec
Should be application/json like content type,
e.g "application/json;charset=utf-8" or "application/merge-patch+json"
"""
content_types: List[str] = body_spec["content"].keys()
json_regex = r"application/([a-z\-]+\+)?json(;\s?charset=(.+))?"
for content_type in content_types:
if re.search(json_regex, content_type):
return content_type

# At present no supported for other types.
raise NotImplementedError(
f"Only content types like 'application/json' are supported. "
f"Content types definded in the spec are '{content_types}'."
)

def get_parametrized_endpoint(self, endpoint: str) -> str:
"""
Get the parametrized endpoint as found in the `paths` section of the openapi
Expand Down
6 changes: 4 additions & 2 deletions tests/libcore/suites/test_get_request_data.robot
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Test Get Request Data For Endpoint With RequestBody
Should Not Be Empty ${request_data.dto_schema}
Should Be Equal ${request_data.parameters} ${list}
Should Be Equal ${request_data.params} ${dict}
Should Be Equal ${request_data.headers} ${dict}
&{expected_headers}= Create Dictionary content-type=application/json
Should Be Equal ${request_data.headers} ${expected_headers}
Should Be True ${request_data.has_body}

Test Get Request Data For Endpoint Without RequestBody But With DtoClass
Expand All @@ -57,5 +58,6 @@ Test Get Request Data For Endpoint Without RequestBody But With DtoClass
# Should Be Equal ${request_data.dto_schema} ${dict}
# Should Not Be Empty ${request_data.parameters}
# Should Be Equal ${request_data.params} ${dict}
# Should Be Equal ${request_data.headers} ${dict}
# &{expected_headers}= Create Dictionary content-type=application/json
# Should Be Equal ${request_data.headers} ${expected_headers}
# Should Be True ${request_data.has_body}

0 comments on commit 979831a

Please sign in to comment.