Tsed: Array in query params is always required

Created on 3 Mar 2018  路  9Comments  路  Source: tsedio/tsed

Informations

Type |聽Version
---|---
Bug | 4.0.6


Description

If I set an array of objects as the type for a query param then it has to be provided or an error is raised. Setting default value on the param doesn't work as a workaround but changing the type to any does.


@Get('/')
    @Returns(404, { description: 'Not found' })
    @Returns(200, { description: 'Success', type: boolean })
    @Returns(500, { description: 'Error: Internal Server Error' })
    @Summary('Returns true')
    async someFunction(
        @PathParams('somePathParam') somePathParam: string,
        @QueryParams('someQueryParam') someQueryParam: Array<string>
    ): Promise<any> {
        return true;
    }

response is:

{
    "name": "BAD_REQUEST",
    "type": "HTTP_EXCEPTION",
    "status": 400,
    "errorMessage": "Bad request on parameter \"request.query.someQueryParam\".\nCannot convert undefined or null to object",
    "dataPath": "someQueryParam",
    "requestType": "QueryParamsFilter"
}
bug

All 9 comments

Hi @Ravendarksky
Thanks for your issue. I'll fix that ASAP ;)
Romain

@Romakita no problem, Sorry that I'm only reporting things and asking questions for now!

Normally it's fixed with the latest version (4.0.7). @tsed/swagger will generate the right schema and enable the form to fill this query param in SwaggerUI.

You can try this example:

@Get('/')
    @Returns(404, { description: 'Not found' })
    @Returns(200, { description: 'Success', type: boolean })
    @Returns(500, { description: 'Error: Internal Server Error' })
    @Summary('Returns true')
    async someFunction(
        @PathParams('somePathParam') somePathParam: string,
        @QueryParams('someQueryParam', String) someQueryParam: Array<string>
    ): Promise<any> {
        return true;
    }

I've added the String type on the decorator QueryParams ;)

See you,
Romain

Hi @Romakita,

Thanks for the fix! The swagger now generates the correct documentation which is awesome.

However there is still an underlying bug preventing me moving away from my Array stopgap. Using Array makes my query parameter mandatory. I cannot hit my endpoint without providing it or an exception is thrown. I've tried a few workarounds as shown below.

@Get('/')
    @Returns(404, { description: 'Not found' })
    @Returns(200, { description: 'Success', type: boolean })
    @Returns(500, { description: 'Error: Internal Server Error' })
    @Summary('Returns true')
    async someFunction(
        @PathParams('somePathParam') somePathParam: string,
        @QueryParams('someQueryParam', String) someQueryParam?: Array<string>
    ): Promise<any> {
        return true;
    }
@Get('/')
    @Returns(404, { description: 'Not found' })
    @Returns(200, { description: 'Success', type: boolean })
    @Returns(500, { description: 'Error: Internal Server Error' })
    @Summary('Returns true')
    async someFunction(
        @PathParams('somePathParam') somePathParam: string,
        @QueryParams('someQueryParam', String) someQueryParam: Array<string> = []
    ): Promise<any> {
        return true;
    }

Both will return the error:


{
    "name": "HttpException",
    "type": "HTTP_EXCEPTION",
    "status": 400,
    "message": "Bad request on parameter \"request.query.someQueryParam\".\nCannot convert undefined or null to object"
}

What do you think?

Can you send me your request ?
I need to know what are the parameters sent ^^

@Romakita I've attached the request here. Basically I'm not providing the query param at all but would still expect the endpoint to work

GET /rest/example/endpoint/somePathParam
content-type: application/json
cache-control: no-cache
postman-token: 22b383bd-5309-44c9-80f6-d8334c425855
authorization: Bearer myBearerToken
user-agent: PostmanRuntime/7.1.1
accept: */*
host: localhost:9000
accept-encoding: gzip, deflate
HTTP/1.1 400
status: 400
x-powered-by: Express
access-control-allow-origin: *
content-type: application/json; charset=utf-8
content-length: 171
etag: W/"ab-1f9O6nGBX+xam3x0tmW9P1fNDFM"
vary: Accept-Encoding
date: Wed, 07 Mar 2018 10:19:49 GMT
connection: keep-alive
{"name":"HttpException","type":"HTTP_EXCEPTION","status":400,"message":"Bad request on parameter \"request.query. someQueryParam\".\nCannot convert undefined or null to object"}

Thanks again!

Thanks i'll fix that :)

PR #259 will fix your bug ;)

Just upgraded to v4.4.0 and can confirm the fix is working! Thanks so much for all your continued hard work on this great package.

Was this page helpful?
0 / 5 - 0 ratings