nestjs / swagger

OpenAPI (Swagger) module for Nest framework (node.js) :earth_americas:
https://nestjs.com
MIT License
1.7k stars 477 forks source link

Example removed from global parameter #3086

Closed am-burban closed 3 weeks ago

am-burban commented 1 month ago

Is there an existing issue for this?

Current behavior

When adding a global parameter with an example, the example is not carried over to the generated spec (see Gist).

Probably related to #3031

Minimum reproduction code

https://gist.github.com/am-burban/5de53ea95c327896d297a26371fa7940

Steps to reproduce

No response

Expected behavior

Example is added to the generated spec

Package version

7.4.2

NestJS version

10.4.3

Node.js version

18.20.2

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 1 month ago

Would you like to create a PR for this issue?

chetanppatil commented 1 month ago

@am-burban I tried updating to new version and found below things which you need to update.

Let's take example that you shared: https://gist.github.com/am-burban/5de53ea95c327896d297a26371fa7940

In this, to get example mentioned in addGlobalParameters in generated specs, you need to use it like below:

Option-1

const config = new DocumentBuilder()
    .setTitle('API Gateway')
    .setVersion(API_SPEC_VERSION)
    .addGlobalParameters(
      {
        in: 'header',
        required: false,
        name: REQUEST_ID_HEADER_NAME,
        description: 'Request ID used for debugging and tracing.',
        example: '5997c5f0-f9a6-4fdc-a4c3-53b850acca28',
      }
    )
    .build();

Option-2

const config = new DocumentBuilder()
    .setTitle('API Gateway')
    .setVersion(API_SPEC_VERSION)
    .addGlobalParameters(
      {
        in: 'header',
        required: false,
        name: REQUEST_ID_HEADER_NAME,
        description: 'Request ID used for debugging and tracing.',
        schema: { type: 'string', minLength: 1, maxLength: 100, example: '5997c5f0-f9a6-4fdc-a4c3-53b850acca28' },
      }
    )
    .build();

Please try this, hope it solves your issue. I tried and both ways its working.