Skip to content

Fix null jsonBody serialization in HttpResponse #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: v4.x
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jun 25, 2025

When creating an HTTP response with jsonBody set to null, the value should be serialized as "null" (like JSON.stringify(null)), but it was being treated as undefined and no body or header was being set.

Problem

The issue was in the HttpResponse constructor where it used isDefined(init.jsonBody) which returns false for null values, causing the response to fall back to using init.body instead of properly serializing the null value as JSON.

// Before: This would not serialize null properly
const response = new HttpResponse({ jsonBody: null });
// Result: Empty body with no content-type header

// After: This now works correctly  
const response = new HttpResponse({ jsonBody: null });
// Result: Body contains "null" with application/json content-type

Solution

Changed the condition in HttpResponse constructor from isDefined(init.jsonBody) to init.jsonBody !== undefined to allow null values to be properly serialized as JSON.

Changes Made

  • Modified src/http/HttpResponse.ts to check init.jsonBody !== undefined instead of isDefined(init.jsonBody)
  • Removed unused isDefined import
  • Added comprehensive test cases for JSON primitive types:
    • null serializes to "null" with application/json content-type
    • false serializes to "false" with application/json content-type
    • 0 serializes to "0" with application/json content-type

The fix ensures that all falsy but valid JSON values (null, false, 0) are treated as valid JSON values that should be serialized, consistent with JavaScript's JSON.stringify() behavior.

Fixes #325.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@swapnil-nagar swapnil-nagar marked this pull request as ready for review June 25, 2025 03:30
@Copilot Copilot AI requested a review from a team as a code owner June 25, 2025 03:32
@swapnil-nagar swapnil-nagar marked this pull request as draft June 25, 2025 03:35
@Copilot Copilot AI changed the title [WIP] Cannot create null jsonBody Fix null jsonBody serialization in HttpResponse Jun 25, 2025
@Copilot Copilot AI requested a review from swapnil-nagar June 25, 2025 03:38
Copilot finished work on behalf of swapnil-nagar June 25, 2025 03:38
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.

Cannot create null jsonBody
2 participants