OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Typescript-fetch]

Open
#15,574 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

When self referencing component is defined with inheritance, typescript import is added to itself and you get conflicting components in one file.

openapi-generator version

6.4.0

OpenAPI declaration file content or url
openapi: "3.0.3"
info:
  title: Self reference reproduce
  version: 1.0.0
tags:
  - name: Menus

paths:
  /menus:
    get:
      tags:
        - Menus
      operationId: GetMenus
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/GetMenusResponse"

components: 
  schemas:   
    GetMenusResponse:
      type: object
      properties:
        menuItems:
          type: array
          items:
            $ref: "#/components/schemas/MenuItem"  

    LabelItem:
      type: object
      properties:       
        label:
          type: string               

    MenuItem:
      type: object
      allOf:        
        - $ref: '#/components/schemas/LabelItem'
      properties:
        name:
          type: string
        children:
          type: array
          items:
            $ref: "#/components/schemas/MenuItem"      
Generation Details

Due the self referencing, it generates import statement to import itself in MenuItem.ts:

import type { MenuItem } from './MenuItem';
...
export interface MenuItem {

Also noticed, that problem exists only if there's inheritance, if I remove

      allOf:        
        - $ref: '#/components/schemas/LabelItem'

problem goes away.

Steps to reproduce
  1. Create folder C:\temp\typescript-self-reference-issue-reproduce
  2. Save the above mentioned spec to spec.yaml
  3. Run (jar path might need to be adjusted to you local cache)
 java -jar ".\node_modules\@openapitools\openapi-generator-cli\versions\6.4.0.jar" generate --input-spec="C:\temp\typescript-self-reference-issue-reproduce\spec.yaml" --generator-name="typescript-fetch" --output="C:\temp\typescript-self-reference-issue-reproduce/src"
  1. Inspect ./src/models/MenuItem.ts. It should contain import to itself.
Suggest a fix

You can't fix that in templates, only in generator itself.

I was looking at
https://github.com/OpenAPITools/openapi-generator/blob/1204662a6485611f427106b3d734c54f36cc49bb/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java#L766

where I think imports array is generated, but could not understand from reading the code why it works correctly without inheritance, probably would need to debug and as long as I'm not a java developer, it would take significant effort to set everything up.

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

Reproduce the issue with the supplied OpenAPI YAML and the typescript-fetch generation command, then inspect src/models/MenuItem.ts. Start with TypeScriptFetchClientCodegen.java around the referenced imports logic and trace how inheritance affects the generated imports. Done means the generated MenuItem.ts no longer imports itself while preserving the inherited and self-referencing model behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, typescript
Domain
api, 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.