Skip to content

Commit 1dc5983

Browse files
committed
[FIX] model_generic.mustache
to_str()
1 parent b651020 commit 1dc5983

411 files changed

Lines changed: 2531 additions & 1915 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_anyof.mustache

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,28 @@ class {{classname}}(RootModel[Union[{{#anyOf}}{{.}}{{^-last}}, {{/-last}}{{/anyO
3535

3636
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")
3737

38+
@classmethod
39+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
40+
"""Returns the object represented by the Dict"""
41+
return cls.model_validate(obj, strict=True)
42+
43+
@classmethod
44+
def from_json(cls, json_str: str) -> Self:
45+
"""Returns the object represented by the json string"""
46+
return cls.model_validate_json(json_str)
47+
48+
def to_dict(self, exclude_unset: bool = True) -> Dict[str, Any]:
49+
"""Returns the dict representation of the actual instance"""
50+
return self.model_dump(by_alias=True, exclude_unset=exclude_unset)
51+
52+
def to_json(self, exclude_unset: bool = True) -> str:
53+
"""Returns the JSON representation of the actual instance"""
54+
return json.dumps(self.model_dump(by_alias=True, exclude_unset=exclude_unset, mode="json"))
55+
56+
def to_str(self) -> str:
57+
"""Returns the string representation of the model using alias"""
58+
return pprint.pformat(self.to_dict(exclude_unset=False))
59+
3860
{{#vendorExtensions.x-py-postponed-model-imports.size}}
3961
{{#vendorExtensions.x-py-postponed-model-imports}}
4062
{{{.}}}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,17 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
166166
"""Returns the object represented by the json string"""
167167
return cls.model_validate_json(json_str)
168168

169-
def to_dict(self) -> Dict[str, Any]:
169+
def to_dict(self, exclude_unset: bool = True) -> Dict[str, Any]:
170170
"""Returns the dict representation of the actual instance"""
171-
return self.model_dump(by_alias=True, exclude_unset=True)
171+
return self.model_dump(by_alias=True, exclude_unset=exclude_unset)
172172

173-
def to_json(self) -> str:
173+
def to_json(self, exclude_unset: bool = True) -> str:
174174
"""Returns the JSON representation of the actual instance"""
175-
return json.dumps(self.model_dump(by_alias=True, exclude_unset=True, mode="json"))
175+
return json.dumps(self.model_dump(by_alias=True, exclude_unset=exclude_unset, mode="json"))
176176

177177
def to_str(self) -> str:
178178
"""Returns the string representation of the model using alias"""
179-
return pprint.pformat(self.to_json())
179+
return pprint.pformat(self.to_dict(exclude_unset=False))
180180

181181

182182
{{#vendorExtensions.x-py-postponed-model-imports.size}}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,28 @@ class {{classname}}(RootModel[Union[{{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/on
3535

3636
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")
3737

38+
@classmethod
39+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
40+
"""Returns the object represented by the Dict"""
41+
return cls.model_validate(obj, strict=True)
42+
43+
@classmethod
44+
def from_json(cls, json_str: str) -> Self:
45+
"""Returns the object represented by the json string"""
46+
return cls.model_validate_json(json_str)
47+
48+
def to_dict(self, exclude_unset: bool = True) -> Dict[str, Any]:
49+
"""Returns the dict representation of the actual instance"""
50+
return self.model_dump(by_alias=True, exclude_unset=exclude_unset)
51+
52+
def to_json(self, exclude_unset: bool = True) -> str:
53+
"""Returns the JSON representation of the actual instance"""
54+
return json.dumps(self.model_dump(by_alias=True, exclude_unset=exclude_unset, mode="json"))
55+
56+
def to_str(self) -> str:
57+
"""Returns the string representation of the model using alias"""
58+
return pprint.pformat(self.to_dict(exclude_unset=False))
59+
3860
{{#vendorExtensions.x-py-postponed-model-imports.size}}
3961
{{#vendorExtensions.x-py-postponed-model-imports}}
4062
{{{.}}}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ def from_json(cls, json_str: str) -> Self:
4949
"""Returns the object represented by the json string"""
5050
return cls.model_validate_json(json_str)
5151

52-
def to_dict(self) -> Dict[str, Any]:
52+
def to_dict(self, exclude_unset: bool = True) -> Dict[str, Any]:
5353
"""Returns the dict representation of the actual instance"""
54-
return self.model_dump(by_alias=True, exclude_unset=True)
54+
return self.model_dump(by_alias=True, exclude_unset=exclude_unset)
5555

56-
def to_json(self) -> str:
56+
def to_json(self, exclude_unset: bool = True) -> str:
5757
"""Returns the JSON representation of the actual instance"""
58-
return json.dumps(self.model_dump(by_alias=True, exclude_unset=True, mode="json"))
58+
return json.dumps(self.model_dump(by_alias=True, exclude_unset=exclude_unset, mode="json"))
5959

6060
def to_str(self) -> str:
6161
"""Returns the string representation of the model using alias"""
62-
return pprint.pformat(self.to_json())
62+
return pprint.pformat(self.to_dict(exclude_unset=False))
6363

6464

6565

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ def from_json(cls, json_str: str) -> Self:
4949
"""Returns the object represented by the json string"""
5050
return cls.model_validate_json(json_str)
5151

52-
def to_dict(self) -> Dict[str, Any]:
52+
def to_dict(self, exclude_unset: bool = True) -> Dict[str, Any]:
5353
"""Returns the dict representation of the actual instance"""
54-
return self.model_dump(by_alias=True, exclude_unset=True)
54+
return self.model_dump(by_alias=True, exclude_unset=exclude_unset)
5555

56-
def to_json(self) -> str:
56+
def to_json(self, exclude_unset: bool = True) -> str:
5757
"""Returns the JSON representation of the actual instance"""
58-
return json.dumps(self.model_dump(by_alias=True, exclude_unset=True, mode="json"))
58+
return json.dumps(self.model_dump(by_alias=True, exclude_unset=exclude_unset, mode="json"))
5959

6060
def to_str(self) -> str:
6161
"""Returns the string representation of the model using alias"""
62-
return pprint.pformat(self.to_json())
62+
return pprint.pformat(self.to_dict(exclude_unset=False))
6363

6464

6565

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ def from_json(cls, json_str: str) -> Self:
5252
"""Returns the object represented by the json string"""
5353
return cls.model_validate_json(json_str)
5454

55-
def to_dict(self) -> Dict[str, Any]:
55+
def to_dict(self, exclude_unset: bool = True) -> Dict[str, Any]:
5656
"""Returns the dict representation of the actual instance"""
57-
return self.model_dump(by_alias=True, exclude_unset=True)
57+
return self.model_dump(by_alias=True, exclude_unset=exclude_unset)
5858

59-
def to_json(self) -> str:
59+
def to_json(self, exclude_unset: bool = True) -> str:
6060
"""Returns the JSON representation of the actual instance"""
61-
return json.dumps(self.model_dump(by_alias=True, exclude_unset=True, mode="json"))
61+
return json.dumps(self.model_dump(by_alias=True, exclude_unset=exclude_unset, mode="json"))
6262

6363
def to_str(self) -> str:
6464
"""Returns the string representation of the model using alias"""
65-
return pprint.pformat(self.to_json())
65+
return pprint.pformat(self.to_dict(exclude_unset=False))
6666

6767

6868

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ def from_json(cls, json_str: str) -> Self:
7070
"""Returns the object represented by the json string"""
7171
return cls.model_validate_json(json_str)
7272

73-
def to_dict(self) -> Dict[str, Any]:
73+
def to_dict(self, exclude_unset: bool = True) -> Dict[str, Any]:
7474
"""Returns the dict representation of the actual instance"""
75-
return self.model_dump(by_alias=True, exclude_unset=True)
75+
return self.model_dump(by_alias=True, exclude_unset=exclude_unset)
7676

77-
def to_json(self) -> str:
77+
def to_json(self, exclude_unset: bool = True) -> str:
7878
"""Returns the JSON representation of the actual instance"""
79-
return json.dumps(self.model_dump(by_alias=True, exclude_unset=True, mode="json"))
79+
return json.dumps(self.model_dump(by_alias=True, exclude_unset=exclude_unset, mode="json"))
8080

8181
def to_str(self) -> str:
8282
"""Returns the string representation of the model using alias"""
83-
return pprint.pformat(self.to_json())
83+
return pprint.pformat(self.to_dict(exclude_unset=False))
8484

8585

8686

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,17 @@ def from_json(cls, json_str: str) -> Self:
5151
"""Returns the object represented by the json string"""
5252
return cls.model_validate_json(json_str)
5353

54-
def to_dict(self) -> Dict[str, Any]:
54+
def to_dict(self, exclude_unset: bool = True) -> Dict[str, Any]:
5555
"""Returns the dict representation of the actual instance"""
56-
return self.model_dump(by_alias=True, exclude_unset=True)
56+
return self.model_dump(by_alias=True, exclude_unset=exclude_unset)
5757

58-
def to_json(self) -> str:
58+
def to_json(self, exclude_unset: bool = True) -> str:
5959
"""Returns the JSON representation of the actual instance"""
60-
return json.dumps(self.model_dump(by_alias=True, exclude_unset=True, mode="json"))
60+
return json.dumps(self.model_dump(by_alias=True, exclude_unset=exclude_unset, mode="json"))
6161

6262
def to_str(self) -> str:
6363
"""Returns the string representation of the model using alias"""
64-
return pprint.pformat(self.to_json())
64+
return pprint.pformat(self.to_dict(exclude_unset=False))
6565

6666

6767

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ def from_json(cls, json_str: str) -> Self:
6868
"""Returns the object represented by the json string"""
6969
return cls.model_validate_json(json_str)
7070

71-
def to_dict(self) -> Dict[str, Any]:
71+
def to_dict(self, exclude_unset: bool = True) -> Dict[str, Any]:
7272
"""Returns the dict representation of the actual instance"""
73-
return self.model_dump(by_alias=True, exclude_unset=True)
73+
return self.model_dump(by_alias=True, exclude_unset=exclude_unset)
7474

75-
def to_json(self) -> str:
75+
def to_json(self, exclude_unset: bool = True) -> str:
7676
"""Returns the JSON representation of the actual instance"""
77-
return json.dumps(self.model_dump(by_alias=True, exclude_unset=True, mode="json"))
77+
return json.dumps(self.model_dump(by_alias=True, exclude_unset=exclude_unset, mode="json"))
7878

7979
def to_str(self) -> str:
8080
"""Returns the string representation of the model using alias"""
81-
return pprint.pformat(self.to_json())
81+
return pprint.pformat(self.to_dict(exclude_unset=False))
8282

8383

8484

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,17 @@ def from_json(cls, json_str: str) -> Self:
6363
"""Returns the object represented by the json string"""
6464
return cls.model_validate_json(json_str)
6565

66-
def to_dict(self) -> Dict[str, Any]:
66+
def to_dict(self, exclude_unset: bool = True) -> Dict[str, Any]:
6767
"""Returns the dict representation of the actual instance"""
68-
return self.model_dump(by_alias=True, exclude_unset=True)
68+
return self.model_dump(by_alias=True, exclude_unset=exclude_unset)
6969

70-
def to_json(self) -> str:
70+
def to_json(self, exclude_unset: bool = True) -> str:
7171
"""Returns the JSON representation of the actual instance"""
72-
return json.dumps(self.model_dump(by_alias=True, exclude_unset=True, mode="json"))
72+
return json.dumps(self.model_dump(by_alias=True, exclude_unset=exclude_unset, mode="json"))
7373

7474
def to_str(self) -> str:
7575
"""Returns the string representation of the model using alias"""
76-
return pprint.pformat(self.to_json())
76+
return pprint.pformat(self.to_dict(exclude_unset=False))
7777

7878

7979

0 commit comments

Comments
 (0)