FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: do not emit Unset for required multi-body endpoints (#1451) by Rajan-vish · Pull Request #1517 · openapi-generators/openapi-python-client · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .jinja  (1) .py  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,41 @@ def test_json_body_still_sets_its_content_type(self, sync_detailed, Client, Uplo

assert request.headers["content-type"] == "application/json"
assert request.content == b'{"name":"a"}'


@with_generated_client_fixture(
"""
paths:
/upload:
post:
operationId: upload
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
name: {type: string}
multipart/form-data:
schema:
type: object
properties:
file: {type: string, format: binary}
responses:
"200": {description: OK}
"""
)
@with_generated_code_imports(
".api.default.upload.sync_detailed",
".client.Client",
".models.UploadJsonBody",
".models.UploadFilesBody",
".types.File",
)
class TestRequiredMultipartBodyAmongSeveralBodies:
def test_json_body_still_sets_its_content_type(self, sync_detailed, Client, UploadJsonBody, UploadFilesBody, File):
request = _send_and_capture(Client, sync_detailed, UploadJsonBody(name="a"))

assert request.headers["content-type"] == "application/json"
assert request.content == b'{"name":"a"}'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ client: AuthenticatedClient | Client,
body: {{ endpoint.bodies[0].prop.get_type_string() | as_unembedded_code }}{% if not endpoint.bodies[0].prop.required %} = UNSET{% endif %},
{% elif endpoint.bodies | length > 1 %}
body:
{%- for body in endpoint.bodies -%}{% set body_required = body_required and body.prop.required %}
{%- set ns = namespace(body_required=true) -%}
{%- for body in endpoint.bodies -%}
{%- set ns.body_required = ns.body_required and body.prop.required -%}
{{ body.prop.get_type_string(no_optional=True) | as_unembedded_code }} {% if not loop.last %} | {% endif %}
{%- endfor -%}{% if not body_required %} | Unset = UNSET{% endif %}
{%- endfor -%}{% if not ns.body_required %} | Unset = UNSET{% endif %}
,
{% endif %}
{# query parameters #}
Expand Down
45 changes: 45 additions & 0 deletions tests/test_templates/test_endpoint_macros.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,48 @@ def test_endpoint_docstring_unwraps_python_code_return_string(env) -> None:

assert "MyModel | None" in result
assert "object at 0x" not in result


def test_endpoint_arguments_multi_body_required(env) -> None:
template = env.get_template("endpoint_macros.py.jinja")
body1 = SimpleNamespace(
prop=SimpleNamespace(required=True, get_type_string=lambda no_optional=False: PythonCode("ItemA"))
)
body2 = SimpleNamespace(
prop=SimpleNamespace(required=True, get_type_string=lambda no_optional=False: PythonCode("ItemB"))
)
endpoint = SimpleNamespace(
path_parameters=[],
query_parameters=[],
header_parameters=[],
cookie_parameters=[],
requires_security=False,
bodies=[body1, body2],
list_all_parameters=lambda: [body1.prop, body2.prop],
)

result = template.module.arguments(endpoint, include_client=False)
assert "Unset" not in result
assert "UNSET" not in result


def test_endpoint_arguments_multi_body_optional(env) -> None:
template = env.get_template("endpoint_macros.py.jinja")
body1 = SimpleNamespace(
prop=SimpleNamespace(required=True, get_type_string=lambda no_optional=False: PythonCode("ItemA"))
)
body2 = SimpleNamespace(
prop=SimpleNamespace(required=False, get_type_string=lambda no_optional=False: PythonCode("ItemB"))
)
endpoint = SimpleNamespace(
path_parameters=[],
query_parameters=[],
header_parameters=[],
cookie_parameters=[],
requires_security=False,
bodies=[body1, body2],
list_all_parameters=lambda: [body1.prop, body2.prop],
)

result = template.module.arguments(endpoint, include_client=False)
assert "| Unset = UNSET" in result

Back | FazBrowse Home | New Git URL