question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] [java] Polymorphy of models broken in v4.3.1

See original GitHub issue
Description

Using the java generator, I used to get models where child-classes extend parent classes with version 4.3.0. With version 4.3.1 the child classes do not extend the parent class anymore.

openapi-generator version

It’s a regression of version 4.3.1. 4.3.0 worked.

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
    version: 1.0.0
    title: Polymorphy demo
paths:
    /random-pet:
        get:
            responses:
                '200':
                    description: Returning a random pet.
                    content:
                        application/json:
                            schema:
                                $ref: "#/components/schemas/Pet"
components:
    schemas:
        Pet:
            oneOf: 
                - $ref: '#/components/schemas/Dog' 
                - $ref: '#/components/schemas/Cat' 
            discriminator: 
                propertyName: petType
                mapping: 
                    dog: '#/components/schemas/Dog' 
                    cat: '#/components/schemas/Cat' 
        PetBase:
            type: object
            required:
                - petType
            properties:
                petType:
                    type: string
                name:
                    type: string
        Dog:
            allOf:
                - $ref: '#/components/schemas/PetBase'
                - type: object
                  properties:
                      barksAlot:
                          type: boolean
        Cat:
            allOf:
                - $ref: '#/components/schemas/PetBase'
                - type: object
                  properties:
                      goesOutdoors:
                          type: boolean
Generation Details

I run the generator in version 4.3.0 and 4.3.1:

$ java -jar openapi-generator-cli-4.3.0.jar generate -g java -o 4.3.0_result -i polymorphy.yaml
$ java -jar openapi-generator-cli-4.3.1.jar generate -g java -o 4.3.1_result -i polymorphy.yaml
Steps to reproduce

After generating the code in 4.3.0 and 4.3.1, compare Cat.java. The generated model of 4.3.0 will contain public class Cat extends PetBase while the generated model of 4.3.1 will only contain public class Cat (without the inheritance).

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
marcdejongecommented, Jun 15, 2021

Ok, an update. In 4.3.1 it added support for the discriminator. After playing around I got the expected result for me by changing the models as follows:

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Polymorphy demo
paths:
  /random-pet:
    get:
      responses:
        '200':
          description: Returning a random pet.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pet"
components:
  schemas:
    Pet:
      type: object
      required:
        - petType
      properties:
        petType:
          type: string
        name:
          type: string
      discriminator:
        propertyName: petType
        mapping:
          dog: '#/components/schemas/Dog'
          cat: '#/components/schemas/Cat'
    Dog:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            barksAlot:
              type: boolean
    Cat:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            goesOutdoors:
              type: boolean
0reactions
epabstcommented, Nov 19, 2022

After I got the Pet model working that @marcdejonge provided in https://github.com/OpenAPITools/openapi-generator/issues/9615#issuecomment-861304768, I figured out a workaround for my own issue. I had separate yaml files for my schema model classes, and by moving them into the same file as the endpoints under this, it worked for me!!!

components:
  schemas:

Note, I also applied this fix to avoid the extra AllOf classes: https://github.com/OpenAPITools/openapi-generator/issues/3100#issuecomment-703481081

Read more comments on GitHub >

github_iconTop Results From Across the Web

[BUG] [JAVA] Java generation of 'extra' class with allOf #3100
I am using the pet store example to create java classes with inheritance. In the newest release (4.0.1) I found a bug.
Read more >
OOP Inheritance & Polymorphism - Java Programming Tutorial
In Java, each subclass can have one and only one direct superclass, i.e., single inheritance. On the other hand, a superclass can have...
Read more >
Polymorphism in Java with Examples in 2023 - Great Learning
Polymorphism in Java can be defined as the ability of an object to take many forms. This helps us perform the same action...
Read more >
OOP Inheritance and Polymorphism - Java Programming ...
A distance(Circle another) method that returns the distance from the center of this instance to the center of the given Circle instance (called...
Read more >
DataStax Java Driver - Changelog
4.0.0-beta3. [bug] JAVA-2066: Array index range error when fetching routing keys on bound statements; [documentation] JAVA-2061: Add section to upgrade ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found