Skip to content

Commit

Permalink
Merge pull request #2 from Leemur89/feature/support-more-content-type
Browse files Browse the repository at this point in the history
feat: support all application/json-like content types
  • Loading branch information
robinmackaij authored Mar 15, 2024
2 parents 6c344e2 + 9633e31 commit 4e1fc3f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
35 changes: 25 additions & 10 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 @@ -916,19 +918,32 @@ def get_request_parameters(
headers = self.get_parameter_data(header_params, parameter_relations)
return parameters, params, headers

@staticmethod
def get_content_schema(body_spec: Dict[str, Any]) -> Dict[str, Any]:
@classmethod
def get_content_schema(cls, 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 = cls.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 4e1fc3f

Please sign in to comment.