Skip to content
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

koa integration overwrites content-type of response #1134

Closed
1 of 4 tasks
mrl5 opened this issue Nov 11, 2021 · 3 comments
Closed
1 of 4 tasks

koa integration overwrites content-type of response #1134

mrl5 opened this issue Nov 11, 2021 · 3 comments

Comments

@mrl5
Copy link
Contributor

mrl5 commented Nov 11, 2021

This applies only to koa (express and hapi properly return content-type) and is related to this known koa bug koajs/koa#1120 - I've discovered it while working on PR #1129 (#1129 (review))

Sorting

  • I'm submitting a ...

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasn't already been submit

Expected Behavior

koa returns custom content-type just like express and hapi currently does

Current Behavior

koa ignores this.setHeader('content-type', 'application/vnd.mycompany.myapp.v2+json'); and returns application/json

Possible Solution

as described in koajs/koa#1120 (comment) by @brunoabreu

I can make it work if I write the header after setting the body. It only affects json responses.

so the solution is as simple as this:

diff --git a/packages/cli/src/routeGeneration/templates/koa.hbs b/packages/cli/src/routeGeneration/templates/koa.hbs
index 6716837..b2d2ee9 100644
--- a/packages/cli/src/routeGeneration/templates/koa.hbs
+++ b/packages/cli/src/routeGeneration/templates/koa.hbs
@@ -204,8 +204,6 @@ export function RegisterRoutes(router: KoaRouter) {
 
     function returnHandler(context: any, next: () => any, statusCode?: number, data?: any, headers: any={}) {
         if (!context.headerSent && !context.response.__tsoaResponded) {
-            context.set(headers);
-
             if (data !== null && data !== undefined) {
                 context.body = data;
                 context.status = 200;
@@ -217,6 +215,7 @@ export function RegisterRoutes(router: KoaRouter) {
                 context.status = statusCode;
             }
 
+            context.set(headers);
             context.response.__tsoaResponded = true;
             return next();
         }

Steps to Reproduce

  1. use koa as a server
  2. use this controller:
@Route('MediaTypeTest')
export class MediaTypeTestController extends Controller {
  @SuccessResponse('202', 'Accepted')
  @Post('Custom')
  async postCustomProduces(@Body() model: UserRequestModel): Promise<UserResponseModel> {
    const body = { id: model.name.length, name: model.name };
    this.setStatus(202);
    this.setHeader('content-type', 'application/vnd.mycompany.myapp.v2+json'); // express and hapi returns it, koa ignores
    return body;
  }
}
  1. generate routes
  2. do request (e.g. with curl)
$ curl -i \
    -H 'content-type: application/json' \
    -X POST -d '{"name": "foo"}' \
    http://localhost:3000/MediaTypeTest/Custom

Context (Environment)

Version of the library: 3.14.0
Version of NodeJS: v14.17.6

  • Confirm you were using yarn not npm:
    [x] I confirm I've used yarn

Detailed Description

I think everything was described already

Breaking change?

hard to tell, I guess not

@mrl5
Copy link
Contributor Author

mrl5 commented Nov 11, 2021

I can provide a fix if maintainers agree that it's a bug that can be fixed as proposed

@WoH
Copy link
Collaborator

WoH commented Nov 12, 2021

Seems like we should obviously fix this, can you open a PR?

@mrl5
Copy link
Contributor Author

mrl5 commented Nov 12, 2021

Seems like we should obviously fix this, can you open a PR?

how about pushing additional commit that fixes it to the PR #1129 ? this is kind of related and additional benefit is that I can introduce integration tests :)

@WoH WoH closed this as completed in 3ba0549 Nov 15, 2021
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

No branches or pull requests

2 participants