Skip to content
New issue

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

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

Already on GitHub? # to your account

[Typescript] Does not support polymorphism #9339

Closed
micaelboucard opened this issue Apr 4, 2019 · 10 comments · Fixed by swagger-api/swagger-codegen-generators#548
Closed
Assignees
Milestone

Comments

@micaelboucard
Copy link

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;
}
@HugoMario
Copy link
Contributor

HugoMario commented Nov 8, 2019

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

@micaelboucard
Copy link
Author

micaelboucard commented Nov 10, 2019

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!

@HugoMario
Copy link
Contributor

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

@HugoMario HugoMario reopened this Nov 11, 2019
@micaelboucard
Copy link
Author

Thanks @HugoMario !

@micaelboucard
Copy link
Author

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

@HugoMario
Copy link
Contributor

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

@micaelboucard
Copy link
Author

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

@HugoMario
Copy link
Contributor

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

@micaelboucard
Copy link
Author

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

@evanjmg
Copy link

evanjmg commented Sep 13, 2021

this is still an issue for typescript rxjs :(

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

Successfully merging a pull request may close this issue.

3 participants