Skip to content

Commit 43245c0

Browse files
authored
Fixed issue with non-required fields in a model not being marked as such (#239) (#240)
Co-authored-by: Ethan Mann <[email protected]>
1 parent 1a0dc6d commit 43245c0

File tree

10 files changed

+119
-85
lines changed

10 files changed

+119
-85
lines changed

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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)
@@ -16,11 +17,11 @@ class AModel:
1617
a_camel_date_time: Union[datetime.datetime, datetime.date]
1718
a_date: datetime.date
1819
required_not_nullable: str
19-
nested_list_of_enums: List[List[DifferentEnum]]
20-
attr_1_leading_digit: str
2120
required_nullable: Optional[str]
22-
not_required_nullable: Optional[str]
23-
not_required_not_nullable: str
21+
nested_list_of_enums: Union[Unset, List[List[DifferentEnum]]] = UNSET
22+
attr_1_leading_digit: Union[Unset, str] = UNSET
23+
not_required_nullable: Union[Unset, Optional[str]] = UNSET
24+
not_required_not_nullable: Union[Unset, str] = UNSET
2425

2526
def to_dict(self) -> Dict[str, Any]:
2627
an_enum_value = self.an_enum_value.value
@@ -34,15 +35,17 @@ def to_dict(self) -> Dict[str, Any]:
3435
a_date = self.a_date.isoformat()
3536

3637
required_not_nullable = self.required_not_nullable
37-
nested_list_of_enums = []
38-
for nested_list_of_enums_item_data in self.nested_list_of_enums:
39-
nested_list_of_enums_item = []
40-
for nested_list_of_enums_item_item_data in nested_list_of_enums_item_data:
41-
nested_list_of_enums_item_item = nested_list_of_enums_item_item_data.value
38+
nested_list_of_enums: Union[Unset, List[Any]] = UNSET
39+
if not isinstance(self.nested_list_of_enums, Unset):
40+
nested_list_of_enums = []
41+
for nested_list_of_enums_item_data in self.nested_list_of_enums:
42+
nested_list_of_enums_item = []
43+
for nested_list_of_enums_item_item_data in nested_list_of_enums_item_data:
44+
nested_list_of_enums_item_item = nested_list_of_enums_item_item_data.value
4245

43-
nested_list_of_enums_item.append(nested_list_of_enums_item_item)
46+
nested_list_of_enums_item.append(nested_list_of_enums_item_item)
4447

45-
nested_list_of_enums.append(nested_list_of_enums_item)
48+
nested_list_of_enums.append(nested_list_of_enums_item)
4649

4750
attr_1_leading_digit = self.attr_1_leading_digit
4851
required_nullable = self.required_nullable
@@ -54,12 +57,16 @@ def to_dict(self) -> Dict[str, Any]:
5457
"aCamelDateTime": a_camel_date_time,
5558
"a_date": a_date,
5659
"required_not_nullable": required_not_nullable,
57-
"nested_list_of_enums": nested_list_of_enums,
58-
"1_leading_digit": attr_1_leading_digit,
5960
"required_nullable": required_nullable,
60-
"not_required_nullable": not_required_nullable,
61-
"not_required_not_nullable": not_required_not_nullable,
6261
}
62+
if nested_list_of_enums is not UNSET:
63+
field_dict["nested_list_of_enums"] = nested_list_of_enums
64+
if attr_1_leading_digit is not UNSET:
65+
field_dict["1_leading_digit"] = attr_1_leading_digit
66+
if not_required_nullable is not UNSET:
67+
field_dict["not_required_nullable"] = not_required_nullable
68+
if not_required_not_nullable is not UNSET:
69+
field_dict["not_required_not_nullable"] = not_required_not_nullable
6370

6471
return field_dict
6572

