swagger-api / swagger-api/swagger-codegen
[typescript-angular] failed to generate tagged unions
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:
-
In
io.swagger.codegen.v3.CodegenPropertythe missing getter/setter fordiscriminatorValueneeds to be added. Otherwise the template engine will ignore this property -
In the
swagger-codegen-generatorsproject thediscriminatorValueis not calculated correctly. Currently the children of aCodegenModelare used but it should be the mapping of the discriminator -
Also in the
swagger-codegen-generatorsproject: ThemodelTaggedUnion.mustachedoes 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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