Skip to content

Commit 7af4593

Browse files
docs: Improve docstring in to_dict() method (#16777)
It was often confusing to me why this method is even there. Also using more line breaks and matching how black would format this.
1 parent 1bbbb73 commit 7af4593

149 files changed

Lines changed: 2554 additions & 979 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/resources/python/model_generic.mustache

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,34 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
122122
"""Create an instance of {{{classname}}} from a JSON string"""
123123
return cls.from_dict(json.loads(json_str))
124124

125-
def to_dict(self):
126-
"""Returns the dictionary representation of the model using alias"""
127-
_dict = self.model_dump(by_alias=True,
128-
exclude={
129-
{{#vendorExtensions.x-py-readonly}}
130-
"{{{.}}}",
131-
{{/vendorExtensions.x-py-readonly}}
132-
{{#isAdditionalPropertiesTrue}}
133-
"additional_properties"
134-
{{/isAdditionalPropertiesTrue}}
135-
},
136-
exclude_none=True)
125+
def to_dict(self) -> Dict[str, Any]:
126+
"""Return the dictionary representation of the model using alias.
127+
128+
This has the following differences from calling pydantic's
129+
`self.model_dump(by_alias=True)`:
130+
131+
* `None` is only added to the output dict for nullable fields that
132+
were set at model initialization. Other fields with value `None`
133+
are ignored.
134+
{{#vendorExtensions.x-py-readonly}}
135+
* OpenAPI `readOnly` fields are excluded.
136+
{{/vendorExtensions.x-py-readonly}}
137+
{{#isAdditionalPropertiesTrue}}
138+
* Fields in `self.additional_properties` are added to the output dict.
139+
{{/isAdditionalPropertiesTrue}}
140+
"""
141+
_dict = self.model_dump(
142+
by_alias=True,
143+
exclude={
144+
{{#vendorExtensions.x-py-readonly}}
145+
"{{{.}}}",
146+
{{/vendorExtensions.x-py-readonly}}
147+
{{#isAdditionalPropertiesTrue}}
148+
"additional_properties",
149+
{{/isAdditionalPropertiesTrue}}
150+
},
151+
exclude_none=True,
152+
)
137153
{{#allVars}}
138154
{{#isContainer}}
139155
{{#isArray}}

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/models/bird.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,22 @@ def from_json(cls, json_str: str) -> Self:
5555
"""Create an instance of Bird from a JSON string"""
5656
return cls.from_dict(json.loads(json_str))
5757

58-
def to_dict(self):
59-
"""Returns the dictionary representation of the model using alias"""
60-
_dict = self.model_dump(by_alias=True,
61-
exclude={
62-
},
63-
exclude_none=True)
58+
def to_dict(self) -> Dict[str, Any]:
59+
"""Return the dictionary representation of the model using alias.
60+
61+
This has the following differences from calling pydantic's
62+
`self.model_dump(by_alias=True)`:
63+
64+
* `None` is only added to the output dict for nullable fields that
65+
were set at model initialization. Other fields with value `None`
66+
are ignored.
67+
"""
68+
_dict = self.model_dump(
69+
by_alias=True,
70+
exclude={
71+
},
72+
exclude_none=True,
73+
)
6474
return _dict
6575

6676
@classmethod

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/models/category.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,22 @@ def from_json(cls, json_str: str) -> Self:
5555
"""Create an instance of Category from a JSON string"""
5656
return cls.from_dict(json.loads(json_str))
5757

58-
def to_dict(self):
59-
"""Returns the dictionary representation of the model using alias"""
60-
_dict = self.model_dump(by_alias=True,
61-
exclude={
62-
},
63-
exclude_none=True)
58+
def to_dict(self) -> Dict[str, Any]:
59+
"""Return the dictionary representation of the model using alias.
60+
61+
This has the following differences from calling pydantic's
62+
`self.model_dump(by_alias=True)`:
63+
64+
* `None` is only added to the output dict for nullable fields that
65+
were set at model initialization. Other fields with value `None`
66+
are ignored.
67+
"""
68+
_dict = self.model_dump(
69+
by_alias=True,
70+
exclude={
71+
},
72+
exclude_none=True,
73+
)
6474
return _dict
6575

6676
@classmethod

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/models/data_query.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,22 @@ def from_json(cls, json_str: str) -> Self:
5858
"""Create an instance of DataQuery from a JSON string"""
5959
return cls.from_dict(json.loads(json_str))
6060

61-
def to_dict(self):
62-
"""Returns the dictionary representation of the model using alias"""
63-
_dict = self.model_dump(by_alias=True,
64-
exclude={
65-
},
66-
exclude_none=True)
61+
def to_dict(self) -> Dict[str, Any]:
62+
"""Return the dictionary representation of the model using alias.
63+
64+
This has the following differences from calling pydantic's
65+
`self.model_dump(by_alias=True)`:
66+
67+
* `None` is only added to the output dict for nullable fields that
68+
were set at model initialization. Other fields with value `None`
69+
are ignored.
70+
"""
71+
_dict = self.model_dump(
72+
by_alias=True,
73+
exclude={
74+
},
75+
exclude_none=True,
76+
)
6777
return _dict
6878

6979
@classmethod

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/models/default_value.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,22 @@ def from_json(cls, json_str: str) -> Self:
7373
"""Create an instance of DefaultValue from a JSON string"""
7474
return cls.from_dict(json.loads(json_str))
7575

76-
def to_dict(self):
77-
"""Returns the dictionary representation of the model using alias"""
78-
_dict = self.model_dump(by_alias=True,
79-
exclude={
80-
},
81-
exclude_none=True)
76+
def to_dict(self) -> Dict[str, Any]:
77+
"""Return the dictionary representation of the model using alias.
78+
79+
This has the following differences from calling pydantic's
80+
`self.model_dump(by_alias=True)`:
81+
82+
* `None` is only added to the output dict for nullable fields that
83+
were set at model initialization. Other fields with value `None`
84+
are ignored.
85+
"""
86+
_dict = self.model_dump(
87+
by_alias=True,
88+
exclude={
89+
},
90+
exclude_none=True,
91+
)
8292
# set to None if array_string_nullable (nullable) is None
8393
# and model_fields_set contains the field
8494
if self.array_string_nullable is None and "array_string_nullable" in self.model_fields_set:

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/models/number_properties_only.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,22 @@ def from_json(cls, json_str: str) -> Self:
5858
"""Create an instance of NumberPropertiesOnly from a JSON string"""
5959
return cls.from_dict(json.loads(json_str))
6060

61-
def to_dict(self):
62-
"""Returns the dictionary representation of the model using alias"""
63-
_dict = self.model_dump(by_alias=True,
64-
exclude={
65-
},
66-
exclude_none=True)
61+
def to_dict(self) -> Dict[str, Any]:
62+
"""Return the dictionary representation of the model using alias.
63+
64+
This has the following differences from calling pydantic's
65+
`self.model_dump(by_alias=True)`:
66+
67+
* `None` is only added to the output dict for nullable fields that
68+
were set at model initialization. Other fields with value `None`
69+
are ignored.
70+
"""
71+
_dict = self.model_dump(
72+
by_alias=True,
73+
exclude={
74+
},
75+
exclude_none=True,
76+
)
6777
return _dict
6878

6979
@classmethod

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/models/pet.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,22 @@ def from_json(cls, json_str: str) -> Self:
7272
"""Create an instance of Pet from a JSON string"""
7373
return cls.from_dict(json.loads(json_str))
7474

75-
def to_dict(self):
76-
"""Returns the dictionary representation of the model using alias"""
77-
_dict = self.model_dump(by_alias=True,
78-
exclude={
79-
},
80-
exclude_none=True)
75+
def to_dict(self) -> Dict[str, Any]:
76+
"""Return the dictionary representation of the model using alias.
77+
78+
This has the following differences from calling pydantic's
79+
`self.model_dump(by_alias=True)`:
80+
81+
* `None` is only added to the output dict for nullable fields that
82+
were set at model initialization. Other fields with value `None`
83+
are ignored.
84+
"""
85+
_dict = self.model_dump(
86+
by_alias=True,
87+
exclude={
88+
},
89+
exclude_none=True,
90+
)
8191
# override the default output from pydantic by calling `to_dict()` of category
8292
if self.category:
8393
_dict['category'] = self.category.to_dict()

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/models/query.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,22 @@ def from_json(cls, json_str: str) -> Self:
6767
"""Create an instance of Query from a JSON string"""
6868
return cls.from_dict(json.loads(json_str))
6969

70-
def to_dict(self):
71-
"""Returns the dictionary representation of the model using alias"""
72-
_dict = self.model_dump(by_alias=True,
73-
exclude={
74-
},
75-
exclude_none=True)
70+
def to_dict(self) -> Dict[str, Any]:
71+
"""Return the dictionary representation of the model using alias.
72+
73+
This has the following differences from calling pydantic's
74+
`self.model_dump(by_alias=True)`:
75+
76+
* `None` is only added to the output dict for nullable fields that
77+
were set at model initialization. Other fields with value `None`
78+
are ignored.
79+
"""
80+
_dict = self.model_dump(
81+
by_alias=True,
82+
exclude={
83+
},
84+
exclude_none=True,
85+
)
7686
return _dict
7787

7888
@classmethod

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/models/tag.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,22 @@ def from_json(cls, json_str: str) -> Self:
5555
"""Create an instance of Tag from a JSON string"""
5656
return cls.from_dict(json.loads(json_str))
5757

58-
def to_dict(self):
59-
"""Returns the dictionary representation of the model using alias"""
60-
_dict = self.model_dump(by_alias=True,
61-
exclude={
62-
},
63-
exclude_none=True)
58+
def to_dict(self) -> Dict[str, Any]:
59+
"""Return the dictionary representation of the model using alias.
60+
61+
This has the following differences from calling pydantic's
62+
`self.model_dump(by_alias=True)`:
63+
64+
* `None` is only added to the output dict for nullable fields that
65+
were set at model initialization. Other fields with value `None`
66+
are ignored.
67+
"""
68+
_dict = self.model_dump(
69+
by_alias=True,
70+
exclude={
71+
},
72+
exclude_none=True,
73+
)
6474
return _dict
6575

6676
@classmethod

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/models/test_query_style_deep_object_explode_true_object_all_of_query_object_parameter.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,22 @@ def from_json(cls, json_str: str) -> Self:
5757
"""Create an instance of TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter from a JSON string"""
5858
return cls.from_dict(json.loads(json_str))
5959

60-
def to_dict(self):
61-
"""Returns the dictionary representation of the model using alias"""
62-
_dict = self.model_dump(by_alias=True,
63-
exclude={
64-
},
65-
exclude_none=True)
60+
def to_dict(self) -> Dict[str, Any]:
61+
"""Return the dictionary representation of the model using alias.
62+
63+
This has the following differences from calling pydantic's
64+
`self.model_dump(by_alias=True)`:
65+
66+
* `None` is only added to the output dict for nullable fields that
67+
were set at model initialization. Other fields with value `None`
68+
are ignored.
69+
"""
70+
_dict = self.model_dump(
71+
by_alias=True,
72+
exclude={
73+
},
74+
exclude_none=True,
75+
)
6676
return _dict
6777

6878
@classmethod

0 commit comments

Comments
 (0)