typescript-rest icon indicating copy to clipboard operation
typescript-rest copied to clipboard

One-element array in a query parameter not parsed correctly

Open mz8i opened this issue 6 years ago • 3 comments

When a query parameter is an Array<T>, but the query contains just one element (for example /path?words=foo as opposed to /path?words=foo&words=bar) then at run-time the parameter passed to the controller is actually a string, not a one-element array.

In my controller, I solved it by code that feels like it could be included in the library code which parses the parameters?

@GET
@Path('endpoint')
public endpoint(@QueryParam('words') words: string[]): void {
    let wordsArray = words;
    if (typeof wordsArray === 'string') {
        wordsArray = [wordsArray];
    }

    // do something with wordsArray
}

mz8i avatar Jun 06 '19 13:06 mz8i

I can confirm this bug too.

FinPlorer avatar Oct 01 '19 14:10 FinPlorer

this happened to me too, it just works the right way if you send more than one item in the array, otherwise it parses for "type" instead of "type []"

NicholasKuchiniski avatar May 26 '20 17:05 NicholasKuchiniski

I have submitted a PR (https://github.com/thiagobustamante/typescript-rest/pull/147) to address this issue.

nthngalone avatar Oct 03 '20 17:10 nthngalone