Skip to content

[BUG][kotlin-server][jaxrs-spec] Inheritance not supported #11552

Open

Description

@pec0ra

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.

Activity

  1. pedro-borges commented on May 18, 2022

    @pedro-borges

    I am also trying to generate kotlin jaxrs models with no success, has this been accepted as a bug? or is there a workaround?

  2. pec0ra commented on May 19, 2022

    @pec0ra
    Author

    I am also trying to generate kotlin jaxrs models with no success, has this been accepted as a bug? or is there a workaround?

    As a quick workaround, you can add the two mustache templates mentioned above in your open api template directory (See https://openapi-generator.tech/docs/templating/#modifying-templates). This won't really fix inheritance but at least you will be able to generate the code without error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions