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

The description of the ApiProperty decorator that declared enumName overwrites each other. #3101

Closed
3 of 4 tasks
0816i opened this issue Oct 14, 2024 · 1 comment
Closed
3 of 4 tasks

Comments

@0816i
Copy link

0816i commented Oct 14, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I declared dto as below.

enum TestEnum {
    A = "A",
    B = "B"
}

class EnumTest {
  @ApiProperty({
    enum: TestEnum,
    enumName: 'TestEnum',
    isArray: true,
    description: 'testEnum of EnumTest'
    default: [],
  })
  readonly testEnum: TestEnum[] = [];
}

class AnotherEnumTest {
  @ApiProperty({
    enum: TestEnum,
    enumName: 'TestEnum',
    isArray: true,
    description: 'testEnum of AnotherEnumTest'
  })
  readonly testEnum: TestEnum[] = []
}

But I got the yaml as below.

components:
  schemas:
    TestEnum:
      type: string
      default: [] # It cause error on openAPI specification.
      description: testEnum of AnotherEnumTest
      enum:
        - A
        - B
# .....
    EnumTest
      type: object
      properties:
        testEnum:
          type: array
          items:
            $ref: "#/components/schemas/TestEnum"
    AnotherEnumTest
      type: object
      properties:
        testEnum:
          type: array
          items:
            $ref: "#/components/schemas/TestEnum"

I know that ['description', 'deprecated', 'default'] can be declared as a field of specific properties. I think that It does not seem appropriate to omit these three fields on EnumTest definition and declare metadata of EnumTest's property on TestEnum's schema.

Minimum reproduction code

https://gist.github.com/0816i/67866e4c94fe0825282b53cde8de3d16

Steps to reproduce

  1. Declare ApiProperty with the same enumName in different classes.
  2. Inside the decorator, we use additionalProperties such as description, default, etc. that go from openAPI schema to enum's.
  3. Values such as description or default that you want to apply to the field of the class to which the API property decorator is applied are applied as the field of the top schema, and if multiple additional fields are attached to the same enumName, this is overwritten.
  4. You'd expect the options in Api Property to be basically a description of the field in that class, but unfortunately it doesn't work that way.

Expected behavior

A class called EnumTest has a property called testEnum, which expects TestEnum to enter the form of an array.
If so, I want the yaml as below.

components:
  schemas:
    TestEnum:
      type: string
      enum:
        - A
        - B
# .....
    EnumTest
      type: object
      properties:
        testEnum:
          type: array
          default: [],
          description: testEnum of EnumTest
          items:
            $ref: "#/components/schemas/TestEnum"
    AnotherEnumTest
      type: object
      properties:
        testEnum:
          type: array
          default: []
          description: testEnum of AnotherEnumTest
          items:
            $ref: "#/components/schemas/TestEnum"

In my opinion, if we want to field description and default for enum, it looks good to create another field that is isolated from property.

Package version

7.4.2

NestJS version

10.4.1

Node.js version

20.12.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

It relate on #2816

Please reconsider your decision on using the additional field for enum.

@kamilmysliwiec
Copy link
Member

#3123

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

2 participants