Skip to content

✨ Quality: Incorrect JSON detection for Content-Type with charset causes wrong response parsing#2774

Open
huyhoang171106 wants to merge 1 commit intoopenfun:masterfrom
huyhoang171106:contribai/improve/quality/incorrect-json-detection-for-content-typ
Open

✨ Quality: Incorrect JSON detection for Content-Type with charset causes wrong response parsing#2774
huyhoang171106 wants to merge 1 commit intoopenfun:masterfrom
huyhoang171106:contribai/improve/quality/incorrect-json-detection-for-content-typ

Conversation

@huyhoang171106
Copy link
Copy Markdown

✨ Code Quality

Problem

getResponseBody() checks Content-Type using strict equality to 'application/json'. Real APIs commonly return 'application/json; charset=utf-8'. In that case, the function falls back to response.text() and returns a string instead of parsed JSON, causing downstream runtime failures when callers access object properties.

Severity: high
File: src/frontend/js/api/utils.ts

Solution

Parse media type using prefix/inclusion instead of exact equality:

Changes

  • src/frontend/js/api/utils.ts (modified)

Purpose

Description...

Proposal

Description...

  • [] item 1...
  • [] item 2...

🤖 About this PR

This pull request was generated by ContribAI, an AI agent
that helps improve open source projects. The change was:

  1. Discovered by automated code analysis
  2. Generated by AI with context-aware code generation
  3. Self-reviewed by AI quality checks

If you have questions or feedback about this PR, please comment below.
We appreciate your time reviewing this contribution!

Closes #2773

…charset causes wrong response parsing

`getResponseBody()` checks `Content-Type` using strict equality to `'application/json'`. Real APIs commonly return `'application/json; charset=utf-8'`. In that case, the function falls back to `response.text()` and returns a string instead of parsed JSON, causing downstream runtime failures when callers access object properties.

Affected files: utils.ts

Signed-off-by: Nguyen Huy Hoang <181364121+huyhoang171106@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 26, 2026 13:17
Copy link
Copy Markdown

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 updates the frontend API response parsing helper so JSON responses are detected even when the Content-Type header includes parameters (e.g., charset=utf-8), preventing callers from accidentally receiving a raw string instead of parsed JSON.

Changes:

  • Cache Content-Type once per response in getResponseBody.
  • Switch JSON detection from strict equality to substring detection.
  • Reuse the cached Content-Type for file-type detection.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +19 to 22
const contentType = response.headers.get('Content-Type') || '';

if (contentType.includes('application/json')) {
return response.json();
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

contentType.includes('application/json') is case-sensitive and can yield false positives (e.g. application/jsonp) while still missing valid JSON media types like application/merge-patch+json / application/*+json. Consider normalizing once (trim + lowercase) and comparing the parsed media type (e.g., split on ;) against application/json or endsWith('+json').

Copilot uses AI. Check for mistakes.
Comment on lines 25 to 27
const fileType = ['application/pdf', 'application/zip'];
if (fileType.includes(response.headers.get('Content-Type') || '')) {
if (fileType.includes(contentType)) {
return new Promise((resolve) => resolve(response));
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

fileType.includes(contentType) still won’t match when the Content-Type header includes parameters (e.g. application/pdf; charset=binary). If you parse/normalize the media type (e.g. contentType.split(';')[0]), reuse that value for both JSON and file detection to avoid similar mis-parsing for downloads.

Copilot uses AI. Check for mistakes.
Comment on lines 18 to 30
export function getResponseBody(response: Response) {
if (response.headers.get('Content-Type') === 'application/json') {
const contentType = response.headers.get('Content-Type') || '';

if (contentType.includes('application/json')) {
return response.json();
}

const fileType = ['application/pdf', 'application/zip'];
if (fileType.includes(response.headers.get('Content-Type') || '')) {
if (fileType.includes(contentType)) {
return new Promise((resolve) => resolve(response));
}

return response.text();
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

There’s no unit test covering the new Content-Type parsing behavior (e.g. application/json; charset=utf-8 should go through response.json(), and ideally application/*+json too). Adding a focused test for getResponseBody would prevent regressions and validate the fix for #2773.

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.

fix(frontend): incorrect json detection for content-type with charset causes wrong response parsing

2 participants