Skip to content

[BUG][typescript-fetch] Schema with type object generates undefined function #1877

Description

@rzane
Description

I'm generating a typescript-fetch client using OpenAPI Generator 4.0.0-beta. After running the generator, the following functions are included in the output, but they are not defined:

  • anyFromJSON
  • anyToJSON

Also, the type for the property is any, but it should be object I think?

openapi-generator version

4.0.0-beta

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: MyAPI
  description: MY API
  version: 0.0.0
servers:
  - url: http://localhost:8080/api/
paths:
  /example:
    get:
      operationId: getExample
      responses:
        "200":
          description: "Success"
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Example"

components:
  schemas:
    Example:
      required:
        - data
      properties:
        data:
          type: object
Command line used for generation
$ node_modules/.bin/openapi-generator generate -i openapi.yaml -o src -g typescript-fetch
Steps to reproduce

I've created a gist that should help to reproduce the issue.

$ git clone https://gist.github.com/rzane/724ad1ed08721d04966f121461c0737d bug-report
$ cd bug-report
$ npm install
$ npm run generate
$ npm run test

You should see the following output:

> tsc -p src --noEmit

src/models/Example.ts:31:17 - error TS2304: Cannot find name 'anyFromJSON'.

31         'data': anyFromJSON(json['data']),
                   ~~~~~~~~~~~

src/models/Example.ts:40:17 - error TS2304: Cannot find name 'anyToJSON'.

40         'data': anyToJSON(value.data),
                   ~~~~~~~~~

The source of the error can be found in src/models/Example.ts, which contains the following:

export interface Example {
    /**
     * 
     * @type {any}
     * @memberof Example
     */
    data: any;
}

export function ExampleFromJSON(json: any): Example {
    return {
        'data': anyFromJSON(json['data']),
    };
}

export function ExampleToJSON(value?: Example): any {
    if (value === undefined) {
        return undefined;
    }
    return {
        'data': anyToJSON(value.data),
    };
}
Related issues/PRs

I couldn't find one 🤷‍♂️

Suggest a fix

Ideally, the property data would be declared as object or empty interface ExampleItems, rather than any.

More importantly, if the schema is an object without any properties or additionalProperties, there should be no coercion.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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