Skip to content

Commit 238aad7

Browse files
algolia-botkai687millotp
committed
fix(specs): built-in ops accept also int (generated)
algolia/api-clients-automation#3450 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Kai Welke <[email protected]> Co-authored-by: Pierre Millot <[email protected]>
1 parent 7a43b0e commit 238aad7

File tree

2 files changed

+121
-5
lines changed

2 files changed

+121
-5
lines changed

algoliasearch/search/models/built_in_operation.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
from json import loads
1010
from typing import Any, Dict, Self
1111

12-
from pydantic import BaseModel, ConfigDict, Field, StrictStr
12+
from pydantic import BaseModel, ConfigDict, Field
1313

1414
from algoliasearch.search.models.built_in_operation_type import BuiltInOperationType
15+
from algoliasearch.search.models.built_in_operation_value import BuiltInOperationValue
1516

1617

1718
class BuiltInOperation(BaseModel):
@@ -20,9 +21,7 @@ class BuiltInOperation(BaseModel):
2021
"""
2122

2223
operation: BuiltInOperationType = Field(alias="_operation")
23-
value: StrictStr = Field(
24-
description="Value that corresponds to the operation, for example an `Increment` or `Decrement` step, or an `Add` or `Remove` value."
25-
)
24+
value: BuiltInOperationValue
2625

2726
model_config = ConfigDict(
2827
use_enum_values=True, populate_by_name=True, validate_assignment=True
@@ -51,6 +50,8 @@ def to_dict(self) -> Dict[str, Any]:
5150
exclude={},
5251
exclude_none=True,
5352
)
53+
if self.value:
54+
_dict["value"] = self.value.to_dict()
5455
return _dict
5556

5657
@classmethod
@@ -63,6 +64,13 @@ def from_dict(cls, obj: Dict) -> Self:
6364
return cls.model_validate(obj)
6465

6566
_obj = cls.model_validate(
66-
{"_operation": obj.get("_operation"), "value": obj.get("value")}
67+
{
68+
"_operation": obj.get("_operation"),
69+
"value": (
70+
BuiltInOperationValue.from_dict(obj.get("value"))
71+
if obj.get("value") is not None
72+
else None
73+
),
74+
}
6775
)
6876
return _obj
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# coding: utf-8
2+
3+
"""
4+
Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.
5+
"""
6+
7+
from __future__ import annotations
8+
9+
from json import dumps, loads
10+
from typing import Dict, Optional, Self, Union
11+
12+
from pydantic import (
13+
BaseModel,
14+
Field,
15+
StrictInt,
16+
StrictStr,
17+
ValidationError,
18+
model_serializer,
19+
)
20+
21+
22+
class BuiltInOperationValue(BaseModel):
23+
"""
24+
BuiltInOperationValue
25+
"""
26+
27+
oneof_schema_1_validator: Optional[StrictStr] = Field(
28+
default=None,
29+
description="A string to append or remove for the `Add`, `Remove`, and `AddUnique` operations.",
30+
)
31+
oneof_schema_2_validator: Optional[StrictInt] = Field(
32+
default=None,
33+
description="A number to add, remove, or append, depending on the operation.",
34+
)
35+
actual_instance: Optional[Union[int, str]] = None
36+
37+
def __init__(self, *args, **kwargs) -> None:
38+
if args:
39+
if len(args) > 1:
40+
raise ValueError(
41+
"If a position argument is used, only 1 is allowed to set `actual_instance`"
42+
)
43+
if kwargs:
44+
raise ValueError(
45+
"If a position argument is used, keyword arguments cannot be used."
46+
)
47+
super().__init__(actual_instance=args[0])
48+
else:
49+
super().__init__(**kwargs)
50+
51+
@model_serializer
52+
def unwrap_actual_instance(self) -> Optional[Union[int, str]]:
53+
"""
54+
Unwraps the `actual_instance` when calling the `to_json` method.
55+
"""
56+
return self.actual_instance
57+
58+
@classmethod
59+
def from_dict(cls, obj: dict) -> Self:
60+
return cls.from_json(dumps(obj))
61+
62+
@classmethod
63+
def from_json(cls, json_str: str) -> Self:
64+
"""Returns the object represented by the json string"""
65+
instance = cls.model_construct()
66+
error_messages = []
67+
68+
try:
69+
instance.oneof_schema_1_validator = loads(json_str)
70+
instance.actual_instance = instance.oneof_schema_1_validator
71+
72+
return instance
73+
except (ValidationError, ValueError) as e:
74+
error_messages.append(str(e))
75+
try:
76+
instance.oneof_schema_2_validator = loads(json_str)
77+
instance.actual_instance = instance.oneof_schema_2_validator
78+
79+
return instance
80+
except (ValidationError, ValueError) as e:
81+
error_messages.append(str(e))
82+
83+
raise ValueError(
84+
"No match found when deserializing the JSON string into BuiltInOperationValue with oneOf schemas: int, str. Details: "
85+
+ ", ".join(error_messages)
86+
)
87+
88+
def to_json(self) -> str:
89+
"""Returns the JSON representation of the actual instance"""
90+
if self.actual_instance is None:
91+
return "null"
92+
93+
to_json = getattr(self.actual_instance, "to_json", None)
94+
if callable(to_json):
95+
return self.actual_instance.to_json()
96+
else:
97+
return dumps(self.actual_instance)
98+
99+
def to_dict(self) -> Dict:
100+
"""Returns the dict representation of the actual instance"""
101+
if self.actual_instance is None:
102+
return None
103+
104+
to_dict = getattr(self.actual_instance, "to_dict", None)
105+
if callable(to_dict):
106+
return self.actual_instance.to_dict()
107+
else:
108+
return self.actual_instance

0 commit comments

Comments
 (0)