swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[Typescript] Does not support polymorphism

Open micaelboucard opened this issue 7 years ago • 10 comments

Description

I got a model that could have 2 different shapes. Following OpenAPIv3 specs I want to be able to generate typescript code from a OpenAPIv3 specification using oneOf that takes care of it and with the help of the discriminator field, be able to serialise to the appropriate shape.

Swagger-codegen version

v3.0.7

Swagger declaration file content

From:

components:
  shemas:
    MyResponse:
      type: "object"
      properties:
        id:
          type: "string"
        configuration:
          $ref: "#/components/schemas/MyConfiguration"
      required:
      - "id"
      - "configuration"
    MyConfiguration:
      type: "object"
      oneOf:
      - $ref: "#/components/schemas/MyConfigurationOne"
      - $ref: "#/components/schemas/MyConfigurationTwo"
      discriminator:
        propertyName: "configurationType"
    MyConfigurationOne:
      type: "object"
      properties:
        configurationType:
          type: "string"
        propertyOneOne:
          type: "string"
        propertyOneTwo:
          type: "string"
      required:
      - "configurationType"
      - "propertyOneOne"
    MyConfigurationTwo:
      type: "object"
      properties:
        configurationType:
          type: "string"
        propertyTwoOne:
          type: "string"
        propertyTwoTwo:
          type: "string"
      required:
      - "configurationType"
      - "propertyTwoOne"
Command line used for generation

java -jar ./swagger-codegen-cli/bin/swagger-codegen-cli.jar generate -i ./swagger-codegen-cli/my-openapi.yml -l typescript-angular -o ./swagger-codegen-cli/services/my-data-service

Steps to reproduce

Generate service from yaml file. Created interfaces do not implement polymorphism as expected. MyConfigurationOne and MyConfigurationTwo interfaces are well defined. But MyConfiguration object is a mix of both, which is not what it is intended:

/**
 * my-data-service
 * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
 *
 * OpenAPI spec version: 2019-02-14T23:09:02Z
 * 
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */import { MyConfigurationOne } from './myConfigurationOne';
import { MyConfigurationTwo } from './myConfigurationTwo';


export interface MyConfiguration { 
    configurationType: string;
    propertyOneOne: string;
    propertyOneTwo?: string;
    propertyTwoOne: string;
    propertyTwoTwo?: string;
}
Suggest a fix/enhancement

Polymorphism in typescript could be translated to:

a MyConfiguration type:

type MyConfiguration =
  MyConfigurationOne |
  MyConfigurationTwo;

referenced in the MyResponse interface:

/**
 * my-data-service
 * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
 *
 * OpenAPI spec version: 2019-02-14T23:09:02Z
 * 
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */import { MyConfiguration } from './myConfiguration';


export interface MyResponse { 
    id: string;
    configuration: MyConfiguration;
}

micaelboucard avatar Apr 04 '19 00:04 micaelboucard

this is fixed by https://github.com/swagger-api/swagger-codegen-generators/pull/531, closing now, but please let me know if you get something wrong and i'll reopen it.

HugoMario avatar Nov 08 '19 16:11 HugoMario

Hi @HugoMario, I have just tried with the example published on this issue but we are not quite there. This is the result I got, using swagger-codegen-cli-3.0.14-20191110.205305-28.jar:

MyResponse interface is correct:

/**
 * build-transactions
 * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
 *
 * OpenAPI spec version: 2019-02-14T23:09:02Z
 * 
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */
import { MyConfiguration } from './myConfiguration';

export interface MyResponse { 
    id: string;
    configuration: MyConfiguration;
}

But then, the type that should be generated for MyConfiguration is empty:

/**
 * build-transactions
 * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
 *
 * OpenAPI spec version: 2019-02-14T23:09:02Z
 * 
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */

export interface MyConfiguration { 
}

MyConfigurationOne and MyConfigurationTwo are correct too.

It's just the link between the parent type and its childs. IMHO it should be something like:

/**
 * build-transactions
 * 
 * NOTE: Due to the lack of support for polymorphism in the swagger code generator,
 * this class is manually generated by us.
 * Do not edit the class manually.
 */

 import { MyConfigurationOne } from './myConfigurationOne';
import { MyConfigurationTwo } from './myConfigurationTwo';

export type MyConfiguration =
  MyConfigurationOne |
  MyConfigurationTwo;

Please let me know what you think!

micaelboucard avatar Nov 10 '19 23:11 micaelboucard

@micaelboucard , yea that should bbe the output, thanks for reporting it, going to work back on this ticket.

HugoMario avatar Nov 11 '19 15:11 HugoMario

Thanks @HugoMario !

micaelboucard avatar Nov 12 '19 01:11 micaelboucard

Hey @HugoMario , i've seen that v3.0.14 has been released today. Is this bug fixed on that release?

micaelboucard avatar Nov 18 '19 03:11 micaelboucard

Hi @micaelboucard, unfortunately, fix is not on release, i'm just going to add a new PR tomorrow

HugoMario avatar Nov 18 '19 03:11 HugoMario

Great! Thanks for the update @HugoMario . I'll keep on eye on this issue..

micaelboucard avatar Nov 18 '19 03:11 micaelboucard

Hi @micaelboucard , just merged a new PR solving issue. Can you try it when you have a chance please?

HugoMario avatar Nov 21 '19 16:11 HugoMario

🥇 @HugoMario !! Tried with swagger-codegen-cli-3.0.15-20191122.131628-6.jar and it's working now like a beauty. Many thanks!

micaelboucard avatar Nov 25 '19 02:11 micaelboucard

this is still an issue for typescript rxjs :(

evanjmg avatar Sep 13 '21 11:09 evanjmg