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

Swagger decorator @ApiHeader is not including header on request #2906

Closed
3 of 15 tasks
douglasjunior opened this issue Apr 2, 2024 · 4 comments
Closed
3 of 15 tasks

Comments

@douglasjunior
Copy link

douglasjunior commented Apr 2, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Swagger decorator @ApiHeader is not including header on request

Code

@Controller('template')
export class TemplateController {

  @Get('test')
  @ApiHeader({
    name: 'accept',
  })
  async test(@Headers('accept') acceptHeader: string) {

    return `ok: ${acceptHeader}`;
  }

}

Result

image

Minimum reproduction code

https://codesandbox.io/p/devbox/green-cache-5xjgvc

Steps to reproduce

Just create a new NestJS project and add a new Controller like TemplateController above.

Expected behavior

Expect that Swagger UI to include the proper accept value on request.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

@nestjs/swagger

NestJS version

10.3.3

Packages versions

    "@nestjs/common": "10.3.3",
    "@nestjs/config": "3.2.0",
    "@nestjs/core": "10.3.3",
    "@nestjs/platform-express": "10.3.3",
    "@nestjs/swagger": "7.3.0",
    "class-transformer": "0.5.1",
    "class-validator": "0.14.1",

Node.js version

18.12.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@kamilmysliwiec kamilmysliwiec transferred this issue from nestjs/nest Apr 3, 2024
@letsaguiar
Copy link

this problem is specifically related to the 'accept' header. It works fine with other headers, but it doesn't with the accept header

Screenshot from 2024-04-15 11-16-52

Screenshot from 2024-04-15 11-17-20

@douglasjunior
Copy link
Author

Confirmed in the provided example: https://codesandbox.io/p/devbox/green-cache-5xjgvc

image

@micalevisk
Copy link
Member

the OpenAPI spec generated looks like this:

openapi: 3.0.0
paths:
  /test:
    get:
      operationId: AppController_test
      parameters:
        - name: accept
          required: true
          in: header
          schema:
            type: string
      responses:
        '200':
          description: ''
info:
  title: Cats example
  description: The cats API description
  version: '1.0'
  contact: {}
tags:
  - name: cats
    description: ''
servers: []
components:
  schemas: {}

image

And this is related with swagger-api/swagger-ui#5567

@micalevisk
Copy link
Member

so according with the spec, we are not supposed to define "Accept" ourselves. So I'd say that this is more a misuse of @ApiHeader() than a bug on @nestjs/swagger

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants