swagger-api / swagger-api/swagger-codegen

[typescript-angular] failed to generate tagged unions

Open
#10,528 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Generating typescript-angular code with taggedUnions enabled throws an Exception:

Exception in thread "Thread-1" java.lang.RuntimeException: Could not generate model 'BillingSectionConfig'
        at io.swagger.codegen.v3.DefaultGenerator.generateModels(DefaultGenerator.java:451)
        at io.swagger.codegen.v3.DefaultGenerator.generate(DefaultGenerator.java:779)
        at io.swagger.codegen.v3.cli.cmd.Generate.run(Generate.java:378)
        at java.lang.Thread.run(Thread.java:748)
Caused by: com.github.jknack.handlebars.HandlebarsException: /handlebars/typescript-angular/modelGenericEnums.mustache:1:3: java.lang.ClassCastException: java.lang.String cannot be cast to io.swagger.codegen.v3.VendorExtendable
    /handlebars/typescript-angular/modelGenericEnums.mustache:1:3
        at io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.apply(ExtensionHelper.java:11)
...
Swagger-codegen version
<dependency>
  <groupId>io.swagger.codegen.v3</groupId>
  <artifactId>swagger-codegen-cli</artifactId>
  <version>3.0.22</version>
</dependency>
Swagger declaration file content or url

The relevant parts that would create tagged union interfaces. This was generated by

<plugin>
  <groupId>io.swagger.core.v3</groupId>
  <artifactId>swagger-maven-plugin</artifactId>
  <version>2.1.5</version>
</plugin>
openapi: 3.0.1
...
components:
  schemas:
    BillingSectionConfig:
      type: object
      allOf:
      - $ref: '#/components/schemas/RequestSectionConfig'
      - $ref: '#/components/schemas/RequestSectionConfigs'
    ContactSectionConfig:
      type: object
      allOf:
      - $ref: '#/components/schemas/RequestSectionConfig'
      - $ref: '#/components/schemas/RequestSectionConfigs'
    RequestSectionConfig:
      type: object
      properties:
        displayName:
          type: string
        description:
          type: string
    RequestSectionConfigs:
      required:
      - type
      type: object
      properties:
        type:
          type: string
      discriminator:
        propertyName: type
        mapping:
          BillingSectionConfig: '#/components/schemas/BillingSectionConfig'
          ContactSectionConfig: '#/components/schemas/ContactSectionConfig'
      oneOf:
      - $ref: '#/components/schemas/BillingSectionConfig'
      - $ref: '#/components/schemas/ContactSectionConfig'
Command line used for generation
java -jar swagger-codegen-cli-3.0.22.jar generate -l typescript-angular -i ../rat-rest/target/swagger/jaxrs-api.yaml -o src/app/api --additional-properties="taggedUnions=true"
Steps to reproduce

Run the command used for generation.

Related issues/PRs

PR: #7245

Suggest a fix/enhancement

I guess a few changes are necessary to get this working:

  1. In io.swagger.codegen.v3.CodegenProperty the missing getter/setter for discriminatorValue needs to be added. Otherwise the template engine will ignore this property

  2. In the swagger-codegen-generators project the discriminatorValue is not calculated correctly. Currently the children of a CodegenModel are used but it should be the mapping of the discriminator

  3. Also in the swagger-codegen-generators project: The modelTaggedUnion.mustache does not support inheritance and does not generate the discrimator type correctly

I guess that I am able to provide PR(s) fixing these issues with some guidance. Please let me know if you would accept one

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the provided typescript-angular command and taggedUnions=true, then inspect CodegenProperty, the discriminator handling in swagger-codegen-generators, and modelGenericEnums.mustache/modelTaggedUnion.mustache. Done means generation completes for the supplied schemas and produces tagged-union interfaces with the discriminator values and inheritance represented correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, java, typescript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.