Skip to content

[typescript-angular2] not required GET parameter is handle as required parameter #3316

@taxpon

Description

@taxpon
Description
  • When I defined get parameter with required: false, generated typescript code handle the parameter as a required parameter.
Swagger-codegen version

2.2.0-SNAPSHOT

Swagger declaration file content or url

Example is here:

/books:
    get:
      tags: [Books]
      operationId: getBooksList
      parameters:
        - name: BookTypeId
          in: path
          required: false
          type: integer
          format: int64
      responses:
        200:
          description: Success
          schema:
            type: array
            items:
              $ref: "#/definitions/Book"

Generated code is here:

    public getBooksList (bookTypeId: number, extraHttpRequestParams?: any ) : Observable<Array<models.Book>> {
        const path = this.basePath + '/books'
            .replace('{' + 'BookTypeId' + '}', String(bookTypeId));

        let queryParameters = new URLSearchParams();
        let headerParams = this.defaultHeaders;
        // verify required parameter 'bookTypeId' is set
        if (!bookTypeId) {
            throw new Error('Missing required parameter bookTypeId when calling getBooksList');
        }
        let requestOptions: RequestOptionsArgs = {
            method: 'GET',
            headers: headerParams,
            search: queryParameters
        };

        return this.http.request(path, requestOptions)
            .map((response: Response) => {
                if (response.status === 204) {
                    return undefined;
                } else {
                    return response.json();
                }
            });
    }

Following parts do not allow skip bookTypeId:

        const path = this.basePath + '/books'
            .replace('{' + 'BookTypeId' + '}', String(bookTypeId));

        ...

        if (!bookTypeId) {
            throw new Error('Missing required parameter bookTypeId when calling getBooksList');
        }
Suggest a Fix

Handle required: false GET parameter correctly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions