swagger icon indicating copy to clipboard operation
swagger copied to clipboard

fix(core): isArray always false for enum arrays in plugin metadata

Open ArielPrevu3D opened this issue 3 years ago • 4 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?

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

Enum arrays in plugin-generated metadata never have 'isArray' to true.

Issue Number: 1676

What is the new behavior?

Enum arrays in plugin-generated metadata always have 'isArray' to true.

Does this PR introduce a breaking change?

  • [ ] Yes
  • [x] No

Other information

ArielPrevu3D avatar Nov 24 '21 22:11 ArielPrevu3D

@kamilmysliwiec is an enum array with 1 value supposed to have isArray to true? Seems like the test disagrees. In my mind, this means the API is expecting an array so isArray should be true.

ArielPrevu3D avatar Nov 24 '21 23:11 ArielPrevu3D

Oh, I ran into this.... @kamilmysliwiec Can we please plan to get this merged? @ArielPrevu3D is there any work around to get this going while we wait for the release?

nawaz-pasha avatar May 08 '23 07:05 nawaz-pasha

Oh, I ran into this.... @kamilmysliwiec Can we please plan to get this merged? @ArielPrevu3D is there any work around to get this going while we wait for the release?

This is the workaround we're using. This fixes the schema after generating the schema document.

  // Workaround for enum arrays
  //   https://github.com/nestjs/swagger/issues/1676
  if (document?.components?.schemas) {
    for (const schemaKey in document.components.schemas) {
      const schema = document.components.schemas[schemaKey];
      if (
        'type' in schema &&
        schema.type === 'object' &&
        schema.properties
      ) {
        for (const schemaPropKey in schema.properties) {
          const property = schema.properties[schemaPropKey];
          if ('items' in property && 'enum' in property) {
            delete property.enum;
          }
        }
      }
    }
  }

ArielPrevu3D avatar May 08 '23 14:05 ArielPrevu3D

Thank you @ArielPrevu3D !

nawaz-pasha avatar May 09 '23 03:05 nawaz-pasha