You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[python-fastapi] support oneOf the pydantic v2 way
* Support oneOf and anyOf schemas the pydantic v2 way by generating them as Unions.
* Generate model constructor that forcefully sets the discriminator field to ensure it is included in the marshalled representation.
raise ValueError("If a position argument is used, only 1 is allowed to set `actual_instance`")
57
-
if kwargs:
58
-
raise ValueError("If a position argument is used, keyword arguments cannot be used.")
59
-
super().__init__(actual_instance=args[0])
60
-
else:
61
-
super().__init__(**kwargs)
62
-
63
-
@field_validator('actual_instance')
64
-
def actual_instance_must_validate_anyof(cls, v):
65
-
{{#isNullable}}
66
-
if v is None:
67
-
return v
68
-
69
-
{{/isNullable}}
70
-
instance = {{{classname}}}.model_construct()
71
-
error_messages = []
72
-
{{#composedSchemas.anyOf}}
73
-
# validate data type: {{{dataType}}}
74
-
{{#isContainer}}
75
-
try:
76
-
instance.{{vendorExtensions.x-py-name}} = v
77
-
return v
78
-
except (ValidationError, ValueError) as e:
79
-
error_messages.append(str(e))
80
-
{{/isContainer}}
81
-
{{^isContainer}}
82
-
{{#isPrimitiveType}}
83
-
try:
84
-
instance.{{vendorExtensions.x-py-name}} = v
85
-
return v
86
-
except (ValidationError, ValueError) as e:
87
-
error_messages.append(str(e))
88
-
{{/isPrimitiveType}}
89
-
{{^isPrimitiveType}}
90
-
if not isinstance(v, {{{dataType}}}):
91
-
error_messages.append(f"Error! Input type `{type(v)}` is not `{{{dataType}}}`")
92
-
else:
93
-
return v
94
-
95
-
{{/isPrimitiveType}}
96
-
{{/isContainer}}
97
-
{{/composedSchemas.anyOf}}
98
-
if error_messages:
99
-
# no match
100
-
raise ValueError("No match found when setting the actual_instance in {{{classname}}} with anyOf schemas: {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}. Details: " + ", ".join(error_messages))
101
-
else:
102
-
return v
103
-
104
-
@classmethod
105
-
def from_dict(cls, obj: dict) -> Self:
106
-
return cls.from_json(json.dumps(obj))
107
34
108
-
@classmethod
109
-
def from_json(cls, json_str: str) -> Self:
110
-
"""Returns the object represented by the json string"""
raise ValueError("No match found when deserializing the JSON string into {{{classname}}} with anyOf schemas: {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}. Details: " + ", ".join(error_messages))
156
-
else:
157
-
return instance
35
+
def to_str(self) -> str:
36
+
"""Returns the string representation of the model using alias"""
0 commit comments