fix: Enum in @QueryParams and @QueryParam: Value cannot be parsed into JSON
See original GitHub issueSome code to reproduce issue:
enum LabelType {
Sent = 'Sent',
Inbox = 'Inbox'
}
class Query {
@IsEnum(LabelType)
label: LabelType;
}
@JsonController()
export class ThreadController {
@Get('/threads')
async list(
@QueryParams() query: Query
) {
return "ok";
}
@Get('/threads/one')
async list(
@QueryParam('label') label: LabelType,
) {
return "ok";
}
@Post('/threads')
async postList(
@Body() query: Query
) {
return "ok";
}
}
Issue:
Post method is working as expected, but Get fails with this error:
Http error: Given parameter label is invalid. Value ("Sent") cannot be parsed into JSON.
Possible reason:
Typescript emits “Object” for “design:type” when type is inferred original issue. In that case string enum treats as an object and routing-controllers tries to parse string.
That happens in normalizeParamValue
method of ActionParameterHandler
here. normalizeParamValue
isn’t handles params from @Body
, that’s why Post works as expected.
Workaround:
Until it fixed we can explicitly specify enum type. Not sure if that will not break anything else, but working fine for me:
export class Query {
@Reflect.metadata('design:type', { name: 'string' })
@IsEnum(LabelType)
label: LabelType;
}
For @QueryParam
it’s possible to set parameter’s type:
@QueryParam('label', { type: 'string' }) label: LabelType
Issue Analytics
- State:
- Created 4 years ago
- Reactions:15
- Comments:10 (2 by maintainers)
Top Results From Across the Web
Parse a URI String into Name-Value Collection - java
MultiValueMap<String, String> queryParams = UriComponentsBuilder.fromUriString(url).build().getQueryParams();. UPDATE: UriComponentsBuilder comes from Spring.
Read more >How can I cast a querystring string value to a enum? - MSDN
Hi,. QueryString in the url to pass value to another page can be just as String. You can't pass enum as QueryString. But...
Read more >Is it a bad idea to pass JSON objects on the query string for an ...
We are in JavaScript on both client and server, so parsing the JSON object is not a difficult task. And I realize that...
Read more >29.2 Creating a RESTful Root Resource Class
The @QueryParam annotation is a type of parameter that you can extract for use in your ... If the step value cannot be...
Read more >How to set default enum value for query parameter in RAML 1.0
I had defined the query parameter (with enum value) in a library file (common.raml) and then imported that parameter in the main raml...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
same problem I cant send something like
comment/b481d670
from the front, unless wrap it with double quotes:comment/"b481d670"
Im getting a the same error with union types (
string | string[]
) or typeany
. I must pass double quotes (?search="foo"
) for a single param to work. Im using koa@2.11.0, routing-controllers@0.8.0 typescript@3.8.3 Here is an example repo