OpenAPITools / OpenAPITools/openapi-generator
[BUG][kotlin-server][jaxrs-spec] Inheritance not supported
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
The new jaxrs-spec library of the kotlin-server generator does not support inheritance.
Inheritance can be represented in openapi with a discriminator and allOf. However, the kotlin-server generator gives an error when inheritance is used in an openapi spec:
Exception in thread "main" java.lang.RuntimeException: Could not generate model 'Pet'
at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:532)
at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:888)
at org.openapitools.codegen.cmd.Generate.execute(Generate.java:441)
at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: org.openapitools.codegen.templating.TemplateNotFoundException: interface_req_var
at org.openapitools.codegen.templating.MustacheEngineAdapter.findTemplate(MustacheEngineAdapter.java:79)
at org.openapitools.codegen.templating.MustacheEngineAdapter.lambda$compileTemplate$0(MustacheEngineAdapter.java:61)
at com.samskivert.mustache.Mustache$IncludedTemplateSegment.execute(Mustache.java:756)
at com.samskivert.mustache.Mustache$BlockSegment.executeSegs(Mustache.java:845)
at com.samskivert.mustache.Mustache$SectionSegment.execute(Mustache.java:870)
at com.samskivert.mustache.Mustache$BlockSegment.executeSegs(Mustache.java:845)
at com.samskivert.mustache.Mustache$SectionSegment.execute(Mustache.java:866)
at com.samskivert.mustache.Mustache$BlockSegment.executeSegs(Mustache.java:845)
at com.samskivert.mustache.Mustache$SectionSegment.execute(Mustache.java:881)
at com.samskivert.mustache.Template.executeSegs(Template.java:157)
at com.samskivert.mustache.Mustache$IncludedTemplateSegment.execute(Mustache.java:774)
at com.samskivert.mustache.Mustache$BlockSegment.executeSegs(Mustache.java:845)
at com.samskivert.mustache.Mustache$InvertedSegment.execute(Mustache.java:910)
at com.samskivert.mustache.Mustache$BlockSegment.executeSegs(Mustache.java:845)
at com.samskivert.mustache.Mustache$InvertedSegment.execute(Mustache.java:906)
at com.samskivert.mustache.Mustache$BlockSegment.executeSegs(Mustache.java:845)
at com.samskivert.mustache.Mustache$InvertedSegment.execute(Mustache.java:910)
at com.samskivert.mustache.Mustache$BlockSegment.executeSegs(Mustache.java:845)
at com.samskivert.mustache.Mustache$SectionSegment.execute(Mustache.java:881)
at com.samskivert.mustache.Mustache$BlockSegment.executeSegs(Mustache.java:845)
at com.samskivert.mustache.Mustache$SectionSegment.execute(Mustache.java:866)
at com.samskivert.mustache.Template.executeSegs(Template.java:157)
at com.samskivert.mustache.Template.execute(Template.java:134)
at com.samskivert.mustache.Template.execute(Template.java:125)
at org.openapitools.codegen.templating.MustacheEngineAdapter.compileTemplate(MustacheEngineAdapter.java:65)
at org.openapitools.codegen.TemplateManager.write(TemplateManager.java:163)
at org.openapitools.codegen.DefaultGenerator.processTemplateToFile(DefaultGenerator.java:1034)
at org.openapitools.codegen.DefaultGenerator.processTemplateToFile(DefaultGenerator.java:1021)
at org.openapitools.codegen.DefaultGenerator.generateModel(DefaultGenerator.java:388)
at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:523)
... 4 more
openapi-generator version
Tested with openapi-generator version 5.4.0 and the docker image openapitools/openapi-generator-cli:latest (as of Feb 9th 2022).
OpenAPI declaration file content or url
openapi: 3.0.0
info:
description: |
kotlin-server jaxrs-spec inheritance issues
title: inheritance bug
version: 1.0.0
paths:
/test:
get:
responses:
'200':
description: ok
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
servers:
- url: http://localhost/
components:
schemas:
Pet:
type: object
required:
- pet_type
properties:
pet_type:
type: string
discriminator:
propertyName: pet_type
mapping:
cachorro: Dog
Cat:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
# all other properties specific to a `Cat`
properties:
name:
type: string
Dog:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
# all other properties specific to a `Dog`
properties:
bark:
type: string
Lizard:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
# all other properties specific to a `Lizard`
properties:
lovesRocks:
type: boolean
Generation Details
Generator: kotlin-server
Library: jaxrs-spec
Was tested with the following command:
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate -i /local/petstore.yaml -g kotlin-server -o /local/out/kotlin --library jaxrs-spec
where the spec given above is present in the current directory as petstore.yaml
Steps to reproduce
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate -i /local/petstore.yaml -g kotlin-server -o /local/out/kotlin --library jaxrs-spec
where the spec given above is present in the current directory as petstore.yaml
Related issues/PRs
The new kotlin-server jaxrs-spec was introduced in #10830
The missing interface_req_var template is not the only problem with inheritance for this generator. The interface_opt_var is also missing.
In addition, there also seem to be an issue with the generated code when using multiple levels of inheritance.
Suggest a fix
A good start would be using the templates from the kotlin-client generator:
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/kotlin-client/interface_opt_var.mustache
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/kotlin-client/interface_req_var.mustache
Other changes will be needed though, like marking the parent's fields as override in the children classes.
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 Docker command and petstore.yaml using the kotlin-server generator with the jaxrs-spec library. Compare the referenced kotlin-client/interface_opt_var.mustache and interface_req_var.mustache templates with the jaxrs-spec templates, then investigate the generated classes for inherited fields. Done means the example generates successfully with inheritance, including the required and optional interface cases and multiple inheritance levels.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100