New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature(): extract Enum out to its schema #412
Conversation
Ok , so I’ve just finished updated my project, which is quite big, I had about 60 enums. Some where in entities, in responses types, in queries, params. Every single one has correctly been defined under the components section, and reused where needed with the « $ref ». But I've noticed something undesirable :
It behaves normally :
It behaves like this :
The enum name is prefixed by the file's name where it's imported Would you be able to solve this issue ? |
@flogh Thanks for testing. It's super helpful. The unexpected behavior is kind of expected but I wasn't sure. I am able to solve the issue but it's flaky. I'll update the PR but keep in mind that @kamilmysliwiec might have other suggestions. We might actually need to introduce a new |
Ok, no problem :) By the way, nice job ! 👍 |
@flogh |
Ok, working as expected 👏 |
Perhaps I missed something, but how can I control whether |
As @nartc said, this PR makes enums behave like this :
He supposed that we might introduce a new decorator like so :
But I don't know if it's possible |
@kamilmysliwiec My intention for this PR is to not require users to change anything unless they want to have // enum
@ApiProperty({ enum: SomeEnum })
// inline
@ApiProperty({ enum: ['a', 'b', 'c'] }) These two will stay working as expected, no This PR added a lazy-function syntax to @ApiProperty({ enum: () => SomeEnum }) The lazy-function allows I am aware that |
unit tests and e2e test have been updated. |
I'm using this PR for more than a month now on my project, and it's working great. Would it be able to merge it on the master branch ? |
@nartc can we somehow add a way to more explicitly control whether I want to extract an enum or not (additional property)? Instead of implicitly assuming that every lazy-function = extract an enum. |
@kamilmysliwiec Implicitly based on the With that said, if it's still essential that we need to explicitly control the extraction, then instead of relying on You have any suggestions? |
Even though this may sound like an additional, extra overhead, I think this will be more straightforward for people. |
@kamilmysliwiec Alrighty then. It makes the PR even simpler with |
@nartc No prob ! 👍 |
Would be a great addition! |
4dc83c7
to
9c4c7d2
Compare
// beginning of this PR
@ApiProperty({ enum: () => Role }) // Role will be extracted out
// now
@ApiProperty({ enum: Role, enumName: 'Role' }) // Role will be extracted out The motivation for this change was in the discussion in this PR. Mainly, we don't want to change the behavior of existing API, so introducing |
@nartc would you like to create a PR to the docs? :) |
@kamilmysliwiec Will do in couple of hours. |
@nartc awesome, thank you! |
Thanks! |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Current behavior is when an
Enum
is defined and used on aModel
, theEnum
values are put on theproperties
of theModel
itself.This causes Frontend Generation Tools (such as NSwag) to generate multiple
enums
even though they are meant for the same thing.Issue Number: #372
What is the new behavior?
New behavior will expect:enum
property on various decorator (ApiParam
,ApiQuery
,ApiProperty
) to accept a lazy-function syntax.This allowsSwaggerExplorer
to grab the Enum name at run-time which makes it possible to extract the Enum out toschemas
to be reused onParameters
Please note this is an addition, not replacement.enum
property still accepts what it currently accepts. This PR makes it so it will solve the issue stated inCurrent Behavior
withlazy function
syntax.Various decorators that accept
enum
now also acceptenumName
. If you want yourenum
to be store as a schema (reusable) then you need to provideenumName
as well. This implementation might seem a little bit explicit compared to thelazy load function
but its intention is also clearer.Again, although the PR is working, maintain the current passing tests, passing two new tests, my implementation might contain flaws and might go against NestJS's design mental. That said, I'm open to ALL suggestions and feedbacks.
Does this PR introduce a breaking change?
Other information