NestJS Swagger UI is duplicating the Authorization field - Stack Overflow

admin2025-04-28  2

I am using the @Headers("Authorization") annotation to inject that header into a method inside a TypeScript class, and it works as expected. However, NestJS is using this annotation and presenting it as a required header field, but then does not actually send that field in the API call.

I have main.ts configured so that the swagger UI page has the "Authorize" button, and entering a value there causes the token to be sent correctly.

const config = new DocumentBuilder()
        .setTitle("Some API")
        .setDescription("The API")
        .setVersion('1.0')
        .addBearerAuth({
            type: "http",
            scheme: "bearer",
            bearerFormat: "JWT",
            in: "header",
            name: "JWT",
            description: "Enter your Bearer token",
        }, "Authorization")
        .addSecurityRequirements("Authorization")
        .build();
    const documentFactory = () => SwaggerModule.createDocument(app, config);
    SwaggerModule.setup("v1/api", app, documentFactory);

However, that makes the value under Parameters unnecessary and even wrong (since it isn't sent anyway). The screenshot below left the Authorization button at the top of the screen blank to illustrate that the value in Parameters is not useful.

@Controller()
@Injectable()
export class UserCredentialController {
@Get(`/v1/auth/readlogin`)
async getOwnUserLoginInfo(@Headers("Authorization") authHeader: string) {
    if (!authHeader) {
        throw new UnauthorizedException("No authorization header found");
    }
    // Rest of code trimmed
}
// omitted

I have tried various combinations of @Api annotations to no avail. Ideally there would be a way to suppress the unused Parameters field since it's injected. What am I missing?

Posts that are similar but didn't solve the problem:

  • Swagger UI not sending Authorization header despite configuration in NestJS
  • @nestjs/swagger does not set authorization headers

I am using the @Headers("Authorization") annotation to inject that header into a method inside a TypeScript class, and it works as expected. However, NestJS is using this annotation and presenting it as a required header field, but then does not actually send that field in the API call.

I have main.ts configured so that the swagger UI page has the "Authorize" button, and entering a value there causes the token to be sent correctly.

const config = new DocumentBuilder()
        .setTitle("Some API")
        .setDescription("The API")
        .setVersion('1.0')
        .addBearerAuth({
            type: "http",
            scheme: "bearer",
            bearerFormat: "JWT",
            in: "header",
            name: "JWT",
            description: "Enter your Bearer token",
        }, "Authorization")
        .addSecurityRequirements("Authorization")
        .build();
    const documentFactory = () => SwaggerModule.createDocument(app, config);
    SwaggerModule.setup("v1/api", app, documentFactory);

However, that makes the value under Parameters unnecessary and even wrong (since it isn't sent anyway). The screenshot below left the Authorization button at the top of the screen blank to illustrate that the value in Parameters is not useful.

@Controller()
@Injectable()
export class UserCredentialController {
@Get(`/v1/auth/readlogin`)
async getOwnUserLoginInfo(@Headers("Authorization") authHeader: string) {
    if (!authHeader) {
        throw new UnauthorizedException("No authorization header found");
    }
    // Rest of code trimmed
}
// omitted

I have tried various combinations of @Api annotations to no avail. Ideally there would be a way to suppress the unused Parameters field since it's injected. What am I missing?

Posts that are similar but didn't solve the problem:

  • Swagger UI not sending Authorization header despite configuration in NestJS
  • @nestjs/swagger does not set authorization headers
Share Improve this question edited Jan 13 at 18:16 MattW asked Jan 9 at 18:34 MattWMattW 8228 silver badges12 bronze badges 4
  • add the controller class for this API in the question with the annotaations – DMabulage Commented Jan 12 at 3:25
  • @DMabulage, see the edit, above. – MattW Commented Jan 13 at 18:17
  • 1 Opened a bug report: github.com/nestjs/swagger/issues/3252 – MattW Commented Jan 20 at 18:01
  • github.com/swagger-api/swagger-ui/issues better report it here – DMabulage Commented Jan 21 at 10:19
Add a comment  | 

1 Answer 1

Reset to default 0
@Controller()
@ApiBearerAuth()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  @ApiHeader({
    name: 'authorization',
    required: false
  })
  async getHello(@Headers('authorization') authHeader: string) {
    if (!authHeader) {
      return 'No token provided';
    }

    return authHeader;
  }
}

add @ApiBearerAuth() for top of the class, and try without @Injectable()

转载请注明原文地址:http://anycun.com/QandA/1745780339a91192.html