swagger icon indicating copy to clipboard operation
swagger copied to clipboard

enumName in ApiParam not creating enum schemas

Open Sharakai opened this issue 3 years ago • 1 comments

I'm submitting a...


[x] Regression 
[ ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Using the following:

@ApiParam({
  name: "type",
  enum: MyExampleEnum,
  enumName: "MyExampleEnum",
})

doesn't result in a new "MyExampleEnum" schema.

This leads to problems in client-side code-gen, as each time this ApiParam enum is used results in a new enum per method, per controller, rather than deduplicating the enum by re-use of the schema.

Expected behavior

A schema object for "MyExampleEnum" should be created, to be referenced in the OpenAPI schema as { $ref: "#/components/schemas/MyExampleEnum" }

This is as defined on the docs (https://docs.nestjs.com/openapi/types-and-parameters#enums-schema):

The enumName property enables @nestjs/swagger to turn CatBreed into its own schema which in turns makes CatBreed enum reusable.

Regression cause

This change: https://github.com/nestjs/swagger/commit/021f2e9d6a9181f79e897b730d1f09a58c2517ac#diff-bd4375f8c339aca69690041a14da31752c1d4707eba6eb1129e5922454d3c7d4 In particular, moving the !isBodyParameter(param) check below the this.isPrimitiveType(param.type).

As a TypeScript enum is either a number or string, the type metadata will always pass the isPrimitiveType check. However, it needs to go into the this.createQueryOrParamSchema in order to call this.createEnumParam and create the enum schema.

This used to happen before @nestjs/swagger v4.7.3, but these 2 checks have been swapped round and it now doesn't enter this function to create the enum schema.

Minimal reproduction of the problem with instructions

Repro: https://github.com/Sharakai/nestjs-swagger-enumname-repro

  1. git clone [email protected]:Sharakai/nestjs-swagger-enumname-repro.git
  2. npm run start:dev
  3. Go to http://localhost:3000/api
  4. The swagger API should include the MyExampleEnum schema, but it does not.

You can also verify that the this.isPrimitiveType check is the issue by adding the following into the @ApiParam decorator:

type: () => MyExampleEnum,

as this'll stop it from being a primitive and make it correctly create the enum schema. Check http://localhost:3000/api and you'll see the "MyExampleEnum" schema now.

What is the motivation / use case for changing the behavior?

We have client code-gen for our NestJS application, but enums are not generated correctly if they're included as @ApiParams.

Environment


Nest version:
@nestjs/core     8.0.5
@nestjs/swagger  5.0.9


For Tooling issues:
- Node version: v14.15.1
- Platform:  Mac

Others:

Sharakai avatar Aug 02 '21 18:08 Sharakai

Would you like to create a PR for this issue?

kamilmysliwiec avatar Aug 03 '21 09:08 kamilmysliwiec

@kamilmysliwiec PR created https://github.com/nestjs/swagger/pull/1619

mateusppereira avatar Jan 17 '23 17:01 mateusppereira

https://github.com/nestjs/swagger/pull/1619

kamilmysliwiec avatar Nov 08 '23 10:11 kamilmysliwiec

Shouldn't this issue remain open until the PR for it is merged?

zbarbuto avatar Nov 13 '23 03:11 zbarbuto

@kamilmysliwiec can you merge the PR please?

mateusppereira avatar Nov 22 '23 18:11 mateusppereira

@kamilmysliwiec Please

hinsxd avatar Jan 15 '24 10:01 hinsxd