Fix: API server returning double-encoded JSON string#1935
Fix: API server returning double-encoded JSON string#1935kl421 wants to merge 1 commit intorobbrad:masterfrom
Conversation
Fix double-encoded JSON output in API server
📝 WalkthroughWalkthroughModified Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
uk_bin_collection_api_server/server.py (1)
35-43:⚠️ Potential issue | 🔴 CriticalCritical indentation error:
try:is outside the function scope.The
try:statement at line 35 has no indentation, placing it at module level rather than inside thecouncil_data()function. This causes a PythonIndentationErrorbecause theexceptclause at line 40 is indented at 4 spaces (function body level), which doesn't match thetry:at column 0.The JSON parsing logic is correct—
CollectData.run()returns a JSON string fromoutput_json(), andjson.loads()properly converts it back to a dict for Connexion to serialize.🐛 Proposed fix for indentation
-try: + try: CollectData = UKBinCollectionApp() CollectData.set_args(args) raw_string_output = CollectData.run() return json.loads(raw_string_output) except Exception as err: logging.error(traceback.format_exc()) logging.info(f"Schema: {err}") raise err🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@uk_bin_collection_api_server/server.py` around lines 35 - 43, The try/except block is at module scope instead of inside the council_data() function; move/indent the entire try/except (the lines creating UKBinCollectionApp, calling CollectData.set_args(args), raw_string_output = CollectData.run(), json.loads(raw_string_output), and the except that logs traceback and err) so that the try: aligns with the other statements inside council_data(); ensure references to UKBinCollectionApp, CollectData.set_args, CollectData.run, json.loads, logging.error(traceback.format_exc()), logging.info(f"Schema: {err}") and raise err remain unchanged except for indentation.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@uk_bin_collection_api_server/server.py`:
- Around line 35-43: The try/except block is at module scope instead of inside
the council_data() function; move/indent the entire try/except (the lines
creating UKBinCollectionApp, calling CollectData.set_args(args),
raw_string_output = CollectData.run(), json.loads(raw_string_output), and the
except that logs traceback and err) so that the try: aligns with the other
statements inside council_data(); ensure references to UKBinCollectionApp,
CollectData.set_args, CollectData.run, json.loads,
logging.error(traceback.format_exc()), logging.info(f"Schema: {err}") and raise
err remain unchanged except for indentation.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 90ae2ff5-fb34-456e-8130-a8e4ad1c6cad
📒 Files selected for processing (1)
uk_bin_collection_api_server/server.py
Issue: > When running the API server, CollectData.run() returns a JSON-formatted string rather than a Python dictionary. Because Connexion automatically serializes the return value, it wraps the already-stringified JSON in another layer of quotes and escape characters (e.g., "{\n "bins": [\n..."). This prevents external dashboards and tools from natively parsing the JSON response.
Fix:
Imported the json module and wrapped the return statement in json.loads(). This converts the string back into a dictionary before handing it to Connexion, allowing the API to serve clean, native JSON payloads.
Summary by CodeRabbit