@@ -86,7 +93,7 @@ def _parse_a_camel_date_time(data: Dict[str, Any]) -> Union[datetime.datetime, d
8693
required_not_nullable = d["required_not_nullable"]
8794

8895
nested_list_of_enums = []
89-
for nested_list_of_enums_item_data in d["nested_list_of_enums"]:
96+
for nested_list_of_enums_item_data in d.get("nested_list_of_enums", UNSET) or []:
9097
nested_list_of_enums_item = []
9198
for nested_list_of_enums_item_item_data in nested_list_of_enums_item_data:
9299
nested_list_of_enums_item_item = DifferentEnum(nested_list_of_enums_item_item_data)
@@ -95,13 +102,13 @@ def _parse_a_camel_date_time(data: Dict[str, Any]) -> Union[datetime.datetime, d
95102

96103
nested_list_of_enums.append(nested_list_of_enums_item)
97104

98-
attr_1_leading_digit = d["1_leading_digit"]
105+
attr_1_leading_digit = d.get("1_leading_digit", UNSET)
99106

100107
required_nullable = d["required_nullable"]
101108

102-
not_required_nullable = d["not_required_nullable"]
109+
not_required_nullable = d.get("not_required_nullable", UNSET)
103110

104-
not_required_not_nullable = d["not_required_not_nullable"]
111+
not_required_not_nullable = d.get("not_required_not_nullable", UNSET)
105112

106113
return AModel(
107114
an_enum_value=an_enum_value,

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1-
from typing import Any, Dict, List
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: List[ValidationError]
13+
detail: Union[Unset, List[ValidationError]] = UNSET
1314

1415
def to_dict(self) -> Dict[str, Any]:
15-
detail = []
16-
for detail_item_data in self.detail:
17-
detail_item = detail_item_data.to_dict()
16+
detail: Union[Unset, List[Any]] = UNSET
17+
if not isinstance(self.detail, Unset):
18+
detail = []
19+
for detail_item_data in self.detail:
20+
detail_item = detail_item_data.to_dict()
1821

19-
detail.append(detail_item)
22+
detail.append(detail_item)
2023

21-
field_dict = {
22-
"detail": detail,
23-
}
24+
field_dict = {}
25+
if detail is not UNSET:
26+
field_dict["detail"] = detail
2427

2528
return field_dict
2629

2730
@staticmethod
2831
def from_dict(d: Dict[str, Any]) -> "HTTPValidationError":
2932
detail = []
30-
for detail_item_data in d["detail"]:
33+
for detail_item_data in d.get("detail", UNSET) or []:
3134
detail_item = ValidationError.from_dict(detail_item_data)
3235

3336
detail.append(detail_item)

end_to_end_tests/golden-record-custom/custom_e2e/models/test_inline_objectsjson_body.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
from typing import Any, Dict
1+
from typing import Any, Dict, Union
22

33
import attr
44

5+
from ..types import UNSET, Unset
6+
57

68
@attr.s(auto_attribs=True)
79
class TestInlineObjectsjsonBody:
810
""" """
911

10-
a_property: str
12+
a_property: Union[Unset, str] = UNSET
1113

1214
def to_dict(self) -> Dict[str, Any]:
1315
a_property = self.a_property
1416

15-
field_dict = {
16-
"a_property": a_property,
17-
}
17+
field_dict = {}
18+
if a_property is not UNSET:
19+
field_dict["a_property"] = a_property
1820

1921
return field_dict
2022

2123
@staticmethod
2224
def from_dict(d: Dict[str, Any]) -> "TestInlineObjectsjsonBody":
23-
a_property = d["a_property"]
25+
a_property = d.get("a_property", UNSET)
2426

2527
return TestInlineObjectsjsonBody(
2628
a_property=a_property,

end_to_end_tests/golden-record-custom/custom_e2e/models/test_inline_objectsresponse_200.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
from typing import Any, Dict
1+
from typing import Any, Dict, Union
22

33
import attr
44

5+
from ..types import UNSET, Unset
6+
57

68
@attr.s(auto_attribs=True)
79
class TestInlineObjectsresponse_200:
810
""" """
911

10-
a_property: str
12+
a_property: Union[Unset, str] = UNSET
1113

1214
def to_dict(self) -> Dict[str, Any]:
1315
a_property = self.a_property
1416

15-
field_dict = {
16-
"a_property": a_property,
17-
}
17+
field_dict = {}
18+
if a_property is not UNSET:
19+
field_dict["a_property"] = a_property
1820

1921
return field_dict
2022

2123
@staticmethod
2224
def from_dict(d: Dict[str, Any]) -> "TestInlineObjectsresponse_200":
23-
a_property = d["a_property"]
25+
a_property = d.get("a_property", UNSET)
2426

2527
return TestInlineObjectsresponse_200(
2628
a_property=a_property,

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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)
@@ -16,11 +17,11 @@ class AModel:
1617
a_camel_date_time: Union[datetime.datetime, datetime.date]
1718
a_date: datetime.date
1819
required_not_nullable: str
19-
nested_list_of_enums: List[List[DifferentEnum]]
20-
attr_1_leading_digit: str
2120
required_nullable: Optional[str]
22-
not_required_nullable: Optional[str]
23-
not_required_not_nullable: str
21+
nested_list_of_enums: Union[Unset, List[List[DifferentEnum]]] = UNSET
22+
attr_1_leading_digit: Union[Unset, str] = UNSET
23+
not_required_nullable: Union[Unset, Optional[str]] = UNSET
24+
not_required_not_nullable: Union[Unset, str] = UNSET
2425

2526
def to_dict(self) -> Dict[str, Any]:
2627
an_enum_value = self.an_enum_value.value
@@ -34,15 +35,17 @@ def to_dict(self) -> Dict[str, Any]:
3435
a_date = self.a_date.isoformat()
3536

3637
required_not_nullable = self.required_not_nullable
37-
nested_list_of_enums = []
38-
for nested_list_of_enums_item_data in self.nested_list_of_enums:
39-
nested_list_of_enums_item = []
40-
for nested_list_of_enums_item_item_data in nested_list_of_enums_item_data:
41-
nested_list_of_enums_item_item = nested_list_of_enums_item_item_data.value
38+
nested_list_of_enums: Union[Unset, List[Any]] = UNSET
39+
if not isinstance(self.nested_list_of_enums, Unset):
40+
nested_list_of_enums = []
41+
for nested_list_of_enums_item_data in self.nested_list_of_enums:
42+
nested_list_of_enums_item = []
43+
for nested_list_of_enums_item_item_data in nested_list_of_enums_item_data:
44+
nested_list_of_enums_item_item = nested_list_of_enums_item_item_data.value
4245

43-
nested_list_of_enums_item.append(nested_list_of_enums_item_item)
46+
nested_list_of_enums_item.append(nested_list_of_enums_item_item)
4447

45-
nested_list_of_enums.append(nested_list_of_enums_item)
48+
nested_list_of_enums.append(nested_list_of_enums_item)
4649

4750
attr_1_leading_digit = self.attr_1_leading_digit
4851
required_nullable = self.required_nullable
@@ -54,12 +57,16 @@ def to_dict(self) -> Dict[str, Any]:
5457
"aCamelDateTime": a_camel_date_time,
5558
"a_date": a_date,
5659
"required_not_nullable": required_not_nullable,
57-
"nested_list_of_enums": nested_list_of_enums,
58-
"1_leading_digit": attr_1_leading_digit,
5960
"required_nullable": required_nullable,
60-
"not_required_nullable": not_required_nullable,
61-
"not_required_not_nullable": not_required_not_nullable,
6261
}
62+
if nested_list_of_enums is not UNSET:
63+
field_dict["nested_list_of_enums"] = nested_list_of_enums
64+
if attr_1_leading_digit is not UNSET:
65+
field_dict["1_leading_digit"] = attr_1_leading_digit
66+
if not_required_nullable is not UNSET:
67+
field_dict["not_required_nullable"] = not_required_nullable
68+
if not_required_not_nullable is not UNSET:
69+
field_dict["not_required_not_nullable"] = not_required_not_nullable
6370

6471
return field_dict
6572

@@ -86,7 +93,7 @@ def _parse_a_camel_date_time(data: Dict[str, Any]) -> Union[datetime.datetime, d
8693
required_not_nullable = d["required_not_nullable"]
8794

8895
nested_list_of_enums = []
89-
for nested_list_of_enums_item_data in d["nested_list_of_enums"]:
96+
for nested_list_of_enums_item_data in d.get("nested_list_of_enums", UNSET) or []:
9097
nested_list_of_enums_item = []
9198
for nested_list_of_enums_item_item_data in nested_list_of_enums_item_data:
9299
nested_list_of_enums_item_item = DifferentEnum(nested_list_of_enums_item_item_data)
@@ -95,13 +102,13 @@ def _parse_a_camel_date_time(data: Dict[str, Any]) -> Union[datetime.datetime, d
95102

96103
nested_list_of_enums.append(nested_list_of_enums_item)
97104

98-
attr_1_leading_digit = d["1_leading_digit"]
105+
attr_1_leading_digit = d.get("1_leading_digit", UNSET)
99106

100107
required_nullable = d["required_nullable"]
101108

102-
not_required_nullable = d["not_required_nullable"]
109+
not_required_nullable = d.get("not_required_nullable", UNSET)
103110

104-
not_required_not_nullable = d["not_required_not_nullable"]
111+
not_required_not_nullable = d.get("not_required_not_nullable", UNSET)
105112

106113
return AModel(
107114
an_enum_value=an_enum_value,

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1-
from typing import Any, Dict, List
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: List[ValidationError]
13+
detail: Union[Unset, List[ValidationError]] = UNSET
1314

1415
def to_dict(self) -> Dict[str, Any]:
15-
detail = []
16-
for detail_item_data in self.detail:
17-
detail_item = detail_item_data.to_dict()
16+
detail: Union[Unset, List[Any]] = UNSET
17+
if not isinstance(self.detail, Unset):
18+
detail = []
19+
for detail_item_data in self.detail:
20+
detail_item = detail_item_data.to_dict()
1821

19-
detail.append(detail_item)
22+
detail.append(detail_item)
2023

21-
field_dict = {
22-
"detail": detail,
23-
}
24+
field_dict = {}
25+
if detail is not UNSET:
26+
field_dict["detail"] = detail
2427

2528
return field_dict
2629

2730
@staticmethod
2831
def from_dict(d: Dict[str, Any]) -> "HTTPValidationError":
2932
detail = []
30-
for detail_item_data in d["detail"]:
33+
for detail_item_data in d.get("detail", UNSET) or []:
3134
detail_item = ValidationError.from_dict(detail_item_data)
3235

3336
detail.append(detail_item)

end_to_end_tests/golden-record/my_test_api_client/models/test_inline_objectsjson_body.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
from typing import Any, Dict
1+
from typing import Any, Dict, Union
22

33
import attr
44

5+
from ..types import UNSET, Unset
6+
57

68
@attr.s(auto_attribs=True)
79
class TestInlineObjectsjsonBody:
810
""" """
911

10-
a_property: str
12+
a_property: Union[Unset, str] = UNSET
1113

1214
def to_dict(self) -> Dict[str, Any]:
1315
a_property = self.a_property
1416

15-
field_dict = {
16-
"a_property": a_property,
17-
}
17+
field_dict = {}
18+
if a_property is not UNSET:
19+
field_dict["a_property"] = a_property
1820

1921
return field_dict
2022

2123
@staticmethod
2224
def from_dict(d: Dict[str, Any]) -> "TestInlineObjectsjsonBody":
23-
a_property = d["a_property"]
25+
a_property = d.get("a_property", UNSET)
2426

2527
return TestInlineObjectsjsonBody(
2628
a_property=a_property,

0 commit comments

Comments
 (0)