cyclosproject / ng-openapi-gen

An OpenAPI 3.0 codegen for Angular
MIT License
397 stars 132 forks source link

Optional query arguments #7

Closed Dasone closed 5 years ago

Dasone commented 5 years ago

Arguments that are posted in params can be optional, which is nice. But there's no handling if they're optional or not when sending a request. This leads for a invalid query where the rest api back end can't handle the request.

For example:

getListOfNumbers$Response(params?: {
 id?: number,
 typeOfNumber?: string
}): Observable<StrictHttpResponse<Array<number>> {
 const rb = new RequestBuilder(...);
 if (params) {
  rb.query('id', params.id);
  rb.query('typeOfNumber', params.typeOfNumber);
 }
 // More code here...
}

If we only request the id, the url would look something like this: http://backend-domain/numbers?id=1&typeOfNumber=

A proposal is to check these optional arguments like this:

getListOfNumbers$Response(params?: {
  id?: number,
  typeOfNumber?: string
}): Observable<StrictHttpResponse<Array<number>> {
 const rb = new RequestBuilder(...);
 if (params) {
  if (params.id) {
    rb.query('id', params.id);
  }
  if (params.typeOfNumber) {
    rb.query('typeOfNumber', params.typeOfNumber);
  }  
 }
 // More code here...
}
luisfpg commented 5 years ago

Was fixed in the RequestBuilder instead. Published on 0.2.2-a (on 0.2.2 was mistakenly publishing all test resources, increasing the bundle size).