swagger icon indicating copy to clipboard operation
swagger copied to clipboard

feat(enum): add support for enum varnames and descriptions

Open JoshThor opened this issue 10 months ago • 2 comments

PR Checklist

Please check if your PR fulfills the following requirements:

  • [x] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
  • [x] Tests for the changes have been added (for bug fixes / features)
  • [x] Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • [ ] Bugfix
  • [x] 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?

Currently only the x-enumNames extension is supported for specifying names for enums, however the popular openapi generator only supports the x-enum-varnames extension.

Issue Number: N/A

What is the new behavior?

Add support for the x-enum-varnames and x-enum-descriptions extensions to be added to the schema.

Example:

enum StatusEnum {
  APPROVED = 1,
  PENDING,
  REJECTED
 }

  @ApiProperty({
    description: 'The status enum example',
    enum: StatusEnum,
    enumName: 'StatusEnum',
    'x-enum-varnames': ['APPROVED', 'PENDING', 'REJECTED'],
    'x-enum-descriptions': ['Approved State', 'Pending State', 'Rejected State']
  })
  statusEnum: StatusEnum;

Would generate:

   {
      "StatusEnum ": {
        "type": "number",
        "enum": [
          1,
          2,
          3
        ],
        "description": "The status enum example",
        "x-enum-varnames": [
          "APPROVED",
          "PENDING",
          "REJECTED"
        ],
        "x-enum-descriptions": [
          "Approved State",
          "Pending State",
          "Rejected State"
        ]
      }
   }

Does this PR introduce a breaking change?

  • [ ] Yes
  • [x] No

Other information

JoshThor avatar Feb 20 '25 04:02 JoshThor

cool

kumv-net avatar Apr 01 '25 17:04 kumv-net

Any plans to get this merged? I would like to add descriptions to enum fields.

mohad12211 avatar Oct 25 '25 19:10 mohad12211