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

Prop isArray is incorrectly added when using @ApiQuery decorator with enum values #1221

Closed
tourlbr opened this issue Feb 21, 2021 · 3 comments · Fixed by #1223
Closed

Prop isArray is incorrectly added when using @ApiQuery decorator with enum values #1221

tourlbr opened this issue Feb 21, 2021 · 3 comments · Fixed by #1223

Comments

@tourlbr
Copy link

tourlbr commented Feb 21, 2021

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Applying an APIQuery decorator to a controller method which uses an enum:

@ApiQuery({
    name: 'pagingStrategy',
    type: 'enum',
    required: false,
    enum: ['withCount', 'noCount'],
  })

produces json:

{
    "name": "pagingStrategy",
    "required": false,
    "in": "query",
    "isArray": false,
    "schema": {
        "enum": [
            "withCount",
           "noCount"
        ],
        "type": "string"
    }
}

When converted to yaml and pasted into the Swagger Editor produces validation errors as the isArray property should not exist.

Manually removing the isArray property in the Swagger Editor solves the issue.

Expected behavior

The isArray property should not be present when a query parameter is an enum value.

Minimal reproduction of the problem with instructions

Minimal Reproduction Repo

Instructions

  1. Using the default nest setup add an ApiQuery decorator to the endpoint method and retrieve the json version of the OpenApi spec (-json in URL).
  2. Paste into the Swagger Editor

What is the motivation / use case for changing the behavior?

To create valid OpenAPI documents.

Environment


Nest version: 7.5.1
 
For Tooling issues:
- Node version: v14.12.0
- Platform:  Mac

@fx-adr
Copy link

fx-adr commented Feb 21, 2021

When fixing this locally for related #1179 I used this yarn patch:

diff --git a/dist/decorators/api-query.decorator.js b/dist/decorators/api-query.decorator.js
index 73ce9283619b232c29f0714aa782066d1dc913fd..3ac8739b00b62bfe227f7ce205de19858c327e40 100644
--- a/dist/decorators/api-query.decorator.js
+++ b/dist/decorators/api-query.decorator.js
@@ -11,14 +11,18 @@ const defaultQueryOptions = {
 function ApiQuery(options) {
     const apiQueryMetadata = options;
     const [type, isArray] = helpers_1.getTypeIsArrayTuple(apiQueryMetadata.type, apiQueryMetadata.isArray);
-    const param = Object.assign(Object.assign({ name: lodash_1.isNil(options.name) ? defaultQueryOptions.name : options.name, in: 'query' }, lodash_1.omit(options, 'enum')), { type,
-        isArray });
+    const param = Object.assign(Object.assign({ name: lodash_1.isNil(options.name) ? defaultQueryOptions.name : options.name, in: 'query' }, lodash_1.omit(options, 'enum')), { type });
     if (enum_utils_1.isEnumArray(options)) {
         enum_utils_1.addEnumArraySchema(param, options);
     }
     else if (enum_utils_1.isEnumDefined(options)) {
         enum_utils_1.addEnumSchema(param, options);
     }
+
+    if(isArray) {
+        param.isArray = isArray;
+    }
+
     return helpers_1.createParamDecorator(param, defaultQueryOptions);
 }
 exports.ApiQuery = ApiQuery;

@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue?

@kamilmysliwiec
Copy link
Member

Let's track this here #1223

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

Successfully merging a pull request may close this issue.

3 participants