Skip to content

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

@KiwiKilian

Description

@KiwiKilian

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions