swagger-typescript-api icon indicating copy to clipboard operation
swagger-typescript-api copied to clipboard

Required properties in swagger.json create duplicate interfaces

Open JustSamuel opened this issue 1 year ago • 3 comments

The following definitions

"definitions": {
    "BaseEntityWithoutId": {
      "required": [],
      "properties": {
        "createdAt": {
          "type": "string",
          "description": "The creation date of the object."
        },
        "updatedAt": {
          "type": "string",
          "description": "The last update date of the object."
        },
        "version": {
          "type": "integer",
          "description": "The current version of the object."
        }
      }
    },
    "BaseEntity": {
      "required": [
        "id"
      ],
      "properties": {
        "id": {
          "type": "integer",
          "description": "The auto-generated object id."
        }
      },
      "allOf": [
        {
          "$ref": "#/definitions/BaseEntityWithoutId"
        }
      ]
    }
  }

Generates the following file:

export interface BaseEntityWithoutId {
  /** The creation date of the object. */
  createdAt?: string;

  /** The last update date of the object. */
  updatedAt?: string;

  /** The current version of the object. */
  version?: number;
}

export type BaseEntity = { id: number } & { id: number };

Removing the 'id' from required fixes it slightly and gives the following output:

export interface BaseEntityWithoutId {
  /** The creation date of the object. */
  createdAt?: string;

  /** The last update date of the object. */
  updatedAt?: string;

  /** The current version of the object. */
  version?: number;
}

export type BaseEntity = BaseEntityWithoutId & { id?: number };

Am I doing something wrong or is this a bug?

JustSamuel avatar Aug 21 '22 14:08 JustSamuel

Fixed by this fork

JustSamuel avatar Aug 21 '22 14:08 JustSamuel

@JustSamuel hello, it is a bug, will work on it

js2me avatar Aug 22 '22 21:08 js2me

Related to https://github.com/acacode/swagger-typescript-api/pull/342

js2me avatar Aug 29 '22 21:08 js2me