Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: add content-type header when request has body #153

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading