Skip to content

Improve JSON pretty printing in generated snippets#1587

Open
Xvvln wants to merge 1 commit intofoss42:mainfrom
Xvvln:fix-1555-json-pretty-print-clean
Open

Improve JSON pretty printing in generated snippets#1587
Xvvln wants to merge 1 commit intofoss42:mainfrom
Xvvln:fix-1555-json-pretty-print-clean

Conversation

@Xvvln
Copy link
Copy Markdown
Contributor

@Xvvln Xvvln commented Mar 30, 2026

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

  • Python Requests snippets now import json and try json.dumps(response.json(), indent=4) before falling back to response.text
  • JavaScript Fetch snippets now try JSON.parse(body) and log JSON.stringify(..., null, 2) before falling back to the raw body
  • Updated codegen tests for both generators

Validation

  • flutter test --no-pub test/codegen/python_requests_codegen_test.dart
  • flutter test --no-pub test/codegen/js_fetch_codegen_test.dart

Copilot AI review requested due to automatic review settings March 30, 2026 14:57
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 json and pretty-print response.json() with indent=4, falling back to response.text on 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.

Comment thread lib/codegen/js/fetch.dart
Comment on lines +41 to +46
.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 {
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread lib/codegen/js/fetch.dart
const body = await res.text();
try {
console.log(JSON.stringify(JSON.parse(body), null, 2));
} catch {
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
} catch {
} catch (e) {

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DX Enrichment] Implement JSON Pretty-Printing for Generated Snippets

2 participants