openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [typescript-fetch] anyOf with "number" and "string" leads to empty Interface

Open tobias-zip opened this issue 1 year ago • 1 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using openapi-generator on an openapi.json that inluced anyOf with number and string, the resulting typescript schema is incorrect.

"PostsSchema": {
                "properties": {
                    "content": {
                        "anyOf": [
                            {
                                "type": "number"
                            },
                            {
                                "type": "string"
                            }
                        ],
                        "title": "Content"
                    }
                },
                "title": "PostsSchema",
                "type": "object"
            }

leads to

export interface Content {
}
export interface PostsSchema {
    /**
     * 
     * @type {Content}
     * @memberof PostsSchema
     */
    content?: Content;
}

but it should be content?: number | string; Content should not be an interface I think?.

openapi-generator version

v7.7.0 (https://github.com/OpenAPITools/openapi-generator/releases/tag/v7.7.0)

OpenAPI declaration file content or url

openapi.json

Generation Details
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v7.7.0 generate -i /local/openapi.json -g typescript-fetch -o /local/out/ts

Steps to reproduce
  1. put openapi.json into directory
  2. execute cli command above
  3. check ./out/ts/models/PostsSchema.ts and Content.ts

for reference here are the files I get: PostsSchema.ts Content.ts

Related issues/PRs
Suggest a fix

tobias-zip avatar Jul 13 '24 10:07 tobias-zip

+1 seeing this as well

          "status": {
            "anyOf": [
              {
                "type": "string",
                "const": "CREATED_NOT_STARTED"
              },
              {
                "type": "string",
                "const": "IN_PROGRESS"
              },
              {
                "type": "string",
                "const": "COMPLETED"
              },
              {
                "type": "string",
                "const": "FAILED"
              },
              {
                "type": "string"
              }
            ],
            "title": "Status"
          },

generates empty interface

/**
 *
 * @export
 * @interface Status
 */
export interface Status {}

woodward54 avatar Dec 03 '25 22:12 woodward54