Skip to content

Commit 9a41599

Browse files
committed
refactor model_generic.mustache
restore from_dict() / from_json()
1 parent 2f6e52f commit 9a41599

383 files changed

Lines changed: 6903 additions & 1158 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: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
from __future__ import annotations
22
import pprint
33
import re # noqa: F401
4-
import json
54

65
{{#vendorExtensions.x-py-other-imports}}
76
{{{.}}}
87
{{/vendorExtensions.x-py-other-imports}}
98
{{#vendorExtensions.x-py-model-imports}}
109
{{{.}}}
1110
{{/vendorExtensions.x-py-model-imports}}
12-
from typing import Optional, Set, Literal
1311
from typing import Self
14-
from pydantic import Field
1512

1613
{{#hasChildren}}
1714
{{#discriminator}}
@@ -36,7 +33,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
3633
{{#-first}}
3734
{{#-last}}
3835
{{! Single value enum - use Literal }}
39-
{{name}}: Literal[{{#isString}}"{{{.}}}"{{/isString}}{{^isString}}{{{.}}}{{/isString}}] = Field(
36+
{{name}}: {{#required}}Literal[{{#isString}}"{{{.}}}"{{/isString}}{{^isString}}{{{.}}}{{/isString}}]{{/required}}{{^required}}Optional[Literal[{{#isString}}"{{{.}}}"{{/isString}}{{^isString}}{{{.}}}{{/isString}}]]{{/required}} = Field(
4037
{{#required}}...{{/required}}{{^required}}None{{/required}},
4138
description="{{description}}{{^description}}{{{name}}} of the {{classname}}{{/description}}",
4239
alias="{{{baseName}}}"
@@ -156,6 +153,24 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
156153

157154
{{/discriminator}}
158155
{{/hasChildren}}
156+
@classmethod
157+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
158+
"""Returns the object represented by the Dict"""
159+
return cls.model_validate(obj)
160+
161+
@classmethod
162+
def from_json(cls, json_str: str) -> Self:
163+
"""Returns the object represented by the json string"""
164+
return cls.model_validate_json(json_str)
165+
166+
def to_json(self) -> str:
167+
"""Returns the JSON representation of the actual instance"""
168+
return self.model_dump_json(by_alias=True)
169+
170+
def to_dict(self) -> Dict[str, Any]:
171+
"""Returns the dict representation of the actual instance"""
172+
return self.model_dump(by_alias=True)
173+
159174
def to_str(self) -> str:
160175
"""Returns the string representation of the model using alias"""
161176
return pprint.pformat(self.model_dump(by_alias=True))

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616
from __future__ import annotations
1717
import pprint
1818
import re # noqa: F401
19-
import json
2019

2120
from pydantic import BaseModel, ConfigDict, StrictStr
2221
from typing import Any, ClassVar, Dict, List, Optional
23-
from typing import Optional, Set, Literal
2422
from typing import Self
25-
from pydantic import Field
2623

2724
class Bird(BaseModel):
2825
"""
@@ -39,6 +36,24 @@ class Bird(BaseModel):
3936
)
4037

4138

39+
@classmethod
40+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
41+
"""Returns the object represented by the Dict"""
42+
return cls.model_validate(obj)
43+
44+
@classmethod
45+
def from_json(cls, json_str: str) -> Self:
46+
"""Returns the object represented by the json string"""
47+
return cls.model_validate_json(json_str)
48+
49+
def to_json(self) -> str:
50+
"""Returns the JSON representation of the actual instance"""
51+
return self.model_dump_json(by_alias=True)
52+
53+
def to_dict(self) -> Dict[str, Any]:
54+
"""Returns the dict representation of the actual instance"""
55+
return self.model_dump(by_alias=True)
56+
4257
def to_str(self) -> str:
4358
"""Returns the string representation of the model using alias"""
4459
return pprint.pformat(self.model_dump(by_alias=True))

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616
from __future__ import annotations
1717
import pprint
1818
import re # noqa: F401
19-
import json
2019

2120
from pydantic import BaseModel, ConfigDict, StrictInt, StrictStr
2221
from typing import Any, ClassVar, Dict, List, Optional
23-
from typing import Optional, Set, Literal
2422
from typing import Self
25-
from pydantic import Field
2623

2724
class Category(BaseModel):
2825
"""
@@ -39,6 +36,24 @@ class Category(BaseModel):
3936
)
4037

4138

39+
@classmethod
40+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
41+
"""Returns the object represented by the Dict"""
42+
return cls.model_validate(obj)
43+
44+
@classmethod
45+
def from_json(cls, json_str: str) -> Self:
46+
"""Returns the object represented by the json string"""
47+
return cls.model_validate_json(json_str)
48+
49+
def to_json(self) -> str:
50+
"""Returns the JSON representation of the actual instance"""
51+
return self.model_dump_json(by_alias=True)
52+
53+
def to_dict(self) -> Dict[str, Any]:
54+
"""Returns the dict representation of the actual instance"""
55+
return self.model_dump(by_alias=True)
56+
4257
def to_str(self) -> str:
4358
"""Returns the string representation of the model using alias"""
4459
return pprint.pformat(self.model_dump(by_alias=True))

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616
from __future__ import annotations
1717
import pprint
1818
import re # noqa: F401
19-
import json
2019

2120
from datetime import datetime
2221
from pydantic import ConfigDict, Field, StrictStr
2322
from typing import Any, ClassVar, Dict, List, Optional
2423
from openapi_client.models.query import Query
25-
from typing import Optional, Set, Literal
2624
from typing import Self
27-
from pydantic import Field
2825

2926
class DataQuery(Query):
3027
"""
@@ -42,6 +39,24 @@ class DataQuery(Query):
4239
)
4340

4441

42+
@classmethod
43+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
44+
"""Returns the object represented by the Dict"""
45+
return cls.model_validate(obj)
46+
47+
@classmethod
48+
def from_json(cls, json_str: str) -> Self:
49+
"""Returns the object represented by the json string"""
50+
return cls.model_validate_json(json_str)
51+
52+
def to_json(self) -> str:
53+
"""Returns the JSON representation of the actual instance"""
54+
return self.model_dump_json(by_alias=True)
55+
56+
def to_dict(self) -> Dict[str, Any]:
57+
"""Returns the dict representation of the actual instance"""
58+
return self.model_dump(by_alias=True)
59+
4560
def to_str(self) -> str:
4661
"""Returns the string representation of the model using alias"""
4762
return pprint.pformat(self.model_dump(by_alias=True))

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@
1616
from __future__ import annotations
1717
import pprint
1818
import re # noqa: F401
19-
import json
2019

2120
from pydantic import BaseModel, ConfigDict, StrictInt, StrictStr, field_validator
2221
from typing import Any, ClassVar, Dict, List, Optional
2322
from openapi_client.models.string_enum_ref import StringEnumRef
24-
from typing import Optional, Set, Literal
2523
from typing import Self
26-
from pydantic import Field
2724

2825
class DefaultValue(BaseModel):
2926
"""
@@ -60,6 +57,24 @@ def array_string_enum_default_validate_enum(cls, value):
6057
)
6158

6259

60+
@classmethod
61+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
62+
"""Returns the object represented by the Dict"""
63+
return cls.model_validate(obj)
64+
65+
@classmethod
66+
def from_json(cls, json_str: str) -> Self:
67+
"""Returns the object represented by the json string"""
68+
return cls.model_validate_json(json_str)
69+
70+
def to_json(self) -> str:
71+
"""Returns the JSON representation of the actual instance"""
72+
return self.model_dump_json(by_alias=True)
73+
74+
def to_dict(self) -> Dict[str, Any]:
75+
"""Returns the dict representation of the actual instance"""
76+
return self.model_dump(by_alias=True)
77+
6378
def to_str(self) -> str:
6479
"""Returns the string representation of the model using alias"""
6580
return pprint.pformat(self.model_dump(by_alias=True))

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@
1616
from __future__ import annotations
1717
import pprint
1818
import re # noqa: F401
19-
import json
2019

2120
from pydantic import BaseModel, ConfigDict, Field, StrictFloat, StrictInt
2221
from typing import Any, ClassVar, Dict, List, Optional, Union
2322
from typing_extensions import Annotated
24-
from typing import Optional, Set, Literal
2523
from typing import Self
26-
from pydantic import Field
2724

2825
class NumberPropertiesOnly(BaseModel):
2926
"""
@@ -41,6 +38,24 @@ class NumberPropertiesOnly(BaseModel):
4138
)
4239

4340

41+
@classmethod
42+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
43+
"""Returns the object represented by the Dict"""
44+
return cls.model_validate(obj)
45+
46+
@classmethod
47+
def from_json(cls, json_str: str) -> Self:
48+
"""Returns the object represented by the json string"""
49+
return cls.model_validate_json(json_str)
50+
51+
def to_json(self) -> str:
52+
"""Returns the JSON representation of the actual instance"""
53+
return self.model_dump_json(by_alias=True)
54+
55+
def to_dict(self) -> Dict[str, Any]:
56+
"""Returns the dict representation of the actual instance"""
57+
return self.model_dump(by_alias=True)
58+
4459
def to_str(self) -> str:
4560
"""Returns the string representation of the model using alias"""
4661
return pprint.pformat(self.model_dump(by_alias=True))

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616
from __future__ import annotations
1717
import pprint
1818
import re # noqa: F401
19-
import json
2019

2120
from pydantic import BaseModel, ConfigDict, Field, StrictInt, StrictStr, field_validator
2221
from typing import Any, ClassVar, Dict, List, Optional
2322
from openapi_client.models.category import Category
2423
from openapi_client.models.tag import Tag
25-
from typing import Optional, Set, Literal
2624
from typing import Self
27-
from pydantic import Field
2825

2926
class Pet(BaseModel):
3027
"""
@@ -58,6 +55,24 @@ def status_validate_enum(cls, value):
5855
)
5956

6057

58+
@classmethod
59+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
60+
"""Returns the object represented by the Dict"""
61+
return cls.model_validate(obj)
62+
63+
@classmethod
64+
def from_json(cls, json_str: str) -> Self:
65+
"""Returns the object represented by the json string"""
66+
return cls.model_validate_json(json_str)
67+
68+
def to_json(self) -> str:
69+
"""Returns the JSON representation of the actual instance"""
70+
return self.model_dump_json(by_alias=True)
71+
72+
def to_dict(self) -> Dict[str, Any]:
73+
"""Returns the dict representation of the actual instance"""
74+
return self.model_dump(by_alias=True)
75+
6176
def to_str(self) -> str:
6277
"""Returns the string representation of the model using alias"""
6378
return pprint.pformat(self.model_dump(by_alias=True))

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616
from __future__ import annotations
1717
import pprint
1818
import re # noqa: F401
19-
import json
2019

2120
from pydantic import BaseModel, ConfigDict, Field, StrictInt, StrictStr, field_validator
2221
from typing import Any, ClassVar, Dict, List, Optional
23-
from typing import Optional, Set, Literal
2422
from typing import Self
25-
from pydantic import Field
2623

2724
class Query(BaseModel):
2825
"""
@@ -53,6 +50,24 @@ def outcomes_validate_enum(cls, value):
5350
)
5451

5552

53+
@classmethod
54+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
55+
"""Returns the object represented by the Dict"""
56+
return cls.model_validate(obj)
57+
58+
@classmethod
59+
def from_json(cls, json_str: str) -> Self:
60+
"""Returns the object represented by the json string"""
61+
return cls.model_validate_json(json_str)
62+
63+
def to_json(self) -> str:
64+
"""Returns the JSON representation of the actual instance"""
65+
return self.model_dump_json(by_alias=True)
66+
67+
def to_dict(self) -> Dict[str, Any]:
68+
"""Returns the dict representation of the actual instance"""
69+
return self.model_dump(by_alias=True)
70+
5671
def to_str(self) -> str:
5772
"""Returns the string representation of the model using alias"""
5873
return pprint.pformat(self.model_dump(by_alias=True))

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616
from __future__ import annotations
1717
import pprint
1818
import re # noqa: F401
19-
import json
2019

2120
from pydantic import BaseModel, ConfigDict, StrictInt, StrictStr
2221
from typing import Any, ClassVar, Dict, List, Optional
23-
from typing import Optional, Set, Literal
2422
from typing import Self
25-
from pydantic import Field
2623

2724
class Tag(BaseModel):
2825
"""
@@ -39,6 +36,24 @@ class Tag(BaseModel):
3936
)
4037

4138

39+
@classmethod
40+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
41+
"""Returns the object represented by the Dict"""
42+
return cls.model_validate(obj)
43+
44+
@classmethod
45+
def from_json(cls, json_str: str) -> Self:
46+
"""Returns the object represented by the json string"""
47+
return cls.model_validate_json(json_str)
48+
49+
def to_json(self) -> str:
50+
"""Returns the JSON representation of the actual instance"""
51+
return self.model_dump_json(by_alias=True)
52+
53+
def to_dict(self) -> Dict[str, Any]:
54+
"""Returns the dict representation of the actual instance"""
55+
return self.model_dump(by_alias=True)
56+
4257
def to_str(self) -> str:
4358
"""Returns the string representation of the model using alias"""
4459
return pprint.pformat(self.model_dump(by_alias=True))

0 commit comments

Comments
 (0)