Skip to content

Commit

Permalink
🐛 Fix: add content-type header when request has body (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Oct 10, 2024
1 parent 336d1de commit ea279c5
Show file tree
Hide file tree
Showing 59 changed files with 5,484 additions and 1,103 deletions.
1 change: 1 addition & 0 deletions codegen/parser/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class RequestBodyData:

type: Literal["form", "json", "file", "raw"]
body_schema: SchemaData
content_type: str | None = None
required: bool = False

@property
Expand Down
55 changes: 44 additions & 11 deletions codegen/parser/endpoints/request_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
from ...source import Source


DEFAULT_JSON_CONTENT_TYPE = "application/json"
DEFAULT_FORM_CONTENT_TYPE = "application/x-www-form-urlencoded"
DEFAULT_MULTIPART_CONTENT_TYPE = "multipart/form-data"
DEFAULT_TEXT_CONTENT_TYPE = "text/plain"
DEFAULT_BINARY_CONTENT_TYPE = "application/octet-stream"


def build_request_body(source: "Source", prefix: str) -> RequestBodyData:
data = type_ref_from_source(source, oas.RequestBody)

Expand All @@ -19,53 +26,78 @@ def build_request_body(source: "Source", prefix: str) -> RequestBodyData:

media_types = list(data.content.keys())
if json_types := [type for type in media_types if "json" in type]:
json_type = json_types[0]
json_type = (
DEFAULT_JSON_CONTENT_TYPE
if DEFAULT_JSON_CONTENT_TYPE in json_types
else json_types[0]
)
return RequestBodyData(
type="json",
body_schema=parse_schema(
source / "content" / json_type / "schema",
concat_snake_name(prefix, "body"),
),
content_type=json_type,
required=data.required,
)
elif form_types := [type for type in media_types if "form" in type]:
form_type = form_types[0]
elif file_types := [type for type in media_types if "multipart" in type]:
file_type = (
DEFAULT_MULTIPART_CONTENT_TYPE
if DEFAULT_MULTIPART_CONTENT_TYPE in file_types
else file_types[0]
)
return RequestBodyData(
type="form",
type="file",
body_schema=parse_schema(
source / "content" / form_type / "schema",
source / "content" / file_type / "schema",
concat_snake_name(prefix, "body"),
),
content_type=file_type,
required=data.required,
)
elif file_types := [type for type in media_types if "multipart" in type]:
file_type = file_types[0]
elif form_types := [type for type in media_types if "form" in type]:
form_type = (
DEFAULT_FORM_CONTENT_TYPE
if DEFAULT_FORM_CONTENT_TYPE in form_types
else form_types[0]
)
return RequestBodyData(
type="file",
type="form",
body_schema=parse_schema(
source / "content" / file_type / "schema",
source / "content" / form_type / "schema",
concat_snake_name(prefix, "body"),
),
content_type=form_type,
required=data.required,
)
elif text_types := [type for type in media_types if "text" in type]:
text_type = text_types[0]
text_type = (
DEFAULT_TEXT_CONTENT_TYPE
if DEFAULT_TEXT_CONTENT_TYPE in text_types
else text_types[0]
)
return RequestBodyData(
type="raw",
body_schema=parse_schema(
source / "content" / text_type / "schema",
concat_snake_name(prefix, "body"),
),
content_type=text_type,
required=data.required,
)
elif binary_types := [type for type in media_types if "octet-stream" in type]:
binary_type = binary_types[0]
binary_type = (
DEFAULT_BINARY_CONTENT_TYPE
if DEFAULT_BINARY_CONTENT_TYPE in binary_types
else binary_types[0]
)
return RequestBodyData(
type="raw",
body_schema=parse_schema(
source / "content" / binary_type / "schema",
concat_snake_name(prefix, "body"),
),
content_type=binary_type,
required=data.required,
)
elif "*/*" in media_types:
Expand All @@ -75,6 +107,7 @@ def build_request_body(source: "Source", prefix: str) -> RequestBodyData:
source / "content" / "*/*" / "schema",
concat_snake_name(prefix, "body"),
),
content_type=None,
required=data.required,
)

Expand Down
7 changes: 5 additions & 2 deletions codegen/templates/rest/_request.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ params = {
{% endif %}
{% endmacro %}

{% macro build_header(params) %}
{% macro build_header(params, request_body) %}
headers = {
{% for param in params %}
"{{ param.name }}": {{ param.prop_name }},
{% endfor %}
{% if request_body and request_body.content_type %}
"Content-Type": "{{ request_body.content_type }}",
{% endif %}
"X-GitHub-Api-Version": self._REST_API_VERSION,
**(headers or {})
}
Expand Down Expand Up @@ -79,7 +82,7 @@ if self._github.config.rest_api_body_validation:

{{ build_path(endpoint) }}
{{ build_query(endpoint.query_params) }}
{{ build_header(endpoint.header_params) }}
{{ build_header(endpoint.header_params, endpoint.request_body) }}
{{ build_cookie(endpoint.cookie_params) }}
{% if endpoint.request_body %}
{{ build_body(endpoint.request_body) }}
Expand Down
Loading

0 comments on commit ea279c5

Please sign in to comment.