Conversation
There was a problem hiding this comment.
Pull request overview
Updates the FastAPI wrap_jsonp helper to correctly JSON-serialize list responses, preventing starlette.responses.Response.render errors when endpoints return [].
Changes:
- Expand
wrap_jsonpinput type to acceptlistin addition todict/str. - Serialize
dict/listinputs viajson.dumpswhile preserving pre-serialized JSON strings. - Add an explicit type guard for unexpected
datatypes.
Comments suppressed due to low confidence (1)
openlibrary/fastapi/models.py:77
wrap_jsonpraisesValueErrorwhen thecallbackquery param is invalid. Since this helper is used directly in FastAPI route handlers (e.g.,openlibrary/fastapi/books.py) and there is no global exception handler forValueErrorin the ASGI app, this will surface as a 500 error for a client-supplied bad callback. Prefer raisingHTTPException(status_code=400, ...)so invalid user input returns a 4xx response (and update the existing unit test that currently expectsValueError).
if callback := request.query_params.get("callback"):
if not JS_CALLBACK_RE.match(callback):
raise ValueError("Invalid callback parameter: must be a valid JavaScript identifier (only letters, numbers, underscore, $, and . allowed)")
return Response(content=f"{callback}({json_string});", media_type="application/javascript")
| if isinstance(data, str): | ||
| json_string = data | ||
| elif isinstance(data, (dict, list)): | ||
| json_string = json.dumps(data) |
There was a problem hiding this comment.
List support was added to wrap_jsonp (serializing list via json.dumps), but the existing unit tests for wrap_jsonp (see openlibrary/tests/fastapi/test_models.py) don’t cover the list case. Please add a test that passes a list (with and without a callback) to prevent this regression from reappearing.
There was a problem hiding this comment.
@copilot apply changes based on this feedback
There was a problem hiding this comment.
Implemented in 81fbb53. I added regression tests in openlibrary/tests/fastapi/test_models.py covering list payloads for both cases: no callback (application/json) and with callback (application/javascript).
Agent-Logs-Url: https://github.com/internetarchive/openlibrary/sessions/176cae49-0cb2-4f2f-8cba-bcc652a22398 Co-authored-by: jimchamp <28732543+jimchamp@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Addresses regression identified here
starlette.responses.Response.renderis throwing anAttributeErrorwhendatais alist. These changes ensure that lists are also serialized.Technical
Testing
Screenshot
Stakeholders
@RayBB
@cdrini