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.

How to make object query parameters

See original GitHub issue

I’m trying to figure out how to make my ApiModelProperty decorators so that Swagger would work with objects as query parameters. My case is something like this:

class PersonQueryDto {
  @ApiModelProperty({
    type: QueryFilterDto,
    isArray: true,
    example: [{ field: 'age', comparator: 'gt', value: 25 }],
  })
  readonly filters: QueryFilterDto[];
}

class QueryFilterDto {
  @ApiModelProperty({
    type: 'string',
  })
  readonly field: string; 

  @ApiModelProperty({
    type: 'string',
  })
  readonly comparator: string; 

  @ApiModelProperty({
    type: 'any',
  })
  readonly value: any; 
}

The swagger UI, however, ends up looking like this

image

And it doesn’t know how to deserialise the object passed.

What’s the trick?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

15reactions
max-resultscommented, May 19, 2021

You can do something like this:

export class OrderDto {
	@IsEnum(SORT)
	@IsOptional()
	@Expose()
	@ApiPropertyOptional({enum: SORT, name: 'orderBy[createdAt]'})
	createdAt?:  @SORT;

	@IsEnum(SORT)
	@IsOptional()
	@Expose()
	@ApiPropertyOptional({enum: SORT, name: 'orderBy[updatedAt]'})
	updatedAt?: SORT;

	@IsEnum(SORT)
	@IsOptional()
	@Expose()
	@ApiPropertyOptional({enum: SORT, name: 'orderBy[name]'})
	name?: SORT;
}

export class CollectionDto  {
        // ......

	@ValidateNested()
	@IsOptional()
	@ApiPropertyOptional({type: () => OrderDto})
	@Type(() => OrderDto)
	@Expose()
	orderBy?: OrderDto;
}

example

7reactions
MarZabcommented, Mar 26, 2021

Highjacking this thread since it helped me figure out how to do a similar task with a simple object with full Swagger support:

Example: ?filters[name]=Marko&filters[age]=21

    ApiExtraModels(QueryFilterDto),
    ApiQuery({
      required: false,
      name: 'filters',
      style: 'deepObject',
      explode: true,
      type: 'object',
      schema: {
        $ref: getSchemaPath(QueryFilterDto),
      },
    }),

Full example here: https://gist.github.com/MarZab/c6311f83dec6401966e847c55d81a9bb

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Turn an Object into Query String Parameters in ...
As a JavaScript developer, you'll often need to construct URLs and query string parameters. One sensible way to construct query string ...
Read more >
Query-string encoding of a JavaScript object - Stack Overflow
This method converts a JavaScript object into a URI query string. It also handles nested arrays and objects (in Ruby on Rails and...
Read more >
How to convert an object to a query string using JavaScript
To convert an object to a query string in older browsers: Use the Object.keys() method to get all object's keys as an array....
Read more >
Objects in query params - Medium
Objects in query params. There is a debate over how objects should be represented in the “query” part of URIs. Some people prefer...
Read more >
Convert an Object to a Query String using JavaScript
Use the URLSearchParams() constructor to convert an object to a query string, e.g. new URLSearchParams(obj).toString() . Calling the toString() method on ...
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