Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 tasks done
arne-kroeger opened this issue Apr 28, 2022 · 3 comments
Open
5 tasks done

Comments

@arne-kroeger
Copy link

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()
Andy2003 added a commit to Andy2003/openapi-generator that referenced this issue Nov 3, 2022
Andy2003 added a commit to Andy2003/openapi-generator that referenced this issue Nov 3, 2022
@multigrid101
Copy link

Hello all, is there any progress on this? It seems that a workaround has already been prepared.... I am currently stuck on this problem.

@yeralin
Copy link

yeralin commented Oct 10, 2023

Another issue is that it will not generate setters for the readOnly field.

Here is the workaround, use custom template file (I am using jersey2's template) and update this line to:

{{#isReadOnly}}protected{{/isReadOnly}} {{^isReadOnly}}private{{/isReadOnly}} {{{datatypeWithEnum}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};

This will generate the final field as protected if it is marked as readOnly.

@wi2david
Copy link

I'm facing the same problem, any solution?

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

No branches or pull requests

4 participants