Skip to content

Commit a7b6d47

Browse files
packygbowenwr
authored andcommitted
Add allOf support for model definitions (#98)
Collapses the child elements into one, without class heirarchy, mixins, etc.
1 parent aa6972c commit a7b6d47

File tree

15 files changed

+610
-15
lines changed

15 files changed

+610
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- `none` will not create a project folder at all, only the inner package folder (which won't be inner anymore)
1717
- Attempt to detect and alert users if they are using an unsupported version of OpenAPI (#281).
1818
- Fixes `Enum` deserialization when the value is `UNSET`.
19+
- Basic support for `allOf` in models (#98)
1920

2021
### Changes
2122

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
""" Contains all the data models used in inputs/outputs """
22

33
from .a_model import AModel
4+
from .all_of_sub_model import AllOfSubModel
45
from .an_enum import AnEnum
56
from .an_int_enum import AnIntEnum
7+
from .another_all_of_sub_model import AnotherAllOfSubModel
68
from .body_upload_file_tests_upload_post import BodyUploadFileTestsUploadPost
79
from .different_enum import DifferentEnum
810
from .free_form_model import FreeFormModel
911
from .http_validation_error import HTTPValidationError
12+
from .model_from_all_of import ModelFromAllOf
1013
from .model_with_additional_properties_inlined import ModelWithAdditionalPropertiesInlined
1114
from .model_with_additional_properties_inlined_additional_property import (
1215
ModelWithAdditionalPropertiesInlinedAdditionalProperty,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import Any, Dict, List, Type, TypeVar, Union
2+
3+
import attr
4+
5+
from ..types import UNSET, Unset
6+
7+
T = TypeVar("T", bound="AllOfSubModel")
8+
9+
10+
@attr.s(auto_attribs=True)
11+
class AllOfSubModel:
12+
""" """
13+
14+
a_sub_property: Union[Unset, str] = UNSET
15+
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
16+
17+
def to_dict(self) -> Dict[str, Any]:
18+
a_sub_property = self.a_sub_property
19+
20+
field_dict: Dict[str, Any] = {}
21+
field_dict.update(self.additional_properties)
22+
field_dict.update({})
23+
if a_sub_property is not UNSET:
24+
field_dict["a_sub_property"] = a_sub_property
25+
26+
return field_dict
27+
28+
@classmethod
29+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
30+
d = src_dict.copy()
31+
a_sub_property = d.pop("a_sub_property", UNSET)
32+
33+
all_of_sub_model = cls(
34+
a_sub_property=a_sub_property,
35+
)
36+
37+
all_of_sub_model.additional_properties = d
38+
return all_of_sub_model
39+
40+
@property
41+
def additional_keys(self) -> List[str]:
42+
return list(self.additional_properties.keys())
43+
44+
def __getitem__(self, key: str) -> Any:
45+
return self.additional_properties[key]
46+
47+
def __setitem__(self, key: str, value: Any) -> None:
48+
self.additional_properties[key] = value
49+
50+
def __delitem__(self, key: str) -> None:
51+
del self.additional_properties[key]
52+
53+
def __contains__(self, key: str) -> bool:
54+
return key in self.additional_properties
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import Any, Dict, List, Type, TypeVar, Union
2+
3+
import attr
4+
5+
from ..types import UNSET, Unset
6+
7+
T = TypeVar("T", bound="AnotherAllOfSubModel")
8+
9+
10+
@attr.s(auto_attribs=True)
11+
class AnotherAllOfSubModel:
12+
""" """
13+
14+
another_sub_property: Union[Unset, str] = UNSET
15+
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
16+
17+
def to_dict(self) -> Dict[str, Any]:
18+
another_sub_property = self.another_sub_property
19+
20+
field_dict: Dict[str, Any] = {}
21+
field_dict.update(self.additional_properties)
22+
field_dict.update({})
23+
if another_sub_property is not UNSET:
24+
field_dict["another_sub_property"] = another_sub_property
25+
26+
return field_dict
27+
28+
@classmethod
29+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
30+
d = src_dict.copy()
31+
another_sub_property = d.pop("another_sub_property", UNSET)
32+
33+
another_all_of_sub_model = cls(
34+
another_sub_property=another_sub_property,
35+
)
36+
37+
another_all_of_sub_model.additional_properties = d
38+
return another_all_of_sub_model
39+
40+
@property
41+
def additional_keys(self) -> List[str]:
42+
return list(self.additional_properties.keys())
43+
44+
def __getitem__(self, key: str) -> Any:
45+
return self.additional_properties[key]
46+
47+
def __setitem__(self, key: str, value: Any) -> None:
48+
self.additional_properties[key] = value
49+
50+
def __delitem__(self, key: str) -> None:
51+
del self.additional_properties[key]
52+
53+
def __contains__(self, key: str) -> bool:
54+
return key in self.additional_properties
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from typing import Any, Dict, List, Type, TypeVar, Union
2+
3+
import attr
4+
5+
from ..types import UNSET, Unset
6+
7+
T = TypeVar("T", bound="ModelFromAllOf")
8+
9+
10+
@attr.s(auto_attribs=True)
11+
class ModelFromAllOf:
12+
""" """
13+
14+
another_sub_property: Union[Unset, str] = UNSET
15+
a_sub_property: Union[Unset, str] = UNSET
16+
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
17+
18+
def to_dict(self) -> Dict[str, Any]:
19+
another_sub_property = self.another_sub_property
20+
a_sub_property = self.a_sub_property
21+
22+
field_dict: Dict[str, Any] = {}
23+
field_dict.update(self.additional_properties)
24+
field_dict.update({})
25+
if another_sub_property is not UNSET:
26+
field_dict["another_sub_property"] = another_sub_property
27+
if a_sub_property is not UNSET:
28+
field_dict["a_sub_property"] = a_sub_property
29+
30+
return field_dict
31+
32+
@classmethod
33+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
34+
d = src_dict.copy()
35+
another_sub_property = d.pop("another_sub_property", UNSET)
36+
37+
a_sub_property = d.pop("a_sub_property", UNSET)
38+
39+
model_from_all_of = cls(
40+
another_sub_property=another_sub_property,
41+
a_sub_property=a_sub_property,
42+
)
43+
44+
model_from_all_of.additional_properties = d
45+
return model_from_all_of
46+
47+
@property
48+
def additional_keys(self) -> List[str]:
49+
return list(self.additional_properties.keys())
50+
51+
def __getitem__(self, key: str) -> Any:
52+
return self.additional_properties[key]
53+
54+
def __setitem__(self, key: str, value: Any) -> None:
55+
self.additional_properties[key] = value
56+
57+
def __delitem__(self, key: str) -> None:
58+
del self.additional_properties[key]
59+
60+
def __contains__(self, key: str) -> bool:
61+
return key in self.additional_properties

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
""" Contains all the data models used in inputs/outputs """
22

33
from .a_model import AModel
4+
from .all_of_sub_model import AllOfSubModel
45
from .an_enum import AnEnum
56
from .an_int_enum import AnIntEnum
7+
from .another_all_of_sub_model import AnotherAllOfSubModel
68
from .body_upload_file_tests_upload_post import BodyUploadFileTestsUploadPost
79
from .different_enum import DifferentEnum
810
from .free_form_model import FreeFormModel
911
from .http_validation_error import HTTPValidationError
12+
from .model_from_all_of import ModelFromAllOf
1013
from .model_with_additional_properties_inlined import ModelWithAdditionalPropertiesInlined
1114
from .model_with_additional_properties_inlined_additional_property import (
1215
ModelWithAdditionalPropertiesInlinedAdditionalProperty,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import Any, Dict, List, Type, TypeVar, Union
2+
3+
import attr
4+
5+
from ..types import UNSET, Unset
6+
7+
T = TypeVar("T", bound="AllOfSubModel")
8+
9+
10+
@attr.s(auto_attribs=True)
11+
class AllOfSubModel:
12+
""" """
13+
14+
a_sub_property: Union[Unset, str] = UNSET
15+
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
16+
17+
def to_dict(self) -> Dict[str, Any]:
18+
a_sub_property = self.a_sub_property
19+
20+
field_dict: Dict[str, Any] = {}
21+
field_dict.update(self.additional_properties)
22+
field_dict.update({})
23+
if a_sub_property is not UNSET:
24+
field_dict["a_sub_property"] = a_sub_property
25+
26+
return field_dict
27+
28+
@classmethod
29+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
30+
d = src_dict.copy()
31+
a_sub_property = d.pop("a_sub_property", UNSET)
32+
33+
all_of_sub_model = cls(
34+
a_sub_property=a_sub_property,
35+
)
36+
37+
all_of_sub_model.additional_properties = d
38+
return all_of_sub_model
39+
40+
@property
41+
def additional_keys(self) -> List[str]:
42+
return list(self.additional_properties.keys())
43+
44+
def __getitem__(self, key: str) -> Any:
45+
return self.additional_properties[key]
46+
47+
def __setitem__(self, key: str, value: Any) -> None:
48+
self.additional_properties[key] = value
49+
50+
def __delitem__(self, key: str) -> None:
51+
del self.additional_properties[key]
52+
53+
def __contains__(self, key: str) -> bool:
54+
return key in self.additional_properties
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import Any, Dict, List, Type, TypeVar, Union
2+
3+
import attr
4+
5+
from ..types import UNSET, Unset
6+
7+
T = TypeVar("T", bound="AnotherAllOfSubModel")
8+
9+
10+
@attr.s(auto_attribs=True)
11+
class AnotherAllOfSubModel:
12+
""" """
13+
14+
another_sub_property: Union[Unset, str] = UNSET
15+
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
16+
17+
def to_dict(self) -> Dict[str, Any]:
18+
another_sub_property = self.another_sub_property
19+
20+
field_dict: Dict[str, Any] = {}
21+
field_dict.update(self.additional_properties)
22+
field_dict.update({})
23+
if another_sub_property is not UNSET:
24+
field_dict["another_sub_property"] = another_sub_property
25+
26+
return field_dict
27+
28+
@classmethod
29+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
30+
d = src_dict.copy()
31+
another_sub_property = d.pop("another_sub_property", UNSET)
32+
33+
another_all_of_sub_model = cls(
34+
another_sub_property=another_sub_property,
35+
)
36+
37+
another_all_of_sub_model.additional_properties = d
38+
return another_all_of_sub_model
39+
40+
@property
41+
def additional_keys(self) -> List[str]:
42+
return list(self.additional_properties.keys())
43+
44+
def __getitem__(self, key: str) -> Any:
45+
return self.additional_properties[key]
46+
47+
def __setitem__(self, key: str, value: Any) -> None:
48+
self.additional_properties[key] = value
49+
50+
def __delitem__(self, key: str) -> None:
51+
del self.additional_properties[key]
52+
53+
def __contains__(self, key: str) -> bool:
54+
return key in self.additional_properties
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from typing import Any, Dict, List, Type, TypeVar, Union
2+
3+
import attr
4+
5+
from ..types import UNSET, Unset
6+
7+
T = TypeVar("T", bound="ModelFromAllOf")
8+
9+
10+
@attr.s(auto_attribs=True)
11+
class ModelFromAllOf:
12+
""" """
13+
14+
another_sub_property: Union[Unset, str] = UNSET
15+
a_sub_property: Union[Unset, str] = UNSET
16+
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
17+
18+
def to_dict(self) -> Dict[str, Any]:
19+
another_sub_property = self.another_sub_property
20+
a_sub_property = self.a_sub_property
21+
22+
field_dict: Dict[str, Any] = {}
23+
field_dict.update(self.additional_properties)
24+
field_dict.update({})
25+
if another_sub_property is not UNSET:
26+
field_dict["another_sub_property"] = another_sub_property
27+
if a_sub_property is not UNSET:
28+
field_dict["a_sub_property"] = a_sub_property
29+
30+
return field_dict
31+
32+
@classmethod
33+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
34+
d = src_dict.copy()
35+
another_sub_property = d.pop("another_sub_property", UNSET)
36+
37+
a_sub_property = d.pop("a_sub_property", UNSET)
38+
39+
model_from_all_of = cls(
40+
another_sub_property=another_sub_property,
41+
a_sub_property=a_sub_property,
42+
)
43+
44+
model_from_all_of.additional_properties = d
45+
return model_from_all_of
46+
47+
@property
48+
def additional_keys(self) -> List[str]:
49+
return list(self.additional_properties.keys())
50+
51+
def __getitem__(self, key: str) -> Any:
52+
return self.additional_properties[key]
53+
54+
def __setitem__(self, key: str, value: Any) -> None:
55+
self.additional_properties[key] = value
56+
57+
def __delitem__(self, key: str) -> None:
58+
del self.additional_properties[key]
59+
60+
def __contains__(self, key: str) -> bool:
61+
return key in self.additional_properties

0 commit comments

Comments
 (0)