routing-controllers-openapi icon indicating copy to clipboard operation
routing-controllers-openapi copied to clipboard

Not able to parse @QueryParams()

Open lgabeskiria opened this issue 6 years ago • 6 comments

class SortOrder {
  [index: string]: 'ASC' | 'DESC'
}

class Query {
  @IsNumber()
  readonly offset: number = 0;

  @IsNumber()
  readonly limit: number = 10;

  @ValidateNested()
  readonly order: SortOrder;
}

class Controller {
  getAll(@QueryParams() query: Query) {
    
  }
}

This example creates one field called query and displays it as json field.

Expected behavior is to have 3 query parameters: offset, limit and order. Order parameter should be of deepObject style.

Example query string:

?limit=10&offset=0&order[firstName]=DESC&order[lastName]=ASC

More info https://swagger.io/docs/specification/serialization/

lgabeskiria avatar Jan 24 '19 13:01 lgabeskiria

Hi, that's bit of a tricky one. So we need to pass a style: "deepObject" property to your Parameter object, and the library is currently lacking this feature, right? One option would be to add a global configuration parameter for defining query parameter style and explode, how'd that work out for you? Or can you think of a way of setting them per-parameter?

Also your SortOrder class might be a problem since I'm not sure whether class-validator can handle index signatures like [index: string]: ....

Thanks for taking the time to open up an issue!

epiphone avatar Jan 25 '19 11:01 epiphone

This should actually be the default behavior. QueryParams should be expanded because it represents the whole query object (or at least part of it) not just one query parameter. I'll take a look and maybe create a PR.

gstamac avatar Apr 15 '19 14:04 gstamac

This is not possible now because routing-controllers-openapi doesn't have information about schemas.

gstamac avatar Apr 15 '19 17:04 gstamac

Do any work arounds exist for this at the moment?

lokeshmohanty avatar Nov 26 '20 06:11 lokeshmohanty

I regenerate the parameters after the spec is generated. In short go through the whole spec and if I find a "query" parameter that has "$ref" set I lookup the schema and replace the parameter.

gstamac avatar Nov 26 '20 17:11 gstamac

I regenerate the parameters after the spec is generated. In short go through the whole spec and if I find a "query" parameter that has "$ref" set I lookup the schema and replace the parameter.

Well that's the best option for now I guess. Thanks a lot.

lokeshmohanty avatar Nov 27 '20 05:11 lokeshmohanty