swagger icon indicating copy to clipboard operation
swagger copied to clipboard

fix(lib): fix param enum schema create WIP

Open Roytangrb opened this issue 4 years ago • 2 comments
trafficstars

fix #1096 fix retrieve enum values from param metadata while creating new enum schema if enumName is used with isArray

PR Checklist

Please check if your PR fulfills the following requirements:

  • [x] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
  • [ ] Tests for the changes have been added (for bug fixes / features)
  • [ ] Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

[x] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

Issue Number: #1096

Reproduction

a full reproduction repo is available by the issue creator https://github.com/murbanowicz/nest-swagger-1096

//enum
export enum FooEnum {
  BAR = 'BAR',
  FOO = 'FOO',
}
//DTO class
class BuggyQueryDTO {
  @ApiProperty({
    isArray: true,
    enum: FooEnum,
    enumName: 'buggy',
  })
  public readonly foos!: FooEnum[];
}
// controller
  @Get()
  getHello(@Query() query: BuggyQueryDTO): string {
    return this.appService.getHello();
  }

The above code will throw error on SwaggerModule.createDocument(app, options);:

TypeError: Cannot read property 'items'
 of undefined
    at SchemaObjectFactory.createEnumParam (/home/marek/dev/oss/bugs/enum-array-query/
node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:168:31)
    at SchemaObjectFactory.createQueryOrParamSchema (/home/marek/dev/oss/bugs/enum-arr
ay-query/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:51:25)
    at /home/marek/dev/oss/bugs/enum-array-query/node_modules/@nestjs/swagger/dist/ser
vices/schema-object-factory.js:31:29
    at Array.map (<anonymous>)
    at SchemaObjectFactory.createFromModel (/home/marek/dev/oss/bugs/enum-array-query/
node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:20:45)
    at exports.exploreApiParametersMetadata (/home/marek/dev/oss/bugs/enum-array-query
/node_modules/@nestjs/swagger/dist/explorers/api-parameters.explorer.js:33:55)
    at /home/marek/dev/oss/bugs/enum-array-query/node_modules/@nestjs/swagger/dist/swa
gger-explorer.js:73:45
    at Array.reduce (<anonymous>)
    at /home/marek/dev/oss/bugs/enum-array-query/node_modules/@nestjs/swagger/dist/swa
gger-explorer.js:72:99
    at /home/marek/dev/oss/bugs/enum-array-query/node_modules/lodash/lodash.js:13427:3
8

What is the new behavior?

After some study on the src code, this PR might fix the issue, or hopefully give some insights for maintainers to fix the bug.

From my understanding, while @ApiPropertyenumName is used in query DTO, we try to create a enum schema for usage of parameter $ref. Through the createApiPropertyDecorator, https://github.com/nestjs/swagger/blob/35b09075230b65b3c0e582e6caa37737a60797f4/lib/decorators/api-property.decorator.ts#L33 the enum type and values should be available in either param.type, param.enum or param.items.type and param.items.enum (if param value type isArray).

Produced output:

  "components": {
    "schemas": {
      "buggy": {
        "type": "string",
        "enum": [
          "BAR",
          "FOO"
        ]
      }
    }
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "AppController_getHello",
        "parameters": [
          {
            "name": "foos",
            "required": true,
            "in": "query",
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/components/schemas/buggy"
              }
            }
          }
        ],

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

Also notice the related test case https://github.com/nestjs/swagger/commit/e417a7af739a732a8a4d6719d25bcc0f02555be0 does not cover this issue.

Roytangrb avatar Dec 24 '20 05:12 Roytangrb

Why is this PR closed? What is needed to merge this PR? Can I do something to make it happen?

yard2010 avatar Mar 27 '22 16:03 yard2010

Can we please merge this? This is a pretty foundational bug and we're hitting this.

PseudoAICoder avatar Apr 21 '22 21:04 PseudoAICoder

Any workaround?

zzswang avatar Oct 26 '22 01:10 zzswang

I applied the changes with https://www.npmjs.com/package/patch-package as a workaround and they seem to work quite well. @kamilmysliwiec is there any help needed to move this fix forward?

mksony avatar Nov 10 '22 20:11 mksony

It is also a problem for us :( Looking forward to merging the PR.

igrek8 avatar Nov 28 '22 11:11 igrek8

Decorate controller using @ApiExtraModels(EventSearchQuery).

enum City {
  LONDON = "LONDON",
  PARIS = "PARIS",
  BERLIN = "BERLIN",
}

class EventSearchQuery {
  @ApiPropertyOptional({ enum: City, enumName: "City" })
  city?: City;
}

@Controller({ path: "events" })
@ApiExtraModels(EventSearchQuery)
class EventsController {}

igrek8 avatar Dec 21 '22 14:12 igrek8

I believe that we can close this PR now as the other #2303 was merged

micalevisk avatar Jul 22 '23 16:07 micalevisk