Skip to content

Commit b0109cd

Browse files
feat: Serialize model query parameters with style=form, explode=true BNCH-18023 (#39)
1 parent f49d0bc commit b0109cd

File tree

8 files changed

+61
-26
lines changed

8 files changed

+61
-26
lines changed

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from ...models.an_enum import AnEnum
1515
from ...models.http_validation_error import HTTPValidationError
16+
from ...models.model_with_union_property import ModelWithUnionProperty
1617
from ...types import UNSET, Unset
1718

1819

@@ -50,6 +51,7 @@ def httpx_request(
5051
union_prop: Union[Unset, float, str] = "not a float",
5152
union_prop_with_ref: Union[Unset, float, AnEnum] = 0.6,
5253
enum_prop: Union[Unset, AnEnum] = UNSET,
54+
model_prop: Union[ModelWithUnionProperty, Unset] = UNSET,
5355
) -> Response[Union[None, HTTPValidationError]]:
5456

5557
json_datetime_prop: Union[Unset, str] = UNSET
@@ -89,27 +91,33 @@ def httpx_request(
8991
if not isinstance(enum_prop, Unset):
9092
json_enum_prop = enum_prop
9193

94+
json_model_prop: Union[Unset, Dict[str, Any]] = UNSET
95+
if not isinstance(model_prop, Unset):
96+
json_model_prop = model_prop.to_dict()
97+
9298
params: Dict[str, Any] = {}
93-
if string_prop is not UNSET:
99+
if not isinstance(string_prop, Unset) and string_prop is not None:
94100
params["string_prop"] = string_prop
95-
if datetime_prop is not UNSET:
101+
if not isinstance(json_datetime_prop, Unset) and json_datetime_prop is not None:
96102
params["datetime_prop"] = json_datetime_prop
97-
if date_prop is not UNSET:
103+
if not isinstance(json_date_prop, Unset) and json_date_prop is not None:
98104
params["date_prop"] = json_date_prop
99-
if float_prop is not UNSET:
105+
if not isinstance(float_prop, Unset) and float_prop is not None:
100106
params["float_prop"] = float_prop
101-
if int_prop is not UNSET:
107+
if not isinstance(int_prop, Unset) and int_prop is not None:
102108
params["int_prop"] = int_prop
103-
if boolean_prop is not UNSET:
109+
if not isinstance(boolean_prop, Unset) and boolean_prop is not None:
104110
params["boolean_prop"] = boolean_prop
105-
if list_prop is not UNSET:
111+
if not isinstance(json_list_prop, Unset) and json_list_prop is not None:
106112
params["list_prop"] = json_list_prop
107-
if union_prop is not UNSET:
113+
if not isinstance(json_union_prop, Unset) and json_union_prop is not None:
108114
params["union_prop"] = json_union_prop
109-
if union_prop_with_ref is not UNSET:
115+
if not isinstance(json_union_prop_with_ref, Unset) and json_union_prop_with_ref is not None:
110116
params["union_prop_with_ref"] = json_union_prop_with_ref
111-
if enum_prop is not UNSET:
117+
if not isinstance(json_enum_prop, Unset) and json_enum_prop is not None:
112118
params["enum_prop"] = json_enum_prop
119+
if not isinstance(json_model_prop, Unset) and json_model_prop is not None:
120+
params.update(json_model_prop)
113121

114122
response = client.request(
115123
"post",

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/optional_value_tests_optional_query_param.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def httpx_request(
4444
json_query_param = query_param
4545

4646
params: Dict[str, Any] = {}
47-
if query_param is not UNSET:
47+
if not isinstance(json_query_param, Unset) and json_query_param is not None:
4848
params["query_param"] = json_query_param
4949

5050
response = client.request(

end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py

+26-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ...client import Client
88
from ...models.an_enum import AnEnum
99
from ...models.http_validation_error import HTTPValidationError
10+
from ...models.model_with_union_property import ModelWithUnionProperty
1011
from ...types import UNSET, Response, Unset
1112

1213

@@ -23,6 +24,7 @@ def _get_kwargs(
2324
union_prop: Union[Unset, float, str] = "not a float",
2425
union_prop_with_ref: Union[Unset, float, AnEnum] = 0.6,
2526
enum_prop: Union[Unset, AnEnum] = UNSET,
27+
model_prop: Union[ModelWithUnionProperty, Unset] = UNSET,
2628
) -> Dict[str, Any]:
2729
url = "{}/tests/defaults".format(client.base_url)
2830

@@ -65,27 +67,33 @@ def _get_kwargs(
6567
if not isinstance(enum_prop, Unset):
6668
json_enum_prop = enum_prop
6769

70+
json_model_prop: Union[Unset, Dict[str, Any]] = UNSET
71+
if not isinstance(model_prop, Unset):
72+
json_model_prop = model_prop.to_dict()
73+
6874
params: Dict[str, Any] = {}
69-
if string_prop is not UNSET:
75+
if not isinstance(string_prop, Unset) and string_prop is not None:
7076
params["string_prop"] = string_prop
71-
if datetime_prop is not UNSET:
77+
if not isinstance(json_datetime_prop, Unset) and json_datetime_prop is not None:
7278
params["datetime_prop"] = json_datetime_prop
73-
if date_prop is not UNSET:
79+
if not isinstance(json_date_prop, Unset) and json_date_prop is not None:
7480
params["date_prop"] = json_date_prop
75-
if float_prop is not UNSET:
81+
if not isinstance(float_prop, Unset) and float_prop is not None:
7682
params["float_prop"] = float_prop
77-
if int_prop is not UNSET:
83+
if not isinstance(int_prop, Unset) and int_prop is not None:
7884
params["int_prop"] = int_prop
79-
if boolean_prop is not UNSET:
85+
if not isinstance(boolean_prop, Unset) and boolean_prop is not None:
8086
params["boolean_prop"] = boolean_prop
81-
if list_prop is not UNSET:
87+
if not isinstance(json_list_prop, Unset) and json_list_prop is not None:
8288
params["list_prop"] = json_list_prop
83-
if union_prop is not UNSET:
89+
if not isinstance(json_union_prop, Unset) and json_union_prop is not None:
8490
params["union_prop"] = json_union_prop
85-
if union_prop_with_ref is not UNSET:
91+
if not isinstance(json_union_prop_with_ref, Unset) and json_union_prop_with_ref is not None:
8692
params["union_prop_with_ref"] = json_union_prop_with_ref
87-
if enum_prop is not UNSET:
93+
if not isinstance(json_enum_prop, Unset) and json_enum_prop is not None:
8894
params["enum_prop"] = json_enum_prop
95+
if not isinstance(json_model_prop, Unset) and json_model_prop is not None:
96+
params.update(json_model_prop)
8997

9098
return {
9199
"url": url,
@@ -130,6 +138,7 @@ def sync_detailed(
130138
union_prop: Union[Unset, float, str] = "not a float",
131139
union_prop_with_ref: Union[Unset, float, AnEnum] = 0.6,
132140
enum_prop: Union[Unset, AnEnum] = UNSET,
141+
model_prop: Union[ModelWithUnionProperty, Unset] = UNSET,
133142
) -> Response[Union[None, HTTPValidationError]]:
134143
kwargs = _get_kwargs(
135144
client=client,
@@ -143,6 +152,7 @@ def sync_detailed(
143152
union_prop=union_prop,
144153
union_prop_with_ref=union_prop_with_ref,
145154
enum_prop=enum_prop,
155+
model_prop=model_prop,
146156
)
147157

148158
response = httpx.post(
@@ -165,6 +175,7 @@ def sync(
165175
union_prop: Union[Unset, float, str] = "not a float",
166176
union_prop_with_ref: Union[Unset, float, AnEnum] = 0.6,
167177
enum_prop: Union[Unset, AnEnum] = UNSET,
178+
model_prop: Union[ModelWithUnionProperty, Unset] = UNSET,
168179
) -> Optional[Union[None, HTTPValidationError]]:
169180
""" """
170181

@@ -180,6 +191,7 @@ def sync(
180191
union_prop=union_prop,
181192
union_prop_with_ref=union_prop_with_ref,
182193
enum_prop=enum_prop,
194+
model_prop=model_prop,
183195
).parsed
184196

185197

@@ -196,6 +208,7 @@ async def asyncio_detailed(
196208
union_prop: Union[Unset, float, str] = "not a float",
197209
union_prop_with_ref: Union[Unset, float, AnEnum] = 0.6,
198210
enum_prop: Union[Unset, AnEnum] = UNSET,
211+
model_prop: Union[ModelWithUnionProperty, Unset] = UNSET,
199212
) -> Response[Union[None, HTTPValidationError]]:
200213
kwargs = _get_kwargs(
201214
client=client,
@@ -209,6 +222,7 @@ async def asyncio_detailed(
209222
union_prop=union_prop,
210223
union_prop_with_ref=union_prop_with_ref,
211224
enum_prop=enum_prop,
225+
model_prop=model_prop,
212226
)
213227

214228
async with httpx.AsyncClient() as _client:
@@ -230,6 +244,7 @@ async def asyncio(
230244
union_prop: Union[Unset, float, str] = "not a float",
231245
union_prop_with_ref: Union[Unset, float, AnEnum] = 0.6,
232246
enum_prop: Union[Unset, AnEnum] = UNSET,
247+
model_prop: Union[ModelWithUnionProperty, Unset] = UNSET,
233248
) -> Optional[Union[None, HTTPValidationError]]:
234249
""" """
235250

@@ -246,5 +261,6 @@ async def asyncio(
246261
union_prop=union_prop,
247262
union_prop_with_ref=union_prop_with_ref,
248263
enum_prop=enum_prop,
264+
model_prop=model_prop,
249265
)
250266
).parsed

end_to_end_tests/golden-record/my_test_api_client/api/tests/optional_value_tests_optional_query_param.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def _get_kwargs(
2121
json_query_param = query_param
2222

2323
params: Dict[str, Any] = {}
24-
if query_param is not UNSET:
24+
if not isinstance(json_query_param, Unset) and json_query_param is not None:
2525
params["query_param"] = json_query_param
2626

2727
return {

end_to_end_tests/openapi.json

+8
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,14 @@
396396
},
397397
"name": "enum_prop",
398398
"in": "query"
399+
},
400+
{
401+
"required": false,
402+
"schema": {
403+
"$ref": "#/components/schemas/ModelWithUnionProperty"
404+
},
405+
"name": "model_prop",
406+
"in": "query"
399407
}
400408
],
401409
"responses": {

openapi_python_client/parser/properties/model_property.py

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ModelProperty(Property):
2727
additional_properties: Union[bool, Property]
2828

2929
template: ClassVar[str] = "model_property.pyi"
30+
json_is_dict: ClassVar[bool] = True
3031

3132
def resolve_references(
3233
self, components: Dict[str, Union[oai.Reference, oai.Schema]], schemas: Schemas

openapi_python_client/parser/properties/property.py

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Property:
2828
python_name: str = attr.ib(init=False)
2929

3030
template: ClassVar[Optional[str]] = None
31+
json_is_dict: ClassVar[bool] = False
3132

3233
def __attrs_post_init__(self) -> None:
3334
object.__setattr__(self, "python_name", utils.to_valid_python_identifier(utils.snake_case(self.name)))

openapi_python_client/templates/endpoint_macros.pyi

+5-4
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ params: Dict[str, Any] = {
3333
}
3434
{% for property in endpoint.query_parameters %}
3535
{% if not property.required %}
36-
if {{ property.python_name }} is not UNSET:
37-
{% if property.template %}
38-
params["{{ property.name }}"] = {{ "json_" + property.python_name }}
36+
{% set property_name = "json_" + property.python_name if property.template else property.python_name %}
37+
if {% if not property.required %}not isinstance({{ property_name }}, Unset) and {% endif %}{{ property_name }} is not None:
38+
{% if property.json_is_dict %}
39+
params.update({{ property_name }})
3940
{% else %}
40-
params["{{ property.name }}"] = {{ property.python_name }}
41+
params["{{ property.name }}"] = {{ property_name }}
4142
{% endif %}
4243
{% endif %}
4344
{% endfor %}

0 commit comments

Comments
 (0)