OpenAPITools / OpenAPITools/openapi-generator

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

Open
#7,638 9 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: Spring
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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"
                }
            },

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the provided OpenAPI schema using the openapi-generator CLI and the spring generator. Inspect the generated Dog.java and the handling of the Pet schema, then verify that the superclass is generated when it has no properties. Done means the generated Java models compile without requiring the dummy property workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.