Tsed: Query param with array

Created on 1 Nov 2018  Â·  14Comments  Â·  Source: tsedio/tsed

I use TSED version 4.30.1

I configure my controller with an endpoint that contains the next query param:
@QueryParams('questionList', String) questionList: string[]

I also use swagger for that endpoint, and it shows well the option to add multiple items to the questionList.
The problem is that when I add multiple items (in the swagger) to the questionList param:
item: "question1"
item: "question2"
item: "question3"

and invoke the endpoint code, I get the questionList param with only one item that contains all of the questions, and it looks like this:
["question1,question2,question3"]

What should I change to get the list of questions as multiple items in the questionList?
So it will be: ["question1", "question2", "question3"]

bug

All 14 comments

Hello @YehonatanVonage ,

The documentation describe how you can configure the format for Array type. https://swagger.io/docs/specification/2-0/describing-parameters/#array

use collectionFormat with right value. I'll check on my side if this parameter is correctly configured...

See you
Romain

Hello @Romakita,

I have the same issue ! (@tsed/swagger version 4.34.2)
On my controller I have :

image

On swagger

image

And finally on the JSON swagger-specs

image

In the swagger's spec, type must be an "array" and an object "items" must be contain type.

I don't know if it's possible but used

@QueryParams("name", Array(String)) name: string[],
// instead of 
@QueryParams("name", String) name: string[],

Thanks.

@Tlahey I think it's bug from SwaggerService. I'll fix that.

Thank you @Romakita

@Tlahey For me it works fine with the v5.0.7

Can you send me your package.json please ?

I’m not on the latest version, i thinks it’s a bug on version 4.34.2, I upgrate ASAP !
Thank you :)

Hi @Romakita,

I upgrade to the latest version (5.0.7 common / core / di / swagger) and I have always the same issue =/

@QueryParams("name", String) name: string[],

On my swagger-ui-dist (swagger-ui-dist@^3.13.6), I don't have the array.

I will try to do other tests.

Edit:

On OpenApiParamsBuilder function getInQueryParams the variable param.isArray are false.

{
    "_allowedRequiredValues": [],
    "_expression": "arrayName",
    "_index": 2,
    "_name": "QueryParamsFilter",
    "_paramType": "query",
    "_propertyKey": "read",
    "_required": false,
    "_service": class QueryParamsFilter { … },
    "_store": Store {_map: Map(0)},
    "_target": Object {constructor: , read: , create: , …},
    "_type": undefined,
    "_useConverter": true,
    "_useValidation": true,
    "allowedRequiredValues": Array(0),
    "collectionName": "",
    "collectionType": undefined,
    "expression": "arrayName",
    "index": 2,
    "isArray": false,
    "isClass": false,
    "isCollection": false,
    "isDate": false,
    "isObject": false,
    "isPrimitive": false,
    "name": "QueryParamsFilter",
    "paramType": "query",
    "propertyKey": "read",
    "required": false,
    "service": class QueryParamsFilter { … },
    "store": Store,
    "target": class ConfigurationCtrl { … },
    "targetName": "ConfigurationCtrl",
    "type": undefined,
    "typeName": "undefined",
    "useConverter": true,
    "useValidation": true,
    "__proto__": Storable {constructor: , expression: <accessor>, service: <accessor>, …}
}

I have the same issue with others types. If I don't set like this my queryParams

@PathParams("logName") @Required() logName: string // swagger show param like an object
@PathParams("logName", String) @Required() logName: string // swagger show param like a string

Hello @Tlahey
I created a branch with different working examples (With QueryParams and PathParams). You can find examples on this link: https://github.com/Romakita/ts-express-decorators/blob/chore/query-params-swagger-example/integration/getting-started/src/controllers/calendars/CalendarsCtrl.ts

And you can checkout the branch and test locally (and compare with your project). I hope it'll help yo ^^

See you
Romain

If the problem persist I invite you to continue the discussion on Gitter https://gitter.im/Tsed-io/community
See you

Ok, I switch on community :)

Fix: Add on tsconfig.json

        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,

Thanks @Romakita

The UI issue is fixed in Swagger but array is still passed as a single string and there seems to be no way around it.
Currently just parsing the string by ',' to get it to work.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dorian-kwon picture dorian-kwon  Â·  3Comments

vorph1 picture vorph1  Â·  6Comments

haroon407 picture haroon407  Â·  4Comments

m0uneer picture m0uneer  Â·  5Comments

royibernthal picture royibernthal  Â·  3Comments