Skip to content

Make orjson usage more consistent #402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed

- Make `orjson` usage more consistent [#402](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/402)
- Improved datetime query handling to only check start and end datetime values when datetime is None [#396](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/396)
- Optimize data_loader.py script [#395](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/395)
- Refactored test configuration to use shared app config pattern [#399](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/399)
Expand Down
12 changes: 6 additions & 6 deletions stac_fastapi/core/stac_fastapi/core/route_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import importlib
import inspect
import json
import logging
import os
from typing import List

import orjson
from fastapi import Depends
from jsonschema import validate

Expand Down Expand Up @@ -84,14 +84,14 @@

def get_route_dependencies_conf(route_dependencies_env: str) -> list:
"""Get Route dependencies configuration from file or environment variable."""
if os.path.exists(route_dependencies_env):
with open(route_dependencies_env, encoding="utf-8") as route_dependencies_file:
route_dependencies_conf = json.load(route_dependencies_file)
if os.path.isfile(route_dependencies_env):
with open(route_dependencies_env, "rb") as f:
route_dependencies_conf = orjson.loads(f.read())

else:
try:
route_dependencies_conf = json.loads(route_dependencies_env)
except json.JSONDecodeError as exception:
route_dependencies_conf = orjson.loads(route_dependencies_env)
except orjson.JSONDecodeError as exception:
_LOGGER.error("Invalid JSON format for route dependencies. %s", exception)
raise

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""Database logic."""

import asyncio
import json
import logging
from base64 import urlsafe_b64decode, urlsafe_b64encode
from copy import deepcopy
from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, Union

import attr
import elasticsearch.helpers as helpers
import orjson
from elasticsearch.dsl import Q, Search
from elasticsearch.exceptions import NotFoundError as ESNotFoundError
from starlette.requests import Request
Expand Down Expand Up @@ -503,7 +503,7 @@ async def execute_search(
search_after = None

if token:
search_after = json.loads(urlsafe_b64decode(token).decode())
search_after = orjson.loads(urlsafe_b64decode(token))

query = search.query.to_dict() if search.query else None

Expand Down Expand Up @@ -543,7 +543,7 @@ async def execute_search(
next_token = None
if len(hits) > limit and limit < max_result_window:
if hits and (sort_array := hits[limit - 1].get("sort")):
next_token = urlsafe_b64encode(json.dumps(sort_array).encode()).decode()
next_token = urlsafe_b64encode(orjson.dumps(sort_array)).decode()

matched = (
es_response["hits"]["total"]["value"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Database logic."""

import asyncio
import json
import logging
from base64 import urlsafe_b64decode, urlsafe_b64encode
from copy import deepcopy
from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, Union

import attr
import orjson
from opensearchpy import exceptions, helpers
from opensearchpy.helpers.query import Q
from opensearchpy.helpers.search import Search
Expand Down Expand Up @@ -527,7 +527,7 @@ async def execute_search(
search_after = None

if token:
search_after = json.loads(urlsafe_b64decode(token).decode())
search_after = orjson.loads(urlsafe_b64decode(token))
if search_after:
search_body["search_after"] = search_after

Expand Down Expand Up @@ -567,7 +567,7 @@ async def execute_search(
next_token = None
if len(hits) > limit and limit < max_result_window:
if hits and (sort_array := hits[limit - 1].get("sort")):
next_token = urlsafe_b64encode(json.dumps(sort_array).encode()).decode()
next_token = urlsafe_b64encode(orjson.dumps(sort_array)).decode()

matched = (
es_response["hits"]["total"]["value"]
Expand Down