Skip to content

Commit bd7f37d

Browse files
committed
fix: do not emit Unset for required multi-body endpoints (#1451)
1 parent 46a4340 commit bd7f37d

3 files changed

Lines changed: 87 additions & 2 deletions

File tree

‎end_to_end_tests/functional_tests/generated_code_execution/test_multipart_bodies.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,41 @@ def test_json_body_still_sets_its_content_type(self, sync_detailed, Client, Uplo
114114

115115
assert request.headers["content-type"] == "application/json"
116116
assert request.content == b'{"name":"a"}'
117+
118+
119+
@with_generated_client_fixture(
120+
"""
121+
paths:
122+
/upload:
123+
post:
124+
operationId: upload
125+
requestBody:
126+
required: true
127+
content:
128+
application/json:
129+
schema:
130+
type: object
131+
properties:
132+
name: {type: string}
133+
multipart/form-data:
134+
schema:
135+
type: object
136+
properties:
137+
file: {type: string, format: binary}
138+
responses:
139+
"200": {description: OK}
140+
"""
141+
)
142+
@with_generated_code_imports(
143+
".api.default.upload.sync_detailed",
144+
".client.Client",
145+
".models.UploadJsonBody",
146+
".models.UploadFilesBody",
147+
".types.File",
148+
)
149+
class TestRequiredMultipartBodyAmongSeveralBodies:
150+
def test_json_body_still_sets_its_content_type(self, sync_detailed, Client, UploadJsonBody, UploadFilesBody, File):
151+
request = _send_and_capture(Client, sync_detailed, UploadJsonBody(name="a"))
152+
153+
assert request.headers["content-type"] == "application/json"
154+
assert request.content == b'{"name":"a"}'

‎openapi_python_client/templates/endpoint_macros.py.jinja‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@ client: AuthenticatedClient | Client,
123123
body: {{ endpoint.bodies[0].prop.get_type_string() | as_unembedded_code }}{% if not endpoint.bodies[0].prop.required %} = UNSET{% endif %},
124124
{% elif endpoint.bodies | length > 1 %}
125125
body:
126-
{%- for body in endpoint.bodies -%}{% set body_required = body_required and body.prop.required %}
126+
{%- set ns = namespace(body_required=true) -%}
127+
{%- for body in endpoint.bodies -%}
128+
{%- set ns.body_required = ns.body_required and body.prop.required -%}
127129
{{ body.prop.get_type_string(no_optional=True) | as_unembedded_code }} {% if not loop.last %} | {% endif %}
128-
{%- endfor -%}{% if not body_required %} | Unset = UNSET{% endif %}
130+
{%- endfor -%}{% if not ns.body_required %} | Unset = UNSET{% endif %}
129131
,
130132
{% endif %}
131133
{# query parameters #}

‎tests/test_templates/test_endpoint_macros.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,48 @@ def test_endpoint_docstring_unwraps_python_code_return_string(env) -> None:
3434

3535
assert "MyModel | None" in result
3636
assert "object at 0x" not in result
37+
38+
39+
def test_endpoint_arguments_multi_body_required(env) -> None:
40+
template = env.get_template("endpoint_macros.py.jinja")
41+
body1 = SimpleNamespace(
42+
prop=SimpleNamespace(required=True, get_type_string=lambda no_optional=False: PythonCode("ItemA"))
43+
)
44+
body2 = SimpleNamespace(
45+
prop=SimpleNamespace(required=True, get_type_string=lambda no_optional=False: PythonCode("ItemB"))
46+
)
47+
endpoint = SimpleNamespace(
48+
path_parameters=[],
49+
query_parameters=[],
50+
header_parameters=[],
51+
cookie_parameters=[],
52+
requires_security=False,
53+
bodies=[body1, body2],
54+
list_all_parameters=lambda: [body1.prop, body2.prop],
55+
)
56+
57+
result = template.module.arguments(endpoint, include_client=False)
58+
assert "Unset" not in result
59+
assert "UNSET" not in result
60+
61+
62+
def test_endpoint_arguments_multi_body_optional(env) -> None:
63+
template = env.get_template("endpoint_macros.py.jinja")
64+
body1 = SimpleNamespace(
65+
prop=SimpleNamespace(required=True, get_type_string=lambda no_optional=False: PythonCode("ItemA"))
66+
)
67+
body2 = SimpleNamespace(
68+
prop=SimpleNamespace(required=False, get_type_string=lambda no_optional=False: PythonCode("ItemB"))
69+
)
70+
endpoint = SimpleNamespace(
71+
path_parameters=[],
72+
query_parameters=[],
73+
header_parameters=[],
74+
cookie_parameters=[],
75+
requires_security=False,
76+
bodies=[body1, body2],
77+
list_all_parameters=lambda: [body1.prop, body2.prop],
78+
)
79+
80+
result = template.module.arguments(endpoint, include_client=False)
81+
assert "| Unset = UNSET" in result

0 commit comments

Comments
 (0)