swagger
swagger copied to clipboard
enumName in ApiParam not creating enum schemas
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
-
git clone [email protected]:Sharakai/nestjs-swagger-enumname-repro.git
-
npm run start:dev
- Go to http://localhost:3000/api
- 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 @ApiParam
s.
Environment
Nest version:
@nestjs/core 8.0.5
@nestjs/swagger 5.0.9
For Tooling issues:
- Node version: v14.15.1
- Platform: Mac
Others:
Would you like to create a PR for this issue?
@kamilmysliwiec PR created https://github.com/nestjs/swagger/pull/1619
https://github.com/nestjs/swagger/pull/1619
Shouldn't this issue remain open until the PR for it is merged?
@kamilmysliwiec can you merge the PR please?
@kamilmysliwiec Please