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

feat(spec): allow additional fields on enum refs #2816

Merged
merged 1 commit into from
Feb 7, 2024

Conversation

Algram
Copy link
Contributor

@Algram Algram commented Feb 2, 2024

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: N/A

What is the new behavior?

If there is an enumName supplied, all the additionalFields are currently just added as a ref-sibling. This is not swagger-spec compliant and therefore gets ignored.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

See this issue for more information: #2806

};
}
// Allow given fields to be part of the referenced enum schema
const additionalParams = ['description', 'deprecated', 'default']
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was not sure if we should just allow all fields, so I added an allowlist here. I did not find any information in the swagger spec on what they technically support on an enum ref field.

@kamilmysliwiec kamilmysliwiec merged commit cf800f2 into nestjs:master Feb 7, 2024
1 check passed
@kamilmysliwiec
Copy link
Member

LGTM

@0816i
Copy link

0816i commented Oct 11, 2024

Is there any reason to assume metadata of ApiProperty as enum's metadata? I have problem by this pr.

I declared dto as below.

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

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

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: []
          items:
            $ref: "#/components/schemas/TestEnum"

But I got the yaml as below.

components:
  schemas:
    TestEnum:
      type: string
      default: [] # It cause error on openAPI specification.
      enum:
        - A
        - B
# .....
    EnumTest
      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.

@0816i
Copy link

0816i commented Oct 11, 2024

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

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

Successfully merging this pull request may close these issues.

3 participants