koshevy / codegena

Tools for codegeneration from OAS3 to TypeScript. Actual Roadmap published — https://github.com/koshevy/codegena/blob/codegena-3.x/ROADMAP.md
https://codegena-playground.stackblitz.io/
MIT License
38 stars 1 forks source link

Multiple inheritance of objects does not work correctly #70

Open mat813 opened 3 years ago

mat813 commented 3 years ago

Given this openapi definition where we have three schemas, price, that inherits from priceCreate that inherits from priceBase:

{
  "openapi": "3.0.3",
  "paths": {
    "/": {
      "get": {
        "operationId": "foo.bar",
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/price"
                }
              }
            }
          },
          "201": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/priceCreate"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "price": {
        "allOf": [
          { "$ref": "#/components/schemas/priceCreate" },
          {
            "type": "object",
            "required": ["id"],
            "properties": {
              "id": {
                "type": "integer"
              }
            }
          }
        ]
      },
      "priceCreate": {
        "allOf": [
          { "$ref": "#/components/schemas/priceBase" },
          {
            "type": "object",
            "required": ["backend_id"],
            "properties": {
              "backend_id": {
                "type": "integer"
              }
            }
          }
        ]
      },
      "priceBase": {
        "type": "object",
        "required": ["technology"],
        "properties": {
          "technology": {
            "type": "string"
          }
        }
      }
    }
  }
}

The generated index.ts file is:

/* tslint:disable */
export interface priceBase {
  technology: string;
}
export interface price extends priceBase {
  backend_id?: number;
  id: number;
  technology: string;
}
export interface priceCreate extends priceBase {
  backend_id: number;
  technology: string;
}
export type FooBarResponse<
  TCode extends 200 | 201 = 200 | 201,
  TContentType extends 'application/json' = 'application/json'
> = TCode extends 200
  ? TContentType extends 'application/json'
    ? price
    : any
  : TCode extends 201
  ? TContentType extends 'application/json'
    ? priceCreate
    : any
  : any;

There are a few problems in there.

1) the most important problem is that in interface price, backend_id became optional, though it should not be. 2) then, less important, is that interface price inherits from priceBase but it should be priceCreate. 3) and then, but it is not really important, there are a lot of properties that are repeated where they don't need to, cleaned up typescript would be:

export interface priceBase {
  technology: string;
}
export interface priceCreate extends priceBase {
  backend_id: number;
}
export interface price extends priceCreate {
  id: number;
}