Skip to content

Array parameters are not deserialized/validated properly #123

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

Closed
marshall007 opened this issue Apr 26, 2017 · 5 comments · Fixed by #677
Closed

Array parameters are not deserialized/validated properly #123

marshall007 opened this issue Apr 26, 2017 · 5 comments · Fixed by #677

Comments

@marshall007
Copy link

marshall007 commented Apr 26, 2017

Given the following decorated parameters on a controller method:

@QueryParam('fields') fields: string[],
@QueryParam('numbers') numbers: number[]

... the behavior of arrays in query parameters is inconsistent. Namely single values are not coerced to an array and the inner values are not coerced or validated against the appropriate type. Some examples:

// GET ?fields=name
// -> ParameterParseJsonError: Value (\"name\") cannot be parsed to JSON

// GET ?fields=foo&fields=bar
console.log(fields) // -> [ "foo", "bar" ]

// GET ?numbers=1&numbers=2
console.log(numbers) // -> [ "1", "2" ] (works, but not deserialized)
@marshall007
Copy link
Author

The second part of this issue regarding type coercion for nested values may not actually be possible. It seems the only type metadata available for array primitives is Array (see microsoft/TypeScript#3015) so we can't know the inner type.

@pleerock are there any other options here? I think class-transformer has a Type decorator to handle the array case? It will be kinda unfortunate if we had to make that a parameter of the Param decorators.

@pleerock
Copy link
Contributor

Right, its not possible to automatically find type of "string[]". You can do it with class-transformer, but your type should be object and inside it you should have your string[] with type set.

Alternative could be to have type option in QueryParam options: @QueryParam("fields", { type: String }), but this is only needed for arrays, Im not sure if this functionality worth it.

@MichalLytek
Copy link
Contributor

You can do it with class-transformer, but your type should be object and inside it you should have your string[] with type set.

So it looks like all the problems with class-validator and class-transformer comes to one solution - #122 😉 I will try to look at the source code to see how much complicated is the implementation of this feature and whether I could help and contribute.

@pleerock
Copy link
Contributor

I'll close it. If someone interested in fixing this issue feel free to create a PR. But I personally think it does not worth it. Better to create object and let class-transfromer to perform proper transformation directly into the object, instead of having each array in a separate query parameter.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants