Skip to content
Merged
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
5 changes: 3 additions & 2 deletions packages/platform-fastify/adapters/fastify-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,10 @@ export class FastifyAdapter<
}
body = body.getStream();
}
const responseContentType = fastifyReply.getHeader('Content-Type');
if (
fastifyReply.getHeader('Content-Type') !== undefined &&
fastifyReply.getHeader('Content-Type') !== 'application/json' &&
typeof responseContentType === 'string' &&
!responseContentType.startsWith('application/json') &&
body?.statusCode >= HttpStatus.BAD_REQUEST
) {
Logger.warn(
Expand Down
29 changes: 29 additions & 0 deletions packages/platform-fastify/test/adapters/fastify-adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,35 @@ describe('FastifyAdapter', () => {
expect(reply.status).toHaveBeenCalledWith(statusCode);
}
});

it('should keep a JSON content type that carries parameters', () => {
const reply = createReply();
reply.getHeader.mockReturnValue('application/json; charset=utf-8');

fastifyAdapter.reply(
reply as any,
{ statusCode: 400, message: 'Oops' },
400,
);

expect(reply.header).not.toHaveBeenCalled();
});

it('should force a JSON content type for error bodies sent with a non-JSON content type', () => {
const reply = createReply();
reply.getHeader.mockReturnValue('text/html');

fastifyAdapter.reply(
reply as any,
{ statusCode: 400, message: 'Oops' },
400,
);

expect(reply.header).toHaveBeenCalledWith(
'Content-Type',
'application/json',
);
});
});

describe('mapException', () => {
Expand Down
Loading