swagger-typescript-api: "circularly references itself" errors after upgrade to 13

Command: swagger-typescript-api --extract-enums --no-client --path https://raw.githubusercontent.com/europace/baufismart-kundenangaben-api/master/kundenangaben-openapi.yaml --name kundenangaben-api.model.ts

Worked with 12, but now typescript throws “circularly references itself” errors with 13.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 12
  • Comments: 17

Commits related to this issue

Most upvoted comments

I fixed this by modifying https://github.com/acacode/swagger-typescript-api/blob/master/src/code-gen-process.js#L122-L124

to be

const componentsToParse = _.sortBy(this.schemaComponentsMap.filter(
      _.compact(['schemas', this.config.extractResponses && 'responses']),
    ), (value) => {
      return value.rawTypeData.hasOwnProperty("discriminator") ? 0 : 1;
    });

This forces the schemas that have a discriminator to execute first creating the right types.

const componentsToParse = _.sortBy(this.schemaComponentsMap.filter(
      _.compact(['schemas', this.config.extractResponses && 'responses']),
    ), (value) => {
      return value.rawTypeData.hasOwnProperty("discriminator") ? 0 : 1;
    });

Looks like this is not works properly. This fix produces code like this (from “oneOf” + “ref”):

export type MyDefinitionsDefinitionsKwargFilter =
    | ({
          type: 'KwargFilterValue';
      } & any)
    | ({
          type: 'KwargFilterProduct';
      } & any)

Which is literally useless (works same as just “any”).

Let’s simplify initial example:

type BaseConfigConfigTypeMapping<Key, Type> = {
  type: Key;
} & Type;

interface BaseConfig {
  type?:
    | 'a'
    | 'b';
}

export type Config = BaseConfig &
  (
    | BaseConfigConfigTypeMapping<'a', AConfig>
    | BaseConfigConfigTypeMapping<'b', BConfig>
  );

export interface AConfig extends Config  {
  ... few unique fields
}

export interface BConfig extends Config  {
  ... few unique fields
}

I assume that instead of

export interface AConfig extends Config  {
  ... few unique fields
}

export interface BConfig extends Config  {
  ... few unique fields
}

lib should generate next lines and use BaseConfig instead of Config

export interface AConfig extends BaseConfig {
  ... few unique fields
}

export interface BConfig extends BaseConfig {
  ... few unique fields
}

Same here. Upgrading from 12 to 13 introduced circular references similar to below.

The previous output:

export interface Config {
  type?:
    | 'anExampleConfig'
    | 'anotherExampleConfig';
}

export interface AnExampleConfig extends Config  {
  field?:
    | 'one'
    | 'two'
    | 'three';
  anotherField?: boolean;
}

The new, circular dependency style:

type BaseConfigConfigTypeMapping<Key, Type> = {
  type: Key;
} & Type;

interface BaseConfig {
  configType?:
    | 'anExampleConfig'
    | 'anotherExampleConfig';
}

export type Config = BaseConfig &
  (
    | BaseConfigConfigTypeMapping<'anExampleConfig', ExampleConfig>
    | BaseConfigConfigTypeMapping<'anotherExampleConfig', AnotherExampleConfig>
  );

export interface ExampleConfig extends Config  {
  field?:
    | 'one'
    | 'two'
    | 'three';
  anotherField?: boolean;
}