Skip to content
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

fix(): Derive enum types correctly when defined as an array #2615 #2647

Conversation

IodizedGabe
Copy link
Contributor

If an enum property is decorated with isArray: true, its type should be derived from the type of the enum item, rather than as an array.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • 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: #2615

What is the new behavior?

Enum schemas should now use the type of enum itself, regardless of whether it was declared as an array or not.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

If an enum property is decorated with `isArray: true`, its type should be derived
from the type of the enum item, rather than as an `array`.
@@ -42,21 +48,36 @@ describe('SchemaObjectFactory', () => {
class Person {
@ApiProperty({ enum: Role, enumName: 'Role' })
role: Role;

@ApiProperty({ enum: Role, enumName: 'Role', isArray: true })
roles: Role[];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A similar issue wasn't detected here with roles likely because role was processed first, where it isn't defined as an array.

groups: Group[];

@ApiProperty({ enum: Ranking, enumName: 'Ranking', isArray: true })
rankings: Ranking[];
Copy link
Contributor Author

@IodizedGabe IodizedGabe Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also check that number-based enums retain the correct type.

@kamilmysliwiec kamilmysliwiec merged commit ecc09e0 into nestjs:master Oct 4, 2023
@IodizedGabe IodizedGabe deleted the fix/issue-2615-fix-array-based-enum branch October 5, 2023 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants