Skip to content
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

[BUG] OAS3 query parameter style deepObject is not applied when QueryParams takes whole Model #2309

Open
1 task
denis-maiorov-brightsec opened this issue Apr 11, 2023 · 2 comments
Assignees

Comments

@denis-maiorov-brightsec

Information

  • Version: 7.20.0
  • Packages: @tsed/schema, @tsed/swagger

As described in the docs deepObject style should be applied to parameter if it's an object. It works as expected for single params in controller like QueryParams('param') but not when all params are combined into one model QueryParams()

Example

@Generics('T')
export class QueryParamBetweenFilter<T> {
  @Description('Greater than constraint')
  @Property('T')
  public gt?: T;

  @Description('Greater than or equal constraint')
  @Property('T')
  public gte?: T;

  @Description('Less than constraint')
  @Property('T')
  public lt?: T;

  @Description('Less than or equal constraint')
  @Property('T')
  public lte?: T;
}

export class Filters {
  @GenericOf(number().integer())
  public param?: QueryParamBetweenFilter<number>;

}

@Controller({path: '/test'})
class TestDeepObjectCtrl {
  @Get('/works')
  public get(@QueryParams("param") @GenericOf(number().integer()) q: QueryParamBetweenFilter<number>) {

  }

  @Get('/doesNotWork')
  public get(@QueryParams() filters: Filters) {

  }
}

Acceptance criteria

  • Using QueryParams with Model should produce deepObject style for object query params present in the Model
@Romakita
Copy link
Collaborator

Romakita commented Apr 22, 2023

Hello @denis-maiorov-brightsec

@tsed/schema doesn't detect the model with deeps property in this case. But the given example is too complex because it use generics.

I suggest to create a simpler use case with some rules/comments:

class Field {
   type: string;
   value: string;
}

export class QueryParamsComplexModel {  
  @CollectionOf(string)
  public select: string[]; // this prop can trigger a deepObject style but works without eg: "select=test&select=test2" or "select[0]=test&select[1]=test2"
  
  @CollectionOf(Field)
  public fields: Fields[]; // this prop must trigger a deepObject style eg "fields[type]=color&fields[value]=yellow"
}

export class QueryParamsPageModel {
  public page: number; // this prop shouldn't trigger a deepObject style
}

Controllers:

@Controller({path: '/products'})
class ProductsCtrl {
  @Get('/scenario1')
  public scenario1(@QueryParams() filters: QueryParamsComplexModel) { 
     // generate params with `style: deepObject`
  }
  
    @Get('/scenario2')
  public scenario2(@QueryParams() filters: QueryParamsPageModel) { 
     // generate params without`style: deepObject`
  }
}

The code to apply style is here: https://github.com/tsedio/tsed/blob/production/packages/specs/schema/src/domain/JsonParameter.ts#L85

Test is here:
https://github.com/tsedio/tsed/blob/production/packages/specs/schema/test/deep-object-query-params.spec.ts

See you

@stale
Copy link

stale bot commented Aug 12, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: To do
Development

No branches or pull requests

2 participants