cyclosproject / ng-openapi-gen

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

Not able to pass query params into the request #112

Open codal-nwadhawana opened 4 years ago

codal-nwadhawana commented 4 years ago

hello,

I am trying to pass query params into GET request from service function which is generated By the package but the query params are not passed in the request.

please help me out this issue.

thanks in advance. :innocent:

My Swagger JSON object is

        "/users/index": {
            "get": {
                "tags": [
                    "Users"
                ],
                "summary": "User Listings",
                "description": "User List",
                "operationId": "api.user.listing",
                "parameters": [
                    {
                        "name": "page",
                        "in": "query",
                        "description": "Page Number",
                        "required": false,
                        "schema": {
                            "type": "integer"
                        }
                    },
                    {
                        "name": "limit",
                        "in": "query",
                        "description": "Records limit",
                        "required": false,
                        "schema": {
                            "type": "integer"
                        }
                    },
                    {
                        "name": "status",
                        "in": "query",
                        "description": "User Status",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "accounttype",
                        "in": "query",
                        "description": "User Account type",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "verification",
                        "in": "query",
                        "description": "Account verification status",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "sortby",
                        "in": "query",
                        "description": "User sort",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "order",
                        "in": "query",
                        "description": "User listing sort by ascending or descending",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "spent_duration",
                        "in": "query",
                        "description": "Credits spent duration",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "search",
                        "in": "query",
                        "description": "Search by Username,  Phone Number, Email",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Ok"
                    },
                    "404": {
                        "description": "No data found"
                    }
                },
                "security": [
                    {
                        "apiAuth": []
                    }
                ]
            }
        }

My request call


let filter_params={status: "Deactivated", limit: "100", search: "Search term"};

        this.api_user_listing_subscribe = this._UserListingService.apiUserListing(filter_params)
        .subscribe (
            suc=>{
                console.log(suc);

            },
            err=>{
                console.log(err);

            }
        );

service functions generated from package

/**
   * Path part for operation apiUserListing
   */
  static readonly ApiUserListingPath = '/users/index';

  /**
   * User Listings.
   *
   * User List
   *
   * This method provides access to the full `HttpResponse`, allowing access to response headers.
   * To access only the response body, use `apiUserListing()` instead.
   *
   * This method doesn't expect any request body.
   */
  apiUserListing$Response(params?: {

    /**
     * Page Number
     */
    page?: number;

    /**
     * Records limit
     */
    limit?: number;

    /**
     * User Status
     */
    status?: string;

    /**
     * User Account type
     */
    accounttype?: string;

    /**
     * Account verification status
     */
    verification?: string;

    /**
     * User sort
     */
    sortby?: string;

    /**
     * User listing sort by ascending or descending
     */
    order?: string;

    /**
     * Credits spent duration
     */
    spent_duration?: string;

    /**
     * Search by Username,  Phone Number, Email
     */
    search?: string;

  }): Observable<StrictHttpResponse<void>> {

    const rb = new RequestBuilder(this.rootUrl, UsersService.ApiUserListingPath, 'get');
    if (params) {

      rb.query('page', params.page, {});
      rb.query('limit', params.limit, {});
      rb.query('status', params.status, {});
      rb.query('accounttype', params.accounttype, {});
      rb.query('verification', params.verification, {});
      rb.query('sortby', params.sortby, {});
      rb.query('order', params.order, {});
      rb.query('spent_duration', params.spent_duration, {});
      rb.query('search', params.search, {});

    }
    return this.http.request(rb.build({
      responseType: 'text',
      accept: '*/*'
    })).pipe(
      filter((r: any) => r instanceof HttpResponse),
      map((r: HttpResponse<any>) => {
        return (r as HttpResponse<any>).clone({ body: undefined }) as StrictHttpResponse<void>;
      })
    );
  }

  /**
   * User Listings.
   *
   * User List
   *
   * This method provides access to only to the response body.
   * To access the full response (for headers, for example), `apiUserListing$Response()` instead.
   *
   * This method doesn't expect any request body.
   */
  apiUserListing(params?: {

    /**
     * Page Number
     */
    page?: number;

    /**
     * Records limit
     */
    limit?: number;

    /**
     * User Status
     */
    status?: string;

    /**
     * User Account type
     */
    accounttype?: string;

    /**
     * Account verification status
     */
    verification?: string;

    /**
     * User sort
     */
    sortby?: string;

    /**
     * User listing sort by ascending or descending
     */
    order?: string;

    /**
     * Credits spent duration
     */
    spent_duration?: string;

    /**
     * Search by Username,  Phone Number, Email
     */
    search?: string;

  }): Observable<void> {

    return this.apiUserListing$Response(params).pipe(
      map((r: StrictHttpResponse<void>) => r.body as void)
    );
  }
luisfpg commented 4 years ago

Everything seems correct. Can you debug in the browser the apiUserListing$Response to check that parameters are being correctly set in the RequestBuilder, and then the RequestBuilder.build call?

codal-nwadhawana commented 4 years ago

hey @luisfpg,

thanks for quick response.

I am getting all the query params info RequestBuilder.build function.

it works when i update this block of code in request-builder.ts

  console.log("this._query.values()",this._query.values());

  for (const queryParam of this._query.values()) {
      httpParams = queryParam.append(httpParams);
 }

