Improve JSON pretty printing in generated snippets#1587
Improve JSON pretty printing in generated snippets#1587Xvvln wants to merge 1 commit intofoss42:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR enhances the Python Requests and JavaScript Fetch code generators to pretty-print JSON responses when possible, falling back to plain-text output when the response body is not valid JSON (closes #1555).
Changes:
- Python Requests snippets now import
jsonand pretty-printresponse.json()withindent=4, falling back toresponse.texton JSON decode failure. - JavaScript Fetch snippets now attempt to parse the response body as JSON and log a pretty-printed
JSON.stringify(..., null, 2), falling back to the raw body. - Updated generator tests to match the new output patterns.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
lib/codegen/python/requests.dart |
Adds JSON pretty-printing with a try/except fallback in the generated Python snippet. |
lib/codegen/js/fetch.dart |
Adds JSON parse + pretty-print fallback logic to generated Fetch snippets. |
test/codegen/python_requests_codegen_test.dart |
Updates expected Python snippets to include import json and pretty-print/fallback behavior. |
test/codegen/js_fetch_codegen_test.dart |
Updates expected Fetch snippets to include JSON pretty-print/fallback behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| .then(async res => { | ||
| console.log(res.status); | ||
| return res.text() | ||
| }) | ||
| .then(body => { | ||
| console.log(body); | ||
| const body = await res.text(); | ||
| try { | ||
| console.log(JSON.stringify(JSON.parse(body), null, 2)); | ||
| } catch { |
There was a problem hiding this comment.
Using an async callback inside .then(...) introduces an async/await requirement for the generated snippet (ES2017). Since the previous version used a plain promise chain, consider keeping return res.text() and moving the JSON.parse/pretty-print into the next .then(body => ...) to preserve compatibility and avoid mixing async/await with .then() here.
| const body = await res.text(); | ||
| try { | ||
| console.log(JSON.stringify(JSON.parse(body), null, 2)); | ||
| } catch { |
There was a problem hiding this comment.
The generated snippet uses optional catch binding (catch { ... }), which is not supported in some older JS runtimes. To maximize snippet compatibility, use catch (e) { ... } even if e is unused.
| } catch { | |
| } catch (e) { |
Closes #1555
Summary
This PR improves generated snippets for Python Requests and JavaScript Fetch by pretty-printing JSON responses when possible, while preserving a plain-text fallback for non-JSON responses.
Changes
jsonand tryjson.dumps(response.json(), indent=4)before falling back toresponse.textJSON.parse(body)and logJSON.stringify(..., null, 2)before falling back to the raw bodyValidation
flutter test --no-pub test/codegen/python_requests_codegen_test.dart✅flutter test --no-pub test/codegen/js_fetch_codegen_test.dart✅