Skip to content

Commit 3583dd7

Browse files
committed
fix model_<anyof/oneof>.mustache
restore from_dict() / from_json() ./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || ex
1 parent d4cc4f6 commit 3583dd7

29 files changed

Lines changed: 311 additions & 28 deletions

File tree

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import pprint
44
{{{.}}}
55
{{/vendorExtensions.x-py-model-imports}}
66
from pydantic import Field, RootModel
7-
from typing import Any, Dict, Union
7+
from typing import Any, Dict, Union, Self
88

99
{{#lambda.uppercase}}{{{classname}}}{{/lambda.uppercase}}_ANY_OF_SCHEMAS = [{{#anyOf}}"{{.}}"{{^-last}}, {{/-last}}{{/anyOf}}]
1010

@@ -33,6 +33,16 @@ class {{classname}}(RootModel[Union[{{#anyOf}}{{.}}{{^-last}}, {{/-last}}{{/anyO
3333

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

36+
@classmethod
37+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
38+
"""Returns the object represented by the Dict"""
39+
return cls.model_validate(obj)
40+
41+
@classmethod
42+
def from_json(cls, json_str: str) -> Self:
43+
"""Returns the object represented by the json string"""
44+
return cls.model_validate_json(json_str)
45+
3646
def to_json(self) -> str:
3747
"""Returns the JSON representation of the actual instance"""
3848
return self.model_dump_json(by_alias=True)

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import pprint
44
{{{.}}}
55
{{/vendorExtensions.x-py-model-imports}}
66
from pydantic import Field, RootModel
7-
from typing import Any, Dict, Union
7+
from typing import Any, Dict, Union, Self
88

99

1010
{{#lambda.uppercase}}{{{classname}}}{{/lambda.uppercase}}_ONE_OF_SCHEMAS = [{{#oneOf}}"{{.}}"{{^-last}}, {{/-last}}{{/oneOf}}]
@@ -33,6 +33,16 @@ class {{classname}}(RootModel[Union[{{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/on
3333

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

36+
@classmethod
37+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
38+
"""Returns the object represented by the Dict"""
39+
return cls.model_validate(obj)
40+
41+
@classmethod
42+
def from_json(cls, json_str: str) -> Self:
43+
"""Returns the object represented by the json string"""
44+
return cls.model_validate_json(json_str)
45+
3646
def to_json(self) -> str:
3747
"""Returns the JSON representation of the actual instance"""
3848
return self.model_dump_json(by_alias=True)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ dependencies = [
6464
{{#asyncio}}
6565
"aiohttp (>=3.8.4)",
6666
"aiohttp-retry (>=2.8.3)",
67+
{{#httpx}}
68+
httpx = ">= 0.28.1",
69+
{{/httpx}}
6770
{{/asyncio}}
6871
{{#tornado}}
6972
"tornado (>=4.2,<5)",

samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/any_of_color.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from __future__ import annotations
1616
import pprint
1717
from pydantic import Field, RootModel
18-
from typing import Any, Dict, Union
18+
from typing import Any, Dict, Union, Self
1919

2020
ANYOFCOLOR_ANY_OF_SCHEMAS = ["List[int]", "str"]
2121

@@ -44,6 +44,16 @@ def __getattr__(self, name):
4444

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

47+
@classmethod
48+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
49+
"""Returns the object represented by the Dict"""
50+
return cls.model_validate(obj)
51+
52+
@classmethod
53+
def from_json(cls, json_str: str) -> Self:
54+
"""Returns the object represented by the json string"""
55+
return cls.model_validate_json(json_str)
56+
4757
def to_json(self) -> str:
4858
"""Returns the JSON representation of the actual instance"""
4959
return self.model_dump_json(by_alias=True)

samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/any_of_pig.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from petstore_api.models.basque_pig import BasquePig
1818
from petstore_api.models.danish_pig import DanishPig
1919
from pydantic import Field, RootModel
20-
from typing import Any, Dict, Union
20+
from typing import Any, Dict, Union, Self
2121

2222
ANYOFPIG_ANY_OF_SCHEMAS = ["BasquePig", "DanishPig"]
2323

@@ -46,6 +46,16 @@ def __getattr__(self, name):
4646

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

49+
@classmethod
50+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
51+
"""Returns the object represented by the Dict"""
52+
return cls.model_validate(obj)
53+
54+
@classmethod
55+
def from_json(cls, json_str: str) -> Self:
56+
"""Returns the object represented by the json string"""
57+
return cls.model_validate_json(json_str)
58+
4959
def to_json(self) -> str:
5060
"""Returns the JSON representation of the actual instance"""
5161
return self.model_dump_json(by_alias=True)

samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/color.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from __future__ import annotations
1616
import pprint
1717
from pydantic import Field, RootModel
18-
from typing import Any, Dict, Union
18+
from typing import Any, Dict, Union, Self
1919

2020

2121
COLOR_ONE_OF_SCHEMAS = ["List[int]", "str"]
@@ -44,6 +44,16 @@ def __getattr__(self, name):
4444

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

47+
@classmethod
48+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
49+
"""Returns the object represented by the Dict"""
50+
return cls.model_validate(obj)
51+
52+
@classmethod
53+
def from_json(cls, json_str: str) -> Self:
54+
"""Returns the object represented by the json string"""
55+
return cls.model_validate_json(json_str)
56+
4757
def to_json(self) -> str:
4858
"""Returns the JSON representation of the actual instance"""
4959
return self.model_dump_json(by_alias=True)

samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/int_or_string.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from __future__ import annotations
1616
import pprint
1717
from pydantic import Field, RootModel
18-
from typing import Any, Dict, Union
18+
from typing import Any, Dict, Union, Self
1919

2020

2121
INTORSTRING_ONE_OF_SCHEMAS = ["int", "str"]
@@ -44,6 +44,16 @@ def __getattr__(self, name):
4444

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

47+
@classmethod
48+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
49+
"""Returns the object represented by the Dict"""
50+
return cls.model_validate(obj)
51+
52+
@classmethod
53+
def from_json(cls, json_str: str) -> Self:
54+
"""Returns the object represented by the json string"""
55+
return cls.model_validate_json(json_str)
56+
4757
def to_json(self) -> str:
4858
"""Returns the JSON representation of the actual instance"""
4959
return self.model_dump_json(by_alias=True)

samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/pig.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from petstore_api.models.basque_pig import BasquePig
1818
from petstore_api.models.danish_pig import DanishPig
1919
from pydantic import Field, RootModel
20-
from typing import Any, Dict, Union
20+
from typing import Any, Dict, Union, Self
2121

2222

2323
PIG_ONE_OF_SCHEMAS = ["BasquePig", "DanishPig"]
@@ -46,6 +46,16 @@ def __getattr__(self, name):
4646

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

49+
@classmethod
50+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
51+
"""Returns the object represented by the Dict"""
52+
return cls.model_validate(obj)
53+
54+
@classmethod
55+
def from_json(cls, json_str: str) -> Self:
56+
"""Returns the object represented by the json string"""
57+
return cls.model_validate_json(json_str)
58+
4959
def to_json(self) -> str:
5060
"""Returns the JSON representation of the actual instance"""
5161
return self.model_dump_json(by_alias=True)

samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/task_activity.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from petstore_api.models.feeding import Feeding
1919
from petstore_api.models.poop_cleaning import PoopCleaning
2020
from pydantic import Field, RootModel
21-
from typing import Any, Dict, Union
21+
from typing import Any, Dict, Union, Self
2222

2323

2424
TASKACTIVITY_ONE_OF_SCHEMAS = ["Bathing", "Feeding", "PoopCleaning"]
@@ -47,6 +47,16 @@ def __getattr__(self, name):
4747

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

50+
@classmethod
51+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
52+
"""Returns the object represented by the Dict"""
53+
return cls.model_validate(obj)
54+
55+
@classmethod
56+
def from_json(cls, json_str: str) -> Self:
57+
"""Returns the object represented by the json string"""
58+
return cls.model_validate_json(json_str)
59+
5060
def to_json(self) -> str:
5161
"""Returns the JSON representation of the actual instance"""
5262
return self.model_dump_json(by_alias=True)

samples/openapi3/client/petstore/python-httpx/petstore_api/models/any_of_color.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from __future__ import annotations
1616
import pprint
1717
from pydantic import Field, RootModel
18-
from typing import Any, Dict, Union
18+
from typing import Any, Dict, Union, Self
1919

2020
ANYOFCOLOR_ANY_OF_SCHEMAS = ["List[int]", "str"]
2121

@@ -44,6 +44,16 @@ def __getattr__(self, name):
4444

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

47+
@classmethod
48+
def from_dict(cls, obj: Dict[str, Any]) -> Self:
49+
"""Returns the object represented by the Dict"""
50+
return cls.model_validate(obj)
51+
52+
@classmethod
53+
def from_json(cls, json_str: str) -> Self:
54+
"""Returns the object represented by the json string"""
55+
return cls.model_validate_json(json_str)
56+
4757
def to_json(self) -> str:
4858
"""Returns the JSON representation of the actual instance"""
4959
return self.model_dump_json(by_alias=True)

0 commit comments

Comments
 (0)