From 59a8350aace8f44122f7a21cf65e006cc66e0114 Mon Sep 17 00:00:00 2001 From: Remco Meeuwissen Date: Thu, 14 Dec 2023 14:03:07 +0100 Subject: [PATCH 1/3] Updated a few places I missed last time with the decimal.Decimal orjson fix --- tipg/factory.py | 9 ++++----- tipg/resources/response.py | 14 +++++++++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/tipg/factory.py b/tipg/factory.py index ed3e713b..3a8c5edb 100644 --- a/tipg/factory.py +++ b/tipg/factory.py @@ -35,7 +35,7 @@ ) from tipg.errors import MissingGeometryColumn, NoPrimaryKey, NotFound from tipg.resources.enums import MediaType -from tipg.resources.response import GeoJSONResponse, SchemaJSONResponse +from tipg.resources.response import GeoJSONResponse, SchemaJSONResponse, orjsonDumps from tipg.settings import FeaturesSettings, MVTSettings, TMSSettings from fastapi import APIRouter, Depends, Path, Query @@ -781,7 +781,7 @@ async def items( # noqa: C901 # NDJSON Response if output_type == MediaType.ndjson: return StreamingResponse( - (orjson.dumps(row) + b"\n" for row in rows), + (orjsonDumps(row) + b"\n" for row in rows), media_type=MediaType.ndjson, headers={ "Content-Disposition": "attachment;filename=items.ndjson" @@ -892,7 +892,7 @@ async def items( # noqa: C901 # GeoJSONSeq Response elif output_type == MediaType.geojsonseq: return StreamingResponse( - (orjson.dumps(f) + b"\n" for f in data["features"]), # type: ignore + (orjsonDumps(f) + b"\n" for f in data["features"]), # type: ignore media_type=MediaType.geojsonseq, headers={ "Content-Disposition": "attachment;filename=items.geojson" @@ -1016,7 +1016,7 @@ async def item( # NDJSON Response if output_type == MediaType.ndjson: return StreamingResponse( - (orjson.dumps(row) + b"\n" for row in rows), + (orjsonDumps(row) + b"\n" for row in rows), media_type=MediaType.ndjson, headers={ "Content-Disposition": "attachment;filename=items.ndjson" @@ -1512,7 +1512,6 @@ async def collection_get_tile( return Response(bytes(tile), media_type=MediaType.mvt.value) def _tilejson_routes(self): - ############################################################################ # ADDITIONAL ENDPOINTS NOT IN OGC Tiles API (tilejson, style.json, viewer) # ############################################################################ diff --git a/tipg/resources/response.py b/tipg/resources/response.py index 51a7f0fd..6e89a403 100644 --- a/tipg/resources/response.py +++ b/tipg/resources/response.py @@ -14,16 +14,20 @@ def default(obj): return str(obj) +def orjsonDumps(content: Any): + return orjson.dumps( + content, + default=default, + option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY, + ) + + class ORJSONResponse(JSONResponse): """Custom response handler for using orjson""" def render(self, content: Any) -> bytes: """Render the content into a JSON response using orjson""" - return orjson.dumps( - content, - default=default, - option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY, - ) + return orjsonDumps(content) class GeoJSONResponse(ORJSONResponse): From 0c7e54fd697255718b07e15ebe61b57f8246ee95 Mon Sep 17 00:00:00 2001 From: Remco Meeuwissen Date: Thu, 14 Dec 2023 14:23:39 +0100 Subject: [PATCH 2/3] Two more orjson.dumps cases I forgot to change --- tipg/factory.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tipg/factory.py b/tipg/factory.py index 3a8c5edb..5442ea41 100644 --- a/tipg/factory.py +++ b/tipg/factory.py @@ -886,7 +886,7 @@ async def items( # noqa: C901 # HTML Response if output_type == MediaType.html: return self._create_html_response( - request, orjson.dumps(data).decode(), template_name="items" + request, orjsonDumps(data).decode(), template_name="items" ) # GeoJSONSeq Response @@ -1050,7 +1050,7 @@ async def item( if output_type == MediaType.html: return self._create_html_response( request, - orjson.dumps(data).decode(), + orjsonDumps(data).decode(), template_name="item", ) From 24b7109b265d8f6fa85049d99e6b5bef16329260 Mon Sep 17 00:00:00 2001 From: Remco Meeuwissen Date: Fri, 15 Dec 2023 13:36:37 +0100 Subject: [PATCH 3/3] Forgot to add a docstring to the new function in response.py --- tipg/resources/response.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tipg/resources/response.py b/tipg/resources/response.py index 6e89a403..0640231b 100644 --- a/tipg/resources/response.py +++ b/tipg/resources/response.py @@ -15,6 +15,7 @@ def default(obj): def orjsonDumps(content: Any): + """Small wrapper function to run the orjson.dumps with the additional options we want""" return orjson.dumps( content, default=default,