Skip to content

Using this.constructor.name is interpreted as default Function for enum #3338

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

Closed
2 of 4 tasks
KiwiKilian opened this issue Mar 7, 2025 · 3 comments
Closed
2 of 4 tasks

Comments

@KiwiKilian
Copy link

KiwiKilian commented Mar 7, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

To transfer the __caslSubjectType__ between Backend and Frontend we use this approach in our base entity, which all other CASL relevant entities inherit from:

@ApiProperty({
  enum: SubjectEnum,
  enumName: 'Subject',
})
readonly __caslSubjectType__: SubjectEnum = this.constructor.name as SubjectEnum;

This results in the following OpenAPI definition:

  "__caslSubjectType__": {
    "default": "Function",
    "allOf": [
      {
        "$ref": "#/components/schemas/Subject"
      }
    ]
  },

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-u7s4phpj?file=src%2Fdto%2Fsome.dto.ts&initialpath=/api

Steps to reproduce

This DTO is enough to trigger the behavior in conjunction with the @nestjs/swagger CLI plugin enabled.

import { ApiExtraModels, ApiProperty } from '@nestjs/swagger';

enum SubjectEnum {
  SomeDto = 'SomeDto',
}

export class SomeDto {
  @ApiProperty({ enum: SubjectEnum, enumName: 'Subject' })
  readonly __caslSubjectType__: SubjectEnum = this.constructor
    .name as SubjectEnum;
}

Expected behavior

There should be no default. Also there seems no way to override/remove this default. Only this works setting it to null:

@ApiProperty({
  enum: SubjectEnum,
  enumName: 'Subject',
  default: null,
})

Package version

11.0.6

NestJS version

11.0.11

Node.js version

22.14.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue?

@KiwiKilian
Copy link
Author

I can try, where should I start looking into this?

@mag123c
Copy link
Contributor

mag123c commented Apr 30, 2025

@kamilmysliwiec @KiwiKilian
The schema control has been strengthened by introducing the skipDefaultValues option, which lets users omit the default field when no initializer is present—avoiding unintended changes to existing schemas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants