Skip to content

[BUG][typescript-fetch] Typescript error in generated code for endpoint returning type boolean #2870

@matthewbpt

Description

@matthewbpt

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used? 4.0.0-beta3 from npm
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

When generating the typescript api client, the generated client has type errors for any methods returning a boolean.

export class UserApi extends runtime.BaseAPI {

    async userEmailExistsRaw(requestParameters: UserEmailExistsRequest): Promise<runtime.ApiResponse<boolean>> {
        if (requestParameters.email === null || requestParameters.email === undefined) {
            throw new runtime.RequiredError('email','Required parameter requestParameters.email was null or undefined when calling userEmailExists.');
        }

        const queryParameters: runtime.HTTPQuery = {};

        const headerParameters: runtime.HTTPHeaders = {};

        const response = await this.request({
            path: `/api/1.0/users/userExists/{email}`.replace(`{${"email"}}`, encodeURIComponent(String(requestParameters.email))),
            method: 'GET',
            headers: headerParameters,
            query: queryParameters,
        });

        return new runtime.TextApiResponse(response);
    }

    async userEmailExists(requestParameters: UserEmailExistsRequest): Promise<boolean> {
        const response = await this.userEmailExistsRaw(requestParameters);
        return await response.value();
    }
}

The return statement from userEmailExistsRaw has error:

Type 'TextApiResponse' is not assignable to type 'ApiResponse<boolean>'.
  Types of property 'value' are incompatible.
    Type '() => Promise<string>' is not assignable to type '() => Promise<boolean>'.
      Type 'Promise<string>' is not assignable to type 'Promise<boolean>'.
        Type 'string' is not assignable to type 'boolean'.ts(2322)
openapi-generator version

4.0.0-beta3 from npm

OpenAPI declaration file content or url
---
swagger: '2.0'
info:
  description: Operations related to the API
  version: '1.0'
  title: API
host: api.example.com
basePath: "/"
tags:
- name: User
  description: All apis relating to Users
schemes:
- http
- https
produces:
- application/xml
- application/json
paths:
  "/api/1.0/users/userExists/{email}":
    get:
      tags:
      - User
      operationId: userEmailExists
      parameters:
      - name: email
        in: path
        description: email
        required: true
        type: string
      responses:
        '200':
          description: Successfully retrieved result!
          schema:
            type: boolean
  "/api/1.0/users/id/{id}":
    get:
      tags:
      - User
      summary: Get specific User by id
      operationId: findUserById
      parameters:
      - name: id
        in: path
        description: id
        required: true
        type: integer
        format: int64
      responses:
        '200':
          description: Successfully retrieved result!
          schema:
            "$ref": "#/definitions/UserDto"
definitions:
  UserDto:
    type: object
    required:
    - id
    - email
    - firstName
    - lastName
    properties:
      id:
        type: integer
        format: int64
      email:
        type: string
      firstName:
        type: string
      lastName:
        type: string
    title: UserDto
Command line used for generation

openapi-generator generate --additional-properties supportsES6=true -i ./openapi.yaml -g typescript-fetch -o out/typescript-fetch

Steps to reproduce

Save yaml to openapi.yaml in current directory and run the command above.

Suggest a fix

Since true and false are both valid json responses, should the return statement simple be return new runtime.JSONApiResponse(response); (This also satisfies the typechecker)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions