Skip to content

Commit 279d739

Browse files
committed
fix model_<anyof/oneof>.mustache
add to_json / to_dict/ to_str ./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
1 parent e5b2cb5 commit 279d739

28 files changed

Lines changed: 392 additions & 107 deletions

File tree

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
from __future__ import annotations
2-
import re # noqa: F401
3-
{{#vendorExtensions.x-py-other-imports}}
4-
{{{.}}}
5-
{{/vendorExtensions.x-py-other-imports}}
2+
import pprint
63
{{#vendorExtensions.x-py-model-imports}}
74
{{{.}}}
85
{{/vendorExtensions.x-py-model-imports}}
9-
from typing import Union
106
from pydantic import Field, RootModel
7+
from typing import Any, Dict, Optional, Union
118

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

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

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

36+
def to_json(self) -> str:
37+
"""Returns the JSON representation of the actual instance"""
38+
return self.model_dump_json(by_alias=True)
39+
40+
def to_dict(self) -> Optional[Union[Dict[str, Any], {{#anyOf}}{{.}}{{^-last}}, {{/-last}}{{/anyOf}}]]:
41+
"""Returns the dict representation of the actual instance"""
42+
return self.model_dump(by_alias=True)
43+
44+
def to_str(self) -> str:
45+
"""Returns the string representation of the actual instance"""
46+
return pprint.pformat(self.model_dump(by_alias=True, mode="json"))
47+
3948
{{#vendorExtensions.x-py-postponed-model-imports.size}}
4049
{{#vendorExtensions.x-py-postponed-model-imports}}
4150
{{{.}}}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from __future__ import annotations
2-
{{#vendorExtensions.x-py-other-imports}}
3-
{{{.}}}
4-
{{/vendorExtensions.x-py-other-imports}}
2+
import pprint
53
{{#vendorExtensions.x-py-model-imports}}
64
{{{.}}}
75
{{/vendorExtensions.x-py-model-imports}}
86
from pydantic import Field, RootModel
9-
from typing import Union
7+
from typing import Any, Dict, Optional, Union
108

119
{{#lambda.uppercase}}{{{classname}}}{{/lambda.uppercase}}_ONE_OF_SCHEMAS = [{{#oneOf}}"{{.}}"{{^-last}}, {{/-last}}{{/oneOf}}]
1210

@@ -32,3 +30,15 @@ class {{classname}}(RootModel[Union[{{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/on
3230
return getattr(root, name)
3331

3432
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")
33+
34+
def to_json(self) -> str:
35+
"""Returns the JSON representation of the actual instance"""
36+
return self.model_dump_json(by_alias=True)
37+
38+
def to_dict(self) -> Optional[Union[Dict[str, Any], {{#anyOf}}{{.}}{{^-last}}, {{/-last}}{{/anyOf}}]]:
39+
"""Returns the dict representation of the actual instance"""
40+
return self.model_dump(by_alias=True)
41+
42+
def to_str(self) -> str:
43+
"""Returns the string representation of the actual instance"""
44+
return pprint.pformat(self.model_dump(by_alias=True, mode="json"))

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@
1313

1414

1515
from __future__ import annotations
16-
import re # noqa: F401
17-
from pydantic import BaseModel, ConfigDict, Field, StrictStr, ValidationError, field_validator
18-
from typing import List, Optional
19-
from typing_extensions import Annotated
20-
from typing import Union
16+
import pprint
2117
from pydantic import Field, RootModel
18+
from typing import Any, Dict, Optional, Union
2219

2320
ANYOFCOLOR_ANY_OF_SCHEMAS = ["List[int]", "str"]
2421

@@ -47,4 +44,16 @@ def __getattr__(self, name):
4744

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

47+
def to_json(self) -> str:
48+
"""Returns the JSON representation of the actual instance"""
49+
return self.model_dump_json(by_alias=True)
50+
51+
def to_dict(self) -> Optional[Union[Dict[str, Any], List[int], str]]:
52+
"""Returns the dict representation of the actual instance"""
53+
return self.model_dump(by_alias=True)
54+
55+
def to_str(self) -> str:
56+
"""Returns the string representation of the actual instance"""
57+
return pprint.pformat(self.model_dump(by_alias=True, mode="json"))
58+
5059

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313

1414

1515
from __future__ import annotations
16-
import re # noqa: F401
17-
from pydantic import BaseModel, ConfigDict, Field, StrictStr, ValidationError, field_validator
18-
from typing import Optional
16+
import pprint
1917
from petstore_api.models.basque_pig import BasquePig
2018
from petstore_api.models.danish_pig import DanishPig
21-
from typing import Union
2219
from pydantic import Field, RootModel
20+
from typing import Any, Dict, Optional, Union
2321

2422
ANYOFPIG_ANY_OF_SCHEMAS = ["BasquePig", "DanishPig"]
2523

@@ -48,4 +46,16 @@ def __getattr__(self, name):
4846

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

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) -> Optional[Union[Dict[str, Any], BasquePig, DanishPig]]:
54+
"""Returns the dict representation of the actual instance"""
55+
return self.model_dump(by_alias=True)
56+
57+
def to_str(self) -> str:
58+
"""Returns the string representation of the actual instance"""
59+
return pprint.pformat(self.model_dump(by_alias=True, mode="json"))
60+
5161

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313

1414

1515
from __future__ import annotations
16-
from pydantic import BaseModel, ConfigDict, Field, StrictStr, ValidationError, field_validator
17-
from typing import Any, List, Optional
18-
from typing_extensions import Annotated
16+
import pprint
1917
from pydantic import Field, RootModel
20-
from typing import Union
18+
from typing import Any, Dict, Optional, Union
2119

2220
COLOR_ONE_OF_SCHEMAS = ["List[int]", "str"]
2321

@@ -44,3 +42,15 @@ def __getattr__(self, name):
4442

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

45+
def to_json(self) -> str:
46+
"""Returns the JSON representation of the actual instance"""
47+
return self.model_dump_json(by_alias=True)
48+
49+
def to_dict(self) -> Optional[Union[Dict[str, Any], ]]:
50+
"""Returns the dict representation of the actual instance"""
51+
return self.model_dump(by_alias=True)
52+
53+
def to_str(self) -> str:
54+
"""Returns the string representation of the actual instance"""
55+
return pprint.pformat(self.model_dump(by_alias=True, mode="json"))
56+

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313

1414

1515
from __future__ import annotations
16-
from pydantic import BaseModel, ConfigDict, Field, StrictStr, ValidationError, field_validator
17-
from typing import Any, List, Optional
18-
from typing_extensions import Annotated
16+
import pprint
1917
from pydantic import Field, RootModel
20-
from typing import Union
18+
from typing import Any, Dict, Optional, Union
2119

2220
INTORSTRING_ONE_OF_SCHEMAS = ["int", "str"]
2321

@@ -44,3 +42,15 @@ def __getattr__(self, name):
4442

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

45+
def to_json(self) -> str:
46+
"""Returns the JSON representation of the actual instance"""
47+
return self.model_dump_json(by_alias=True)
48+
49+
def to_dict(self) -> Optional[Union[Dict[str, Any], ]]:
50+
"""Returns the dict representation of the actual instance"""
51+
return self.model_dump(by_alias=True)
52+
53+
def to_str(self) -> str:
54+
"""Returns the string representation of the actual instance"""
55+
return pprint.pformat(self.model_dump(by_alias=True, mode="json"))
56+

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313

1414

1515
from __future__ import annotations
16-
from pydantic import BaseModel, ConfigDict, Field, StrictStr, ValidationError, field_validator
17-
from typing import Any, List, Optional
16+
import pprint
1817
from petstore_api.models.basque_pig import BasquePig
1918
from petstore_api.models.danish_pig import DanishPig
2019
from pydantic import Field, RootModel
21-
from typing import Union
20+
from typing import Any, Dict, Optional, Union
2221

2322
PIG_ONE_OF_SCHEMAS = ["BasquePig", "DanishPig"]
2423

@@ -45,3 +44,15 @@ def __getattr__(self, name):
4544

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

47+
def to_json(self) -> str:
48+
"""Returns the JSON representation of the actual instance"""
49+
return self.model_dump_json(by_alias=True)
50+
51+
def to_dict(self) -> Optional[Union[Dict[str, Any], ]]:
52+
"""Returns the dict representation of the actual instance"""
53+
return self.model_dump(by_alias=True)
54+
55+
def to_str(self) -> str:
56+
"""Returns the string representation of the actual instance"""
57+
return pprint.pformat(self.model_dump(by_alias=True, mode="json"))
58+

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313

1414

1515
from __future__ import annotations
16-
from pydantic import BaseModel, ConfigDict, Field, StrictStr, ValidationError, field_validator
17-
from typing import Any, List, Optional
16+
import pprint
1817
from petstore_api.models.bathing import Bathing
1918
from petstore_api.models.feeding import Feeding
2019
from petstore_api.models.poop_cleaning import PoopCleaning
2120
from pydantic import Field, RootModel
22-
from typing import Union
21+
from typing import Any, Dict, Optional, Union
2322

2423
TASKACTIVITY_ONE_OF_SCHEMAS = ["Bathing", "Feeding", "PoopCleaning"]
2524

@@ -46,3 +45,15 @@ def __getattr__(self, name):
4645

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

48+
def to_json(self) -> str:
49+
"""Returns the JSON representation of the actual instance"""
50+
return self.model_dump_json(by_alias=True)
51+
52+
def to_dict(self) -> Optional[Union[Dict[str, Any], ]]:
53+
"""Returns the dict representation of the actual instance"""
54+
return self.model_dump(by_alias=True)
55+
56+
def to_str(self) -> str:
57+
"""Returns the string representation of the actual instance"""
58+
return pprint.pformat(self.model_dump(by_alias=True, mode="json"))
59+

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@
1313

1414

1515
from __future__ import annotations
16-
import re # noqa: F401
17-
from pydantic import BaseModel, ConfigDict, Field, StrictStr, ValidationError, field_validator
18-
from typing import List, Optional
19-
from typing_extensions import Annotated
20-
from typing import Union
16+
import pprint
2117
from pydantic import Field, RootModel
18+
from typing import Any, Dict, Optional, Union
2219

2320
ANYOFCOLOR_ANY_OF_SCHEMAS = ["List[int]", "str"]
2421

@@ -47,4 +44,16 @@ def __getattr__(self, name):
4744

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

47+
def to_json(self) -> str:
48+
"""Returns the JSON representation of the actual instance"""
49+
return self.model_dump_json(by_alias=True)
50+
51+
def to_dict(self) -> Optional[Union[Dict[str, Any], List[int], str]]:
52+
"""Returns the dict representation of the actual instance"""
53+
return self.model_dump(by_alias=True)
54+
55+
def to_str(self) -> str:
56+
"""Returns the string representation of the actual instance"""
57+
return pprint.pformat(self.model_dump(by_alias=True, mode="json"))
58+
5059

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313

1414

1515
from __future__ import annotations
16-
import re # noqa: F401
17-
from pydantic import BaseModel, ConfigDict, Field, StrictStr, ValidationError, field_validator
18-
from typing import Optional
16+
import pprint
1917
from petstore_api.models.basque_pig import BasquePig
2018
from petstore_api.models.danish_pig import DanishPig
21-
from typing import Union
2219
from pydantic import Field, RootModel
20+
from typing import Any, Dict, Optional, Union
2321

2422
ANYOFPIG_ANY_OF_SCHEMAS = ["BasquePig", "DanishPig"]
2523

@@ -48,4 +46,16 @@ def __getattr__(self, name):
4846

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

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) -> Optional[Union[Dict[str, Any], BasquePig, DanishPig]]:
54+
"""Returns the dict representation of the actual instance"""
55+
return self.model_dump(by_alias=True)
56+
57+
def to_str(self) -> str:
58+
"""Returns the string representation of the actual instance"""
59+
return pprint.pformat(self.model_dump(by_alias=True, mode="json"))
60+
5161

0 commit comments

Comments
 (0)