to this

  console.log("this._query.values()",this._query.values());

  for (const queryParam of Array.from(this._query.values())) {
      httpParams = queryParam.append(httpParams);
  }

console output image : https://drive.google.com/file/d/1xWxMFkcP8cXABdDwH9w1jFMivpcacmN7/view

I am not able to save above change in my code. as, it will be generated from package.

is there any issue with my code or anything else?

luisfpg commented 4 years ago

I can't find an explanation for this... Which browser are you using? We have used this code for years and the requests always have the parameters correctly. See the https://github.com/cyclosproject/cyclos4-ui (there's a running demo on https://demo-ui.cyclos.org/). And all searches use query parameters...

Vtfelker commented 2 years ago

I experience the same issue, and the problem is that the generated code is the following:

get2$Response(params?: {
  }): Observable<StrictHttpResponse<PageDtoApiModel>> {

    const rb = new RequestBuilder(this.rootUrl, TransactionControllerApiService.Get2Path, 'get');
    if (params) {
    }

    return this.http.request(rb.build({
      responseType: 'json',
      accept: 'application/json'
    })).pipe(
      filter((r: any) => r instanceof HttpResponse),
      map((r: HttpResponse<any>) => {
        return r as StrictHttpResponse<PageDtoApiModel>;
      })
    );
  }

As you can see, the generated code does nothing to the params, so they are not sent out.

luisfpg commented 2 years ago

@Vtfelker please, share the openapi definition (json or yaml) for your operation

Vtfelker commented 2 years ago

{   "get": {     "tags": [       "transaction-controller"     ],     "operationId": "get_2",     "responses": {       "200": {         "description": "OK",         "content": {           "application/json": {             "schema": {               "$ref": "#/components/schemas/PageDTO"             }           }         }       },       "400": {         "description": "Bad Request",         "content": {           "/": {             "schema": {               "$ref": "#/components/schemas/ErrorResponse"             }           }         }       }     }   } }

luisfpg commented 2 years ago

Well, the point is that you're not declaring any parameters in this operation. You need query parameters in order to have them generated.

itekaf commented 1 year ago

@luisfpg I suggest create flag on config file like anyParameters: true | false. Wich add enable to add any parametrs to our services and function.

Becuase I have following problem. I need to send request like this:

http://localhost:1337/api/v1/navigation?populate[mainmenu][populate][0]=submenu

but my swagger json file doesnt allow dynamic params and I can't modify it (because it's autogenerated from CMS)

  "parameters": [
        {
          "name": "sort",
          "in": "query",
          "description": "Sort by attributes ascending (asc) or descending (desc)",
          "deprecated": false,
          "required": false,
          "schema": {
            "type": "string"
          }
        },
        {
          "name": "pagination[withCount]",
          "in": "query",
          "description": "Return page/pageSize (default: true)",
          "deprecated": false,
          "required": false,
          "schema": {
            "type": "boolean"
          }
        },
        {
          "name": "pagination[page]",
          "in": "query",
          "description": "Page number (default: 0)",
          "deprecated": false,
          "required": false,
          "schema": {
            "type": "integer"
          }
        },
        {
          "name": "pagination[pageSize]",
          "in": "query",
          "description": "Page size (default: 25)",
          "deprecated": false,
          "required": false,
          "schema": {
            "type": "integer"
          }
        },
        {
          "name": "pagination[start]",
          "in": "query",
          "description": "Offset value (default: 0)",
          "deprecated": false,
          "required": false,
          "schema": {
            "type": "integer"
          }
        },
        {
          "name": "pagination[limit]",
          "in": "query",
          "description": "Number of entities to return (default: 25)",
          "deprecated": false,
          "required": false,
          "schema": {
            "type": "integer"
          }
        },
        {
          "name": "fields",
          "in": "query",
          "description": "Fields to return (ex: title,author)",
          "deprecated": false,
          "required": false,
          "schema": {
            "type": "string"
          }
        },
        {
          "name": "populate",
          "in": "query",
          "description": "Relations to return",
          "deprecated": false,
          "required": false,
          "schema": {
            "type": "string"
          }
        },
        {
          "name": "filters",
          "in": "query",
          "description": "Filters to apply",
          "deprecated": false,
          "required": false,
          "schema": {
            "type": "object"
          },
          "style": "deepObject"
        },
        {
          "name": "locale",
          "in": "query",
          "description": "Locale to apply",
          "deprecated": false,
          "required": false,
          "schema": {
            "type": "string"
          }
        }
      ],

as result I have following params:

  if (params) {
    rb.query('sort', params.sort, {});
    rb.query('pagination[withCount]', params['pagination[withCount]'], {});
    rb.query('pagination[page]', params['pagination[page]'], {});
    rb.query('pagination[pageSize]', params['pagination[pageSize]'], {});
    rb.query('pagination[start]', params['pagination[start]'], {});
    rb.query('pagination[limit]', params['pagination[limit]'], {});
    rb.query('fields', params.fields, {});
    rb.query('populate', params.populate, {});
    rb.query('filters', params.filters, {"style":"deepObject"});
    rb.query('locale', params.locale, {});
  }

In this case I can't to send my request from augenerated services and I need to create custom, only because I can't send custom params to request :(