Skip to content

Commit 6dde143

Browse files
committed
use request wrapper
1 parent 5a66c64 commit 6dde143

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

stac_fastapi/core/stac_fastapi/core/extensions/collections_search.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ async def documented_endpoint(
9393
example="cql2-json",
9494
),
9595
):
96-
# Delegate to original endpoint which reads from request.query_params
97-
# But first, ensure the q parameter is properly handled as a list
98-
# since FastAPI extracts it from the URL when it's defined as a function parameter
96+
# Delegate to original endpoint with parameters
97+
# Since FastAPI extracts parameters from the URL when they're defined as function parameters,
98+
# we need to create a request wrapper that provides our modified query_params
9999

100100
# Create a mutable copy of query_params
101101
if hasattr(request, "_query_params"):
@@ -113,15 +113,28 @@ async def documented_endpoint(
113113
# Already a list, use as-is
114114
query_params["q"] = q
115115

116-
# Temporarily replace request.query_params
117-
original_query_params = request.query_params
118-
request.query_params = query_params
116+
# Add filter parameters back to query_params if they were provided
117+
if filter is not None:
118+
query_params["filter"] = filter
119+
if filter_lang is not None:
120+
query_params["filter-lang"] = filter_lang
119121

120-
try:
121-
return await original_endpoint(request)
122-
finally:
123-
# Restore original query_params
124-
request.query_params = original_query_params
122+
# Create a request wrapper that provides our modified query_params
123+
class RequestWrapper:
124+
def __init__(self, original_request, modified_query_params):
125+
self._original = original_request
126+
self._query_params = modified_query_params
127+
128+
@property
129+
def query_params(self):
130+
return self._query_params
131+
132+
def __getattr__(self, name):
133+
# Delegate all other attributes to the original request
134+
return getattr(self._original, name)
135+
136+
wrapped_request = RequestWrapper(request, query_params)
137+
return await original_endpoint(wrapped_request)
125138

126139
documented_endpoint.__name__ = original_endpoint.__name__
127140
return documented_endpoint

0 commit comments

Comments
 (0)