Skip to content

Commit eb1c52e

Browse files
committed
Merge branch 'feature/add-unset-value-and-model-to-dict-params' into support_inline_object_schemas
2 parents 8b9b3d8 + 961f6df commit eb1c52e

26 files changed

+759
-271
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- Update minimum Pydantic version to support Python 3.9
2121

2222
### Additions
23+
- Better compatibility for "required" (whether or not the field must be included) and "nullable" (whether or not the field can be null) (#205 & #208). Thanks @bowenwr & @emannguitar!
2324

2425
- Allow specifying the generated client's version using `package_version_override` in a config file. (#225 - Thanks @fyhertz!)
2526

end_to_end_tests/golden-record-custom/my_test_api_client/models/a_model.py

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,27 @@
66

77
from ..models.an_enum import AnEnum
88
from ..models.different_enum import DifferentEnum
9+
from ..types import UNSET, Unset
910

1011

1112
@attr.s(auto_attribs=True)
1213
class AModel:
1314
""" A Model for testing all the ways custom objects can be used """
1415

1516
an_enum_value: AnEnum
16-
some_dict: Optional[Dict[Any, Any]]
1717
a_camel_date_time: Union[datetime.datetime, datetime.date]
1818
a_date: datetime.date
19-
nested_list_of_enums: Optional[List[List[DifferentEnum]]] = None
20-
attr_1_leading_digit: Optional[str] = None
19+
required_not_nullable: str
20+
nested_list_of_enums: Union[Unset, List[List[DifferentEnum]]] = UNSET
21+
some_dict: Optional[Dict[Any, Any]] = None
22+
attr_1_leading_digit: Union[Unset, str] = UNSET
23+
required_nullable: Optional[str] = None
24+
not_required_nullable: Union[Unset, Optional[str]] = UNSET
25+
not_required_not_nullable: Union[Unset, str] = UNSET
2126

2227
def to_dict(self) -> Dict[str, Any]:
2328
an_enum_value = self.an_enum_value.value
2429

25-
some_dict = self.some_dict
26-
2730
if isinstance(self.a_camel_date_time, datetime.datetime):
2831
a_camel_date_time = self.a_camel_date_time.isoformat()
2932

@@ -32,9 +35,9 @@ def to_dict(self) -> Dict[str, Any]:
3235

3336
a_date = self.a_date.isoformat()
3437

35-
if self.nested_list_of_enums is None:
36-
nested_list_of_enums = None
37-
else:
38+
required_not_nullable = self.required_not_nullable
39+
nested_list_of_enums: Union[Unset, List[Any]] = UNSET
40+
if not isinstance(self.nested_list_of_enums, Unset):
3841
nested_list_of_enums = []
3942
for nested_list_of_enums_item_data in self.nested_list_of_enums:
4043
nested_list_of_enums_item = []
@@ -45,23 +48,36 @@ def to_dict(self) -> Dict[str, Any]:
4548

4649
nested_list_of_enums.append(nested_list_of_enums_item)
4750

51+
some_dict = self.some_dict if self.some_dict else None
52+
4853
attr_1_leading_digit = self.attr_1_leading_digit
54+
required_nullable = self.required_nullable
55+
not_required_nullable = self.not_required_nullable
56+
not_required_not_nullable = self.not_required_not_nullable
4957

50-
return {
58+
field_dict = {
5159
"an_enum_value": an_enum_value,
52-
"some_dict": some_dict,
5360
"aCamelDateTime": a_camel_date_time,
5461
"a_date": a_date,
55-
"nested_list_of_enums": nested_list_of_enums,
56-
"1_leading_digit": attr_1_leading_digit,
62+
"required_not_nullable": required_not_nullable,
63+
"some_dict": some_dict,
64+
"required_nullable": required_nullable,
5765
}
66+
if nested_list_of_enums is not UNSET:
67+
field_dict["nested_list_of_enums"] = nested_list_of_enums
68+
if attr_1_leading_digit is not UNSET:
69+
field_dict["1_leading_digit"] = attr_1_leading_digit
70+
if not_required_nullable is not UNSET:
71+
field_dict["not_required_nullable"] = not_required_nullable
72+
if not_required_not_nullable is not UNSET:
73+
field_dict["not_required_not_nullable"] = not_required_not_nullable
74+
75+
return field_dict
5876

5977
@staticmethod
6078
def from_dict(d: Dict[str, Any]) -> "AModel":
6179
an_enum_value = AnEnum(d["an_enum_value"])
6280

63-
some_dict = d["some_dict"]
64-
6581
def _parse_a_camel_date_time(data: Dict[str, Any]) -> Union[datetime.datetime, datetime.date]:
6682
a_camel_date_time: Union[datetime.datetime, datetime.date]
6783
try:
@@ -78,8 +94,10 @@ def _parse_a_camel_date_time(data: Dict[str, Any]) -> Union[datetime.datetime, d
7894

7995
a_date = isoparse(d["a_date"]).date()
8096

97+
required_not_nullable = d["required_not_nullable"]
98+
8199
nested_list_of_enums = []
82-
for nested_list_of_enums_item_data in d.get("nested_list_of_enums") or []:
100+
for nested_list_of_enums_item_data in d.get("nested_list_of_enums", UNSET) or []:
83101
nested_list_of_enums_item = []
84102
for nested_list_of_enums_item_item_data in nested_list_of_enums_item_data:
85103
nested_list_of_enums_item_item = DifferentEnum(nested_list_of_enums_item_item_data)
@@ -88,13 +106,25 @@ def _parse_a_camel_date_time(data: Dict[str, Any]) -> Union[datetime.datetime, d
88106

89107
nested_list_of_enums.append(nested_list_of_enums_item)
90108

91-
attr_1_leading_digit = d.get("1_leading_digit")
109+
some_dict = d["some_dict"]
110+
111+
attr_1_leading_digit = d.get("1_leading_digit", UNSET)
112+
113+
required_nullable = d["required_nullable"]
114+
115+
not_required_nullable = d.get("not_required_nullable", UNSET)
116+
117+
not_required_not_nullable = d.get("not_required_not_nullable", UNSET)
92118

93119
return AModel(
94120
an_enum_value=an_enum_value,
95-
some_dict=some_dict,
96121
a_camel_date_time=a_camel_date_time,
97122
a_date=a_date,
123+
required_not_nullable=required_not_nullable,
98124
nested_list_of_enums=nested_list_of_enums,
125+
some_dict=some_dict,
99126
attr_1_leading_digit=attr_1_leading_digit,
127+
required_nullable=required_nullable,
128+
not_required_nullable=not_required_nullable,
129+
not_required_not_nullable=not_required_not_nullable,
100130
)

end_to_end_tests/golden-record-custom/my_test_api_client/models/http_validation_error.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
1-
from typing import Any, Dict, List, Optional
1+
from typing import Any, Dict, List, Union
22

33
import attr
44

55
from ..models.validation_error import ValidationError
6+
from ..types import UNSET, Unset
67

78

89
@attr.s(auto_attribs=True)
910
class HTTPValidationError:
1011
""" """
1112

12-
detail: Optional[List[ValidationError]] = None
13+
detail: Union[Unset, List[ValidationError]] = UNSET
1314

1415
def to_dict(self) -> Dict[str, Any]:
15-
if self.detail is None:
16-
detail = None
17-
else:
16+
detail: Union[Unset, List[Any]] = UNSET
17+
if not isinstance(self.detail, Unset):
1818
detail = []
1919
for detail_item_data in self.detail:
2020
detail_item = detail_item_data.to_dict()
2121

2222
detail.append(detail_item)
2323

24-
return {
25-
"detail": detail,
26-
}
24+
field_dict = {}
25+
if detail is not UNSET:
26+
field_dict["detail"] = detail
27+
28+
return field_dict
2729

2830
@staticmethod
2931
def from_dict(d: Dict[str, Any]) -> "HTTPValidationError":
3032
detail = []
31-
for detail_item_data in d.get("detail") or []:
33+
for detail_item_data in d.get("detail", UNSET) or []:
3234
detail_item = ValidationError.from_dict(detail_item_data)
3335

3436
detail.append(detail_item)

0 commit comments

Comments
 (0)