Skip to content

Commit a059d30

Browse files
committed
Fix: CORE Filter serialization
The Filter class' str method would not be called, and the pydantic serialization logic could not deduce it. Add an explicit field serializer to make it str.
1 parent cb389a7 commit a059d30

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/obelisk/asynchronous/core.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
model_validator,
2525
SerializerFunctionWrapHandler,
2626
WrapSerializer,
27+
field_serializer,
2728
)
2829
from typing import (
2930
Annotated,
@@ -157,7 +158,7 @@ class QueryParams(BaseModel):
157158
"""List of Field Names, with their potential prefixes and suffixes, to select ordering. None user server defaults."""
158159
dataType: DataType | None = None
159160
"""Data type expected to be returned, is mandatory if the `value` field is requested in the `fields` parameter"""
160-
filter_: Annotated[str | Filter | None, Field(serialization_alias="filter")] = None
161+
filter_: Annotated[str | Filter | None, Field(serialization_alias="filter",)] = None
161162
"""
162163
Obelisk CORE handles filtering in [RSQL format](https://obelisk.pages.ilabt.imec.be/obelisk-core/query.html#rsql-format),
163164
to make it easier to also programatically write these filters, we provide the `obelisk.types.core.Filter` option as well.
@@ -181,6 +182,13 @@ def to_dict(self) -> dict[str, Any]:
181182
exclude_none=True, by_alias=True, mode="json", exclude={"dataset"}
182183
)
183184

185+
@field_serializer('filter_', mode='plain')
186+
def serialize_filter(self, value: Filter | str | None) -> str | None:
187+
if value is None or isinstance(value, str):
188+
return value
189+
190+
return str(value)
191+
184192

185193
class ChunkedParams(BaseModel):
186194
"""

src/tests/asynchronous/core_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from obelisk.asynchronous.core import QueryParams
2+
from obelisk.types.core import Filter, Comparison
23

34

45
def test_query_param_serialize():
@@ -24,3 +25,14 @@ def test_comma_separate_string():
2425
assert data["groupBy"] == "dataset,timestamp"
2526
assert data["orderBy"] == "timestamp,value"
2627

28+
29+
30+
def test_filter_serialize():
31+
filter_ = Filter().add_and(Comparison.equal("metric", "smartphone.acceleration::number[]"), Comparison.greater_equal("timestamp", 123456))
32+
q = QueryParams(
33+
dataset="83989232",
34+
filter_=filter_,
35+
dataType="number[]",
36+
)
37+
dump = q.to_dict()
38+
assert "filter" in dump

0 commit comments

Comments
 (0)