Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type error: Type alias ‘XXX’ circularly references itself. #1987

Open
bondars1995 opened this issue Jan 16, 2024 · 14 comments
Open

Type error: Type alias ‘XXX’ circularly references itself. #1987

bondars1995 opened this issue Jan 16, 2024 · 14 comments
Assignees

Comments

@bondars1995
Copy link

bondars1995 commented Jan 16, 2024

When generating typescript objects for such configuration getting error on lint check.

Type error: Type alias ‘CardPaymentMethod’ circularly references itself.
  4 | /* eslint-disable */
  5 | import type { PaymentMethod } from ‘./PaymentMethod’;
> 6 | export type CardPaymentMethod = (PaymentMethod & {
    |             ^
  7 |     /**
  8 |      * Cardholder name.
  9 |      */
- info Linting and checking validity of types
openapi: 3.0.1
info:
  title: Payment Endpoints API Documentation
  description: Endpoints provided by payment team
  version: '1.0'
  license:
    name: MIT
components:
  schemas:
    CardPaymentMethod:
      type: object
      allOf:
        - $ref: '#/components/schemas/PaymentMethod'
      properties:
        name:
          type: 'string'
          description: "Cardholder name."
          example: "John Doe"
    PaymentMethod:
      required:
        - type
      type: object
      oneOf:
        - $ref: '#/components/schemas/CardPaymentMethod'
      properties:
        type:
          type: string
      discriminator:
        propertyName: type
        mapping:
          CardPaymentMethod: '#/components/schemas/CardPaymentMethod'
   Generated files
/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { PaymentMethod } from './PaymentMethod';
export type CardPaymentMethod = (PaymentMethod & {
    /**
     * Cardholder name.
     */
    name?: string;
});
/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { CardPaymentMethod } from './CardPaymentMethod';
export type PaymentMethod = (CardPaymentMethod | {
    type: string;
});

Command used for generation:
npx openapi-typescript-codegen --input ./generated/payment.yml --exportServices=false --exportCore=false

@bondars1995
Copy link
Author

Hello Guys,
@ferdikoomen could you please have a look?

@mrlubos
Copy link
Collaborator

mrlubos commented Feb 15, 2024

@bondars1995 are you able to create a failing test case?

@bondars95
Copy link

@mrlubos sure, I've create test stand for you, just clone repo and follow instructions

https://github.com/bondars95/typescript-codegen-issue

@mrlubos
Copy link
Collaborator

mrlubos commented Feb 15, 2024

@bondars95 thanks! How do you generate that OpenAPI spec? I am trying to understand what kind of types generate that output

@bondars95
Copy link

@mrlubos as mentioned in package.json
npx openapi-typescript-codegen --input spec.yaml --output ./generated --exportServices=false --exportCore=false

@bondars95
Copy link

@mrlubos and error is coming when compiling generated files with typescript
tsc generated/index.ts

@bondars95
Copy link

@mrlubos did you have any chance to have a look into it?

@mrlubos
Copy link
Collaborator

mrlubos commented Feb 22, 2024

@bondars95 not yet, because it's not clear to me:

  • what's the expected result based on the OpenAPI spec provided
  • how you're able to generate that OpenAPI spec in your backend language (which tooling do you use?)

@bondars95
Copy link

bondars95 commented Feb 22, 2024

@mrlubos openapi spec created manually
just wondering why it is causing issues with typescript generated types, maybe smth can be changed in typescript generation

@mrlubos
Copy link
Collaborator

mrlubos commented Feb 22, 2024

@bondars95 did it work on any of the previous versions? What result would you expect?

@bondars95
Copy link

@mrlubos I didn't check, but I suppose I will get same error, just wanted to check if it can be fixed somehow from your side.

@mrlubos
Copy link
Collaborator

mrlubos commented Feb 23, 2024

@bondars95 please refer to my earlier messages

@bondars95
Copy link

bondars95 commented Feb 23, 2024

@mrlubos I've got your point here, we need type descriminator for generating java classes
oneOf is used just for correct displaying on UI in swagger
when using both discriminator and oneOf it breaks typescript generation
Any way I can tell openapi-typescript-codegen to omit generating part related to discriminators? Or to ignore one of them if both discrimator and oneOf is present?
Cause using both discriminate and one of cause circular reference for typescript

@bondars95
Copy link

bondars95 commented Feb 23, 2024

@mrlubos in my case let's say expected behaviour would be having PaymentMethod generated without having reference to CardPaymentMethod not to cause circular reference

/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
export type PaymentMethod = ({
    type: string;
});

it obviously can be achieved by removing discrimantor, but this causes other issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants