question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fix: Enum in @QueryParams and @QueryParam: Value cannot be parsed into JSON

See original GitHub issue

Some 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:open
  • Created 4 years ago
  • Reactions:15
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
miladr0commented, Jun 20, 2022

same problem I cant send something like comment/b481d670 from the front, unless wrap it with double quotes: comment/"b481d670"

1reaction
AntonisFKcommented, Mar 24, 2020

Im getting a the same error with union types (string | string[]) or type any. 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found