OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript-fetch] Self-referencing types for oneOf where property name and type name match

Open
#15,212 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

The typescript-fetch generator generates interfaces which reference themselves for oneOf object schemas where the property names of these objects are the same as the type names, since it generates intermediate types with the same name, eg:

/**
 * @type FooOrBar
 * 
 * @export
 */
export type FooOrBar = Bar | Foo;
/**
 * 
 * @export
 * @interface Foo
 */
export interface Foo {
    /**
     * 
     * @type {Foo}
     * @memberof Foo
     */
    foo: Foo;
}
openapi-generator version

6.3.0. Also tried with latest mainline.

OpenAPI declaration file content or url

Full example: https://gist.github.com/cogwirrel/6f6355e04b831bfbbbe57337b3429b9c

Example of just the schema part below

{
  "oneOf": [
    {
      "type": "object",
      "title": "foo",
      "properties": {
        "foo": {
          "$ref": "#/components/schemas/Foo"
        }
      },
      "required": [
        "foo"
      ]
    },
    {
      "type": "object",
      "title": "bar",
      "properties": {
        "bar": {
          "$ref": "#/components/schemas/Bar"
        }
      },
      "required": [
        "bar"
      ]
    }
  ]
}
Generation Details

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g typescript-fetch -i https://gist.githubusercontent.com/cogwirrel/6f6355e04b831bfbbbe57337b3429b9c/raw/38c4af6ae02d8dd0c8bfc7a29c9f9d63069154ad/example-api-oneof-issue.json -o ./oneof-test

Steps to reproduce
  • Checkout latest mainline
  • Build with ./mvnw clean install
  • Run java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g typescript-fetch -i https://gist.githubusercontent.com/cogwirrel/6f6355e04b831bfbbbe57337b3429b9c/raw/38c4af6ae02d8dd0c8bfc7a29c9f9d63069154ad/example-api-oneof-issue.json -o ./oneof-test

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the typescript-fetch generator and reproduce the issue using the OpenAPI declaration at the linked gist and the provided generation command. Inspect the generated interfaces and oneOf handling; done means matching property and type names no longer produce incorrect self-referencing intermediate types.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.