Skip to content

Commit 9cb311f

Browse files
authored
[FIX] fix long urls (#1085)
* make okay fix * fix style * update openapi
1 parent 0896135 commit 9cb311f

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

store/backend/neurostore/openapi

store/backend/neurostore/resources/base.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,14 +441,27 @@ def clear_cache(unique_ids):
441441

442442
def cache_key_creator(*args, **kwargs):
443443
# relevant pieces of information
444-
# 1. the query arguments
444+
# 1. the query arguments (including extra_args if present)
445445
# 2. the path
446446
# 3. the user
447447
path = request.path
448448
user = get_current_user().id if get_current_user() else ""
449-
args_as_sorted_tuple = tuple(
450-
sorted(pair for pair in request.args.items(multi=True))
451-
)
449+
450+
# Get query args from request
451+
query_items = list(request.args.items(multi=True))
452+
453+
# If extra_args is present, merge into query_items
454+
extra_args = kwargs.get("extra_args")
455+
if extra_args:
456+
for k, v in extra_args.items():
457+
# Support both single values and lists
458+
if isinstance(v, list):
459+
for item in v:
460+
query_items.append((k, item))
461+
else:
462+
query_items.append((k, v))
463+
464+
args_as_sorted_tuple = tuple(sorted(query_items))
452465
query_args = str(args_as_sorted_tuple)
453466

454467
cache_key = "_".join([path, query_args, user])
@@ -628,9 +641,14 @@ def create_metadata(self, q, total):
628641
return {"total_count": total}
629642

630643
@cache.cached(60 * 60, query_string=True, make_cache_key=cache_key_creator)
631-
def search(self):
644+
def search(self, extra_args=None):
632645
# Parse arguments using webargs
646+
import logging
647+
648+
logging.warning(f"I RAN HERE Request args: {request.args}")
633649
args = parser.parse(self._user_args, request, location="query")
650+
if extra_args:
651+
args.update(extra_args)
634652

635653
m = self._model # for brevity
636654
q = m.query

store/backend/neurostore/resources/pipeline.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Pipeline related resources"""
22

33
from sqlalchemy import text, and_, or_
4-
from flask import abort
4+
from flask import abort, request
55

66
from sqlalchemy.orm import selectinload, aliased
77
from webargs import fields
@@ -272,3 +272,22 @@ def eager_load(self, q, args=None):
272272
)
273273
)
274274
return q
275+
276+
def post(self):
277+
"""
278+
If 'study_ids' is present in the request body, treat as a search (bypass authorization).
279+
Only study_ids are in the body; all other filters are in the query string.
280+
Otherwise, treat as a creation (require authorization).
281+
"""
282+
data = request.get_json() or {}
283+
284+
if "study_ids" in data:
285+
# Bypass authorization for search requests
286+
# Convert study_ids to study_id for consistent filtering
287+
study_ids = data.get("study_ids", [])
288+
extra_args = {"study_id": study_ids}
289+
# Call cached search (enables cache for study_ids POST)
290+
return self.search(extra_args=extra_args)
291+
else:
292+
# Standard POST: require authorization (enforced by OpenAPI and Flask)
293+
return super().post(self)

store/backend/neurostore/tests/api/test_pipeline_resources.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,3 +590,26 @@ def test_pipeline_config_with_schema(auth_client, pipeline1):
590590
assert data["config_args"]["extractor"] == "ParticipantDemographicsExtractor"
591591
assert "extractor_kwargs" in data["config_args"]
592592
assert data["config_args"]["extraction_model"] == "gpt-4"
593+
594+
595+
def test_post_pipeline_study_results_with_study_ids(
596+
auth_client, result1, result2, pipeline_study_result_payload
597+
):
598+
study1_id = pipeline_study_result_payload[0]["base_study_id"]
599+
study2_id = pipeline_study_result_payload[1]["base_study_id"]
600+
url = "/api/pipeline-study-results/"
601+
payload = {"study_ids": [study1_id, study2_id]}
602+
603+
# First request: should call search logic and return both results
604+
response = auth_client.post(url, data=payload, content_type="application/json")
605+
assert response.status_code == 200
606+
data = response.json()
607+
returned_ids = {r["base_study_id"] for r in data["results"]}
608+
assert returned_ids == {study1_id, study2_id}
609+
610+
# Test cache hit (currently not working)
611+
response2 = auth_client.post(url, data=payload)
612+
assert response2.status_code == 200
613+
data2 = response2.json()
614+
returned_ids2 = {r["base_study_id"] for r in data2["results"]}
615+
assert returned_ids2 == {study1_id, study2_id}

0 commit comments

Comments
 (0)