OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] Fields not visible for extended Class if readonly in subclass

Open
#12,269 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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?
Description

If you use inheritance with Java and you use readonly fields in the extended model (used in allOf) the generator will create a private field in the base class and inside the constructor of the main model there is a setter to the private field of the base class.

public MyEntity(
     String id, 
     String $type
  ) {
    this();
    this.id = id;
    this.$type = $type;
  }
openapi-generator version

Checked with 5.4 and current master version

OpenAPI declaration file content or url
"MyEntity" : {
  "description" : "Represents a main class.",
  "allOf" : [ {
    "$ref" : "#/components/schemas/MyBaseClass"
    }, {
    "type" : "object",
    "properties" : {
      "shortName" : {
        "type" : "string",
         "readOnly" : false
       },
       "description" : {
         "type" : "string",
         "readOnly" : false
        }
      }
    } ]
},
"MyBaseClass" : {
  "type" : "object",
  "properties" : {
      "name" : {
        "type" : "string",
        "readOnly" : false
      },
      "id" : {
        "type" : "string",
        "readOnly" : true
       },
       "$type" : {
         "type" : "string",
         "readOnly" : true
       }
   },
   "description" : "Represents a base class",
   "discriminator" : {
     "propertyName" : "$type"
    }
},

You can also check with the current version of Jetbrains Youtrack OAS: https://youtrack.jetbrains.com/api/openapi.json

Generated Class Project and base class is IssueFolder

Generation Details

Checked with the following Maven Config:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <!-- RELEASE_VERSION -->
    <version>5.4.0</version>
    <!-- /RELEASE_VERSION -->
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>/path/to/openapi.json</inputSpec>
                <generatorName>java</generatorName>
                <output>${project.build.directory}/generated-sources/openapi</output>
            </configuration>
        </execution>
    </executions>
</plugin>
Steps to reproduce

Generate the mentioned jetbrains youtrack api with any Java generator.

Related issues/PRs
Suggest a fix
  • Most simple way is to set the variables no longer as private but package or protected instead in Mustache template (this is my workaround for now).
  • This could be conditional so if the model is used in any allOf / inheritance scenario it should be package / protected scoped var
  • Most complex but most clean way is that instead of setting the vars directly in the constructor of the MainClass the already existing constructor of the baseclass with these readonly vars super(readOnly1, readOnly2) is called instead of this()

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

Start with the Java generator and its Mustache model templates, then reproduce the issue using the Maven plugin and the supplied allOf schema or JetBrains YouTrack specification. Inspect the generated MyEntity and IssueFolder classes; done means readonly inherited fields are generated and initialized without assigning a private base-class field from the subclass.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.