openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] If superclass has no properties, it will not be generated, but derived class does extend not existing/generated class

Open sascha-friederich opened this issue 6 years ago • 9 comments

Description

I use the openapi gradle-plugin to generate java classes - but if the superclass has no properties, the class will not be generated but the derived class extends the non existing class. So the code does not compile.

Using the command-line I see the following message:

Model Pet not generated since it's a free-form object

As a workaround we modify the openapi json to add some dummy attribute in the superclass, so that it will be generated.

openapi-generator version

org.openapi.generator 4.3.1

OpenAPI declaration file content or url
{
    "openapi": "3.0.1",
    "info": {
        "title": "Abstract Generation problem",
        "description": "Abstract Generation problem",
        "version": "1.0.0-SNAPSHOT"
    },
    "servers": [],
    "paths": {},
    "components": {
        "schemas": {
            "Pet": {
                "title": "AbstractPet",
                "type": "object",
                "description": "model containing all the details of a pet",
                "discriminator": {
                    "propertyName": "petTypeName"
                }
            },
            "Dog": {
                "title": "DogDetails",
                "description": "The model containing the details of the dog",
                "allOf": [
                    {
                        "$ref": "#/components/schemas/Pet"
                    },
                    {
                        "title": "DogDetails",
                        "type": "object",
                        "properties": {
                            "legs": {
                                "type": "string",
                                "description": "the number of legs"
                            }
                        },
                        "description": "The model containing the details of the dog"
                    }
                ]
            }
        }
    }
}
Generation Details

java -jar openapi-generator-cli-4.3.1.jar generate -i openapi.json -g spring -o ./temp

Generated Models

There were two java classes generated:

  • Dog.java
  • DogDetails.java

Dog.java does extend Pet, that does not exist!

public class Dog extends Pet  {
  @JsonProperty("legs")
  private String legs;

  public Dog legs(String legs) {
    this.legs = legs;
    return this;
  }
...
Workaround

change definition of Pet to:

            "Pet": {
                "title": "AbstractPet",
                "type": "object",
                "properties": {
                    "dummyProperty": {
                        "type": "string",
                        "description": "Workaround - OpenAPI generator does not consider definitions without properties"
                    }
                },
                "description": "model containing all the details of a pet",
                "discriminator": {
                    "propertyName": "petTypeName"
                }
            },

sascha-friederich avatar Oct 09 '20 11:10 sascha-friederich

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

auto-labeler avatar Oct 09 '20 11:10 auto-labeler

Will this fix be merged? I have the same problem in my project.

WojciechSobczyk avatar Apr 26 '21 08:04 WojciechSobczyk

Are there any updates on this issue? As far as I can see, this is still broken in 6.0.1.

WeihmannDev avatar Aug 24 '22 16:08 WeihmannDev

same issue here

jishei-128 avatar Sep 08 '22 15:09 jishei-128

same here

ambition-consulting avatar Dec 08 '22 14:12 ambition-consulting

absolutely the same issue here.

PuskyWasTaken avatar May 22 '23 11:05 PuskyWasTaken

Same issue here - is there an update on this issue and will this fix be merged?

rboutellier avatar Jun 26 '23 05:06 rboutellier

There are two workarounds (at least for the spring-generator) for generating a Java class for an object type that has no properties:

  1. Add the allOf key with an empty array, e.g.,
MyBaseType:
  type: object
  discriminator:
    propertyName: '@type'
  required:
    - '@type'
  allOf: []
  1. Use the x-is-free-form vendor extension with the value "false" (but omit the type: object validation in order to avoid generator warning messages), e.g.,
MyBaseType:
  discriminator:
    propertyName: '@type'
  required:
    - '@type'
  x-is-free-form: false

mgrafl avatar Jun 30 '23 12:06 mgrafl

@wing328 please fix this issue, it's very impactful

ldeluigi avatar Jun 19 '26 17:06 ldeluigi