Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions packages/http-server-js/src/http/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,24 +641,30 @@ function* emitResultProcessingForType(

if (body) {
const bodyCase = parseCase(body.name);
const serializationRequired = isSerializationRequired(
ctx,
module,
body.type,
"application/json",
);
requireSerialization(ctx, body.type, "application/json");

yield `${names.ctx}.response.setHeader("content-type", "application/json");`;

if (serializationRequired) {
const typeReference = emitTypeReference(ctx, body.type, body, module, {
altName: namer.getAltName("Body"),
requireDeclaration: true,
});
yield `${names.ctx}.response.end(globalThis.JSON.stringify(${typeReference}.toJsonObject(${names.result}.${bodyCase.camelCase})))`;
if (ctx.program.checker.isStdType(body.type, "bytes")) {
// Binary response: content-type already set by @header above, send bytes directly.
yield `${names.ctx}.response.end(${names.result}.${bodyCase.camelCase});`;
} else {
yield `${names.ctx}.response.end(globalThis.JSON.stringify(${names.result}.${bodyCase.camelCase}));`;
const serializationRequired = isSerializationRequired(
ctx,
module,
body.type,
"application/json",
);
requireSerialization(ctx, body.type, "application/json");

yield `${names.ctx}.response.setHeader("content-type", "application/json");`;

if (serializationRequired) {
const typeReference = emitTypeReference(ctx, body.type, body, module, {
altName: namer.getAltName("Body"),
requireDeclaration: true,
});
yield `${names.ctx}.response.end(globalThis.JSON.stringify(${typeReference}.toJsonObject(${names.result}.${bodyCase.camelCase})))`;
} else {
yield `${names.ctx}.response.end(globalThis.JSON.stringify(${names.result}.${bodyCase.camelCase}));`;
}
}
} else if (isArrayModelType(target)) {
const itemType = target.indexer.value;
Expand Down
21 changes: 21 additions & 0 deletions packages/http-server-js/test/scalar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,27 @@ describe("scalar", () => {
expect(serverRaw).toMatch(/response\.end\(globalThis\.JSON\.stringify\(__result_\d+\)\);/);
});

it("emits direct bytes send for binary body with custom content-type", async () => {
const { outputs } = await HttpServerEmitterTester.compile(`
@service(#{ title: "Example" })
@route("/")
namespace Example {
@post op download(): {
@statusCode statusCode: 200;
@header contentType: "application/zip";
@body content: bytes;
};
}
`);

const serverRaw = outputs["src/generated/http/operations/server-raw.ts"];

expect(serverRaw).toBeDefined();
expect(serverRaw).not.toContain('"application/json"');
expect(serverRaw).not.toContain("Uint8Array.toJsonObject");
expect(serverRaw).toMatch(/response\.end\(__result_\d+\.content\);/);
});

describe("date/time/duration types", () => {
describe("mode: temporal", () => {
const options: JsEmitterOptions = {
Expand Down