Skip to content

Commit 55e16f3

Browse files
feat: Change optional Open API query parameters to allow `None (#40)
1 parent b0109cd commit 55e16f3

38 files changed

+506
-329
lines changed

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

+68-32
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Client = httpx.Client
88

99
import datetime
10-
from typing import Dict, List, Union
10+
from typing import Dict, List, Optional, Union
1111

1212
from dateutil.parser import isoparse
1313

@@ -41,65 +41,101 @@ def _build_response(*, response: httpx.Response) -> Response[Union[None, HTTPVal
4141
def httpx_request(
4242
*,
4343
client: Client,
44-
string_prop: Union[Unset, str] = "the default string",
45-
datetime_prop: Union[Unset, datetime.datetime] = isoparse("1010-10-10T00:00:00"),
46-
date_prop: Union[Unset, datetime.date] = isoparse("1010-10-10").date(),
47-
float_prop: Union[Unset, float] = 3.14,
48-
int_prop: Union[Unset, int] = 7,
49-
boolean_prop: Union[Unset, bool] = False,
50-
list_prop: Union[Unset, List[AnEnum]] = UNSET,
51-
union_prop: Union[Unset, float, str] = "not a float",
52-
union_prop_with_ref: Union[Unset, float, AnEnum] = 0.6,
53-
enum_prop: Union[Unset, AnEnum] = UNSET,
54-
model_prop: Union[ModelWithUnionProperty, Unset] = UNSET,
44+
string_prop: Union[Unset, None, str] = "the default string",
45+
not_required_not_nullable_datetime_prop: Union[Unset, None, datetime.datetime] = isoparse("1010-10-10T00:00:00"),
46+
not_required_nullable_datetime_prop: Union[Unset, None, datetime.datetime] = isoparse("1010-10-10T00:00:00"),
47+
required_not_nullable_datetime_prop: datetime.datetime = isoparse("1010-10-10T00:00:00"),
48+
required_nullable_datetime_prop: Optional[datetime.datetime] = isoparse("1010-10-10T00:00:00"),
49+
date_prop: Union[Unset, None, datetime.date] = isoparse("1010-10-10").date(),
50+
float_prop: Union[Unset, None, float] = 3.14,
51+
int_prop: Union[Unset, None, int] = 7,
52+
boolean_prop: Union[Unset, None, bool] = False,
53+
list_prop: Union[Unset, None, List[AnEnum]] = UNSET,
54+
union_prop: Union[Unset, None, float, str] = "not a float",
55+
union_prop_with_ref: Union[Unset, None, float, AnEnum] = 0.6,
56+
enum_prop: Union[Unset, None, AnEnum] = UNSET,
57+
model_prop: Union[Unset, None, ModelWithUnionProperty] = UNSET,
5558
) -> Response[Union[None, HTTPValidationError]]:
5659

57-
json_datetime_prop: Union[Unset, str] = UNSET
58-
if not isinstance(datetime_prop, Unset):
59-
json_datetime_prop = datetime_prop.isoformat()
60+
json_not_required_not_nullable_datetime_prop: Union[Unset, None, str] = UNSET
61+
if not isinstance(not_required_not_nullable_datetime_prop, Unset):
62+
json_not_required_not_nullable_datetime_prop = (
63+
not_required_not_nullable_datetime_prop.isoformat() if not_required_not_nullable_datetime_prop else None
64+
)
6065

61-
json_date_prop: Union[Unset, str] = UNSET
66+
json_not_required_nullable_datetime_prop: Union[Unset, None, str] = UNSET
67+
if not isinstance(not_required_nullable_datetime_prop, Unset):
68+
json_not_required_nullable_datetime_prop = (
69+
not_required_nullable_datetime_prop.isoformat() if not_required_nullable_datetime_prop else None
70+
)
71+
72+
json_required_not_nullable_datetime_prop = required_not_nullable_datetime_prop.isoformat()
73+
74+
json_required_nullable_datetime_prop = (
75+
required_nullable_datetime_prop.isoformat() if required_nullable_datetime_prop else None
76+
)
77+
78+
json_date_prop: Union[Unset, None, str] = UNSET
6279
if not isinstance(date_prop, Unset):
63-
json_date_prop = date_prop.isoformat()
80+
json_date_prop = date_prop.isoformat() if date_prop else None
6481

65-
json_list_prop: Union[Unset, List[Any]] = UNSET
82+
json_list_prop: Union[Unset, None, List[Any]] = UNSET
6683
if not isinstance(list_prop, Unset):
67-
json_list_prop = []
68-
for list_prop_item_data in list_prop:
69-
list_prop_item = list_prop_item_data.value
84+
if list_prop is None:
85+
json_list_prop = None
86+
else:
87+
json_list_prop = []
88+
for list_prop_item_data in list_prop:
89+
list_prop_item = list_prop_item_data.value
7090

71-
json_list_prop.append(list_prop_item)
91+
json_list_prop.append(list_prop_item)
7292

73-
json_union_prop: Union[Unset, float, str]
93+
json_union_prop: Union[Unset, None, float, str]
7494
if isinstance(union_prop, Unset):
7595
json_union_prop = UNSET
96+
elif union_prop is None:
97+
json_union_prop = None
7698
else:
7799
json_union_prop = union_prop
78100

79-
json_union_prop_with_ref: Union[Unset, float, AnEnum]
101+
json_union_prop_with_ref: Union[Unset, None, float, int]
80102
if isinstance(union_prop_with_ref, Unset):
81103
json_union_prop_with_ref = UNSET
104+
elif union_prop_with_ref is None:
105+
json_union_prop_with_ref = None
82106
elif isinstance(union_prop_with_ref, AnEnum):
83107
json_union_prop_with_ref = UNSET
84108
if not isinstance(union_prop_with_ref, Unset):
85-
json_union_prop_with_ref = union_prop_with_ref
109+
json_union_prop_with_ref = union_prop_with_ref.value
86110

87111
else:
88112
json_union_prop_with_ref = union_prop_with_ref
89113

90-
json_enum_prop: Union[Unset, AnEnum] = UNSET
114+
json_enum_prop: Union[Unset, None, int] = UNSET
91115
if not isinstance(enum_prop, Unset):
92-
json_enum_prop = enum_prop
116+
json_enum_prop = enum_prop.value if enum_prop else None
93117

94-
json_model_prop: Union[Unset, Dict[str, Any]] = UNSET
118+
json_model_prop: Union[Unset, None, Dict[str, Any]] = UNSET
95119
if not isinstance(model_prop, Unset):
96-
json_model_prop = model_prop.to_dict()
120+
json_model_prop = model_prop.to_dict() if model_prop else None
97121

98-
params: Dict[str, Any] = {}
122+
params: Dict[str, Any] = {
123+
"required_not_nullable_datetime_prop": json_required_not_nullable_datetime_prop,
124+
}
99125
if not isinstance(string_prop, Unset) and string_prop is not None:
100126
params["string_prop"] = string_prop
101-
if not isinstance(json_datetime_prop, Unset) and json_datetime_prop is not None:
102-
params["datetime_prop"] = json_datetime_prop
127+
if (
128+
not isinstance(json_not_required_not_nullable_datetime_prop, Unset)
129+
and json_not_required_not_nullable_datetime_prop is not None
130+
):
131+
params["not_required_not_nullable_datetime_prop"] = json_not_required_not_nullable_datetime_prop
132+
if (
133+
not isinstance(json_not_required_nullable_datetime_prop, Unset)
134+
and json_not_required_nullable_datetime_prop is not None
135+
):
136+
params["not_required_nullable_datetime_prop"] = json_not_required_nullable_datetime_prop
137+
if json_required_nullable_datetime_prop is not None:
138+
params["required_nullable_datetime_prop"] = json_required_nullable_datetime_prop
103139
if not isinstance(json_date_prop, Unset) and json_date_prop is not None:
104140
params["date_prop"] = json_date_prop
105141
if not isinstance(float_prop, Unset) and float_prop is not None:

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ def _build_response(*, response: httpx.Response) -> Response[Union[None, HTTPVal
3636
def httpx_request(
3737
*,
3838
client: Client,
39-
query_param: Union[Unset, List[str]] = UNSET,
39+
query_param: Union[Unset, None, List[str]] = UNSET,
4040
) -> Response[Union[None, HTTPValidationError]]:
4141

42-
json_query_param: Union[Unset, List[Any]] = UNSET
42+
json_query_param: Union[Unset, None, List[Any]] = UNSET
4343
if not isinstance(query_param, Unset):
44-
json_query_param = query_param
44+
if query_param is None:
45+
json_query_param = None
46+
else:
47+
json_query_param = query_param
4548

4649
params: Dict[str, Any] = {}
4750
if not isinstance(json_query_param, Unset) and json_query_param is not None:

Diff for: end_to_end_tests/golden-record-custom/custom_e2e/models/a_model.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ class AModel:
2727
nullable_model: Optional[AModelNullableModel]
2828
nested_list_of_enums: Union[Unset, List[List[DifferentEnum]]] = UNSET
2929
attr_1_leading_digit: Union[Unset, str] = UNSET
30-
not_required_nullable: Union[Unset, Optional[str]] = UNSET
30+
not_required_nullable: Union[Unset, None, str] = UNSET
3131
not_required_not_nullable: Union[Unset, str] = UNSET
32-
not_required_model: Union[AModelNotRequiredModel, Unset] = UNSET
33-
not_required_nullable_model: Union[Optional[AModelNotRequiredNullableModel], Unset] = UNSET
32+
not_required_model: Union[Unset, AModelNotRequiredModel] = UNSET
33+
not_required_nullable_model: Union[Unset, None, AModelNotRequiredNullableModel] = UNSET
3434

3535
def to_dict(self) -> Dict[str, Any]:
3636
an_enum_value = self.an_enum_value.value
@@ -68,7 +68,7 @@ def to_dict(self) -> Dict[str, Any]:
6868
if not isinstance(self.not_required_model, Unset):
6969
not_required_model = self.not_required_model.to_dict()
7070

71-
not_required_nullable_model: Union[None, Unset, Dict[str, Any]] = UNSET
71+
not_required_nullable_model: Union[Unset, None, Dict[str, Any]] = UNSET
7272
if not isinstance(self.not_required_nullable_model, Unset):
7373
not_required_nullable_model = (
7474
self.not_required_nullable_model.to_dict() if self.not_required_nullable_model else None
@@ -158,7 +158,7 @@ def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.dat
158158
if _nullable_model is not None:
159159
nullable_model = AModelNullableModel.from_dict(cast(Dict[str, Any], _nullable_model))
160160

161-
not_required_model: Union[AModelNotRequiredModel, Unset] = UNSET
161+
not_required_model: Union[Unset, AModelNotRequiredModel] = UNSET
162162
_not_required_model = d.pop("not_required_model", UNSET)
163163
if not isinstance(_not_required_model, Unset):
164164
not_required_model = AModelNotRequiredModel.from_dict(cast(Dict[str, Any], _not_required_model))

Diff for: end_to_end_tests/golden-record-custom/custom_e2e/models/a_model_model.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ class AModelModel:
1515
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
1616

1717
def to_dict(self) -> Dict[str, Any]:
18-
a_property: Union[Unset, AnEnum, AnIntEnum]
18+
a_property: Union[Unset, int]
1919
if isinstance(self.a_property, Unset):
2020
a_property = UNSET
2121
elif isinstance(self.a_property, AnEnum):
2222
a_property = UNSET
2323
if not isinstance(self.a_property, Unset):
24-
a_property = self.a_property
24+
a_property = self.a_property.value
2525

2626
else:
2727
a_property = UNSET
2828
if not isinstance(self.a_property, Unset):
29-
a_property = self.a_property
29+
a_property = self.a_property.value
3030

3131
field_dict: Dict[str, Any] = {}
3232
field_dict.update(self.additional_properties)

Diff for: end_to_end_tests/golden-record-custom/custom_e2e/models/a_model_not_required_model.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ class AModelNotRequiredModel:
1515
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
1616

1717
def to_dict(self) -> Dict[str, Any]:
18-
a_property: Union[Unset, AnEnum, AnIntEnum]
18+
a_property: Union[Unset, int]
1919
if isinstance(self.a_property, Unset):
2020
a_property = UNSET
2121
elif isinstance(self.a_property, AnEnum):
2222
a_property = UNSET
2323
if not isinstance(self.a_property, Unset):
24-
a_property = self.a_property
24+
a_property = self.a_property.value
2525

2626
else:
2727
a_property = UNSET
2828
if not isinstance(self.a_property, Unset):
29-
a_property = self.a_property
29+
a_property = self.a_property.value
3030

3131
field_dict: Dict[str, Any] = {}
3232
field_dict.update(self.additional_properties)

Diff for: end_to_end_tests/golden-record-custom/custom_e2e/models/a_model_not_required_nullable_model.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ class AModelNotRequiredNullableModel:
1515
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
1616

1717
def to_dict(self) -> Dict[str, Any]:
18-
a_property: Union[Unset, AnEnum, AnIntEnum]
18+
a_property: Union[Unset, int]
1919
if isinstance(self.a_property, Unset):
2020
a_property = UNSET
2121
elif isinstance(self.a_property, AnEnum):
2222
a_property = UNSET
2323
if not isinstance(self.a_property, Unset):
24-
a_property = self.a_property
24+
a_property = self.a_property.value
2525

2626
else:
2727
a_property = UNSET
2828
if not isinstance(self.a_property, Unset):
29-
a_property = self.a_property
29+
a_property = self.a_property.value
3030

3131
field_dict: Dict[str, Any] = {}
3232
field_dict.update(self.additional_properties)

Diff for: end_to_end_tests/golden-record-custom/custom_e2e/models/a_model_nullable_model.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ class AModelNullableModel:
1515
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
1616

1717
def to_dict(self) -> Dict[str, Any]:
18-
a_property: Union[Unset, AnEnum, AnIntEnum]
18+
a_property: Union[Unset, int]
1919
if isinstance(self.a_property, Unset):
2020
a_property = UNSET
2121
elif isinstance(self.a_property, AnEnum):
2222
a_property = UNSET
2323
if not isinstance(self.a_property, Unset):
24-
a_property = self.a_property
24+
a_property = self.a_property.value
2525

2626
else:
2727
a_property = UNSET
2828
if not isinstance(self.a_property, Unset):
29-
a_property = self.a_property
29+
a_property = self.a_property.value
3030

3131
field_dict: Dict[str, Any] = {}
3232
field_dict.update(self.additional_properties)

Diff for: end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_primitive_additional_properties.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class ModelWithPrimitiveAdditionalProperties:
1313
""" """
1414

15-
a_date_holder: Union[ModelWithPrimitiveAdditionalPropertiesADateHolder, Unset] = UNSET
15+
a_date_holder: Union[Unset, ModelWithPrimitiveAdditionalPropertiesADateHolder] = UNSET
1616
additional_properties: Dict[str, str] = attr.ib(init=False, factory=dict)
1717

1818
def to_dict(self) -> Dict[str, Any]:
@@ -31,7 +31,7 @@ def to_dict(self) -> Dict[str, Any]:
3131
@staticmethod
3232
def from_dict(src_dict: Dict[str, Any]) -> "ModelWithPrimitiveAdditionalProperties":
3333
d = src_dict.copy()
34-
a_date_holder: Union[ModelWithPrimitiveAdditionalPropertiesADateHolder, Unset] = UNSET
34+
a_date_holder: Union[Unset, ModelWithPrimitiveAdditionalPropertiesADateHolder] = UNSET
3535
_a_date_holder = d.pop("a_date_holder", UNSET)
3636
if not isinstance(_a_date_holder, Unset):
3737
a_date_holder = ModelWithPrimitiveAdditionalPropertiesADateHolder.from_dict(

Diff for: end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ class ModelWithUnionProperty:
1414
a_property: Union[Unset, AnEnum, AnIntEnum] = UNSET
1515

1616
def to_dict(self) -> Dict[str, Any]:
17-
a_property: Union[Unset, AnEnum, AnIntEnum]
17+
a_property: Union[Unset, int]
1818
if isinstance(self.a_property, Unset):
1919
a_property = UNSET
2020
elif isinstance(self.a_property, AnEnum):
2121
a_property = UNSET
2222
if not isinstance(self.a_property, Unset):
23-
a_property = self.a_property
23+
a_property = self.a_property.value
2424

2525
else:
2626
a_property = UNSET
2727
if not isinstance(self.a_property, Unset):
28-
a_property = self.a_property
28+
a_property = self.a_property.value
2929

3030
field_dict: Dict[str, Any] = {}
3131
field_dict.update({})

Diff for: end_to_end_tests/golden-record-custom/custom_e2e/types.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ def __bool__(self) -> bool:
1111

1212
UNSET: Unset = Unset()
1313

14+
# Used as `FileProperty._json_type_string`
15+
FileJsonType = Tuple[Optional[str], Union[BinaryIO, TextIO], Optional[str]]
16+
1417

1518
@attr.s(auto_attribs=True)
1619
class File:
@@ -20,7 +23,7 @@ class File:
2023
file_name: Optional[str] = None
2124
mime_type: Optional[str] = None
2225

23-
def to_tuple(self) -> Tuple[Optional[str], Union[BinaryIO, TextIO], Optional[str]]:
26+
def to_tuple(self) -> FileJsonType:
2427
""" Return a tuple representation that httpx will accept for multipart/form-data """
2528
return self.file_name, self.payload, self.mime_type
2629

0 commit comments

Comments
 (0)