Skip to content

[Bug]: http-server-js emitter generates incorrect response code for bytes body with custom content-type #10609

@snipd-mikel

Description

@snipd-mikel

Describe the bug

When an operation returns a bytes body with a custom @header contentType (e.g. application/zip),
the emitter generates broken response code that:

  1. Correctly sets the content-type from the @header property, then immediately overwrites it with application/json
  2. Calls Uint8Array.toJsonObject() which doesn't exist, causing a runtime crash

This is the response-side equivalent of the request-side fix in #6898.

Reproduction

@post
op exportFile(): {
  @statusCode statusCode: 200;
  @header contentType: "application/zip";
  @body content: bytes;
};

Generated code in server-raw.ts:

// Bug 1: correct content-type is immediately overwritten
ctx.response.setHeader("content-type", result.contentType); // "application/zip" ✓
ctx.response.setHeader("content-type", "application/json"); // overwrites it ✗

// Bug 2: Uint8Array has no static toJsonObject method — throws at runtime
ctx.response.end(globalThis.JSON.stringify(Uint8Array.toJsonObject(result.content)));

Expected behavior:

ctx.response.setHeader("content-type", result.contentType);
ctx.response.end(result.content); // send bytes directly, no JSON serialization

Root cause:

In packages/http-server-js/src/http/server/index.ts, the response body handling block unconditionally applies JSON serialization without checking whether the body type is bytes (via ctx.program.checker.isStdType(body.type, "bytes")). The same check already exists on the request deserialization path (added in #6898) but was not applied symmetrically to responses.

Additional context:

Emitter version: 0.58.0-alpha.26

Checklist

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions