Skip to content

Commit dd4a4c7

Browse files
jlahovnikanesson-cs
authored andcommitted
fix: filter extension for search and items
1 parent e8f4017 commit dd4a4c7

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

stac_fastapi/eodag/core.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
import attr
2929
import cql2
3030
import orjson
31-
import pygeofilter
3231
from fastapi import HTTPException
3332
from pydantic import ValidationError
3433
from pydantic_core import InitErrorDetails, PydanticCustomError
34+
from pygeofilter.parsers.cql2_json import parse as parse_json
3535
from stac_fastapi.api.models import create_post_request_model
3636
from stac_fastapi.types.errors import NotFoundError
3737
from stac_fastapi.types.requests import get_base_url
@@ -481,10 +481,11 @@ def _clean_search_args(
481481
"""Clean up search arguments to match format expected by pgstac"""
482482
if filter_expr:
483483
if filter_lang == "cql2-text":
484-
filter_expr = cql2.parse_text(filter_expr).to_json()
485-
filter_lang = "cql2-json"
486-
487-
base_args["filter"] = str2json("filter_expr", filter_expr)
484+
base_args["filter"] = cql2.parse_text(filter_expr).to_json()
485+
elif filter_lang == "cql2-json":
486+
base_args["filter"] = str2json("filter_expr", filter_expr)
487+
else:
488+
raise HTTPException(status_code=400, detail=f"Unsupported filter_lang {filter_lang}")
488489
base_args["filter_lang"] = "cql2-json"
489490

490491
if datetime:
@@ -501,10 +502,12 @@ def _clean_search_args(
501502
for sort in sortby:
502503
sortparts = re.match(r"^([+-]?)(.*)$", sort)
503504
if sortparts:
504-
sort_param.append({
505-
"field": sortparts.group(2).strip(),
506-
"direction": "desc" if sortparts.group(1) == "-" else "asc",
507-
})
505+
sort_param.append(
506+
{
507+
"field": sortparts.group(2).strip(),
508+
"direction": "desc" if sortparts.group(1) == "-" else "asc",
509+
}
510+
)
508511
base_args["sortby"] = sort_param
509512

510513
# Remove None values from dict
@@ -649,7 +652,7 @@ def add_error(error_message: str) -> None:
649652

650653
errors: list[InitErrorDetails] = []
651654
try:
652-
parsing_result = EodagEvaluator().evaluate(pygeofilter.parse(filter_)) # type: ignore
655+
parsing_result = EodagEvaluator().evaluate(parse_json(filter_)) # type: ignore
653656
except (ValueError, NotImplementedError) as e:
654657
add_error(str(e))
655658
raise ValidationError.from_exception_data(title="stac-fastapi-eodag", line_errors=errors) from e

0 commit comments

Comments
 (0)