OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] [7.0.0] Fluent setters generated for readOnly properties

Open
#16,498 1 comment 0 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? Don't generate the fluentd setter method
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Generated code is not compiling because

  @JsonCreator
  public Child(
    @JsonProperty(JSON_PROPERTY_ID) String id
  ) {
    this();
    this.id = id;  // id is private in the class 'Parent' + being readonly the setter shouldn't be generated
  }

The bug seems a consequence of https://github.com/OpenAPITools/openapi-generator/issues/14915.

openapi-generator version

7.0.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: test
  description: test schema
  version: 1.0.0
servers:
  - url: 'http://test.com'
    description: stage
paths:
  /demo:
    get:
      summary: placeholder summary
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Child'
components:
  schemas:
    Parent:
      type: object
      properties:
        id:
          type: string
          readOnly: true
        name:
          type: string
    Child:
      allOf:
        - $ref: '#/components/schemas/Parent'
        - type: object
          properties:
            childProperty:
              type: string
Generation Details
    <build>
        <plugins>
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>7.0.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/readonly-openapi.yaml</inputSpec>
                            <generatorName>java</generatorName>
                            <library>resttemplate</library>
                            <openapiNormalizer>REF_AS_PARENT_IN_ALLOF=true</openapiNormalizer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
Steps to reproduce

Generate source code with java generator (library resttemplateor other) and setting openapiNormalizer to REF_AS_PARENT_IN_ALLOF=true to force that the first schema of allOf is used as the parent class.

Related issues/PRs

N/A

Suggest a fix

In Java/pojo.mustache probably check for vars not being readonly.

  {{#parent}}
  {{#allVars}}
  {{#isOverridden}}{{! START FIX }}{{^isReadOnly}}{{! END FIX }}
  @Override
  public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) {
    {{#vendorExtensions.x-is-jackson-optional-nullable}}
    this.{{setter}}(JsonNullable.<{{{datatypeWithEnum}}}>of({{name}}));
    {{/vendorExtensions.x-is-jackson-optional-nullable}}
    {{^vendorExtensions.x-is-jackson-optional-nullable}}
    this.{{setter}}({{name}});
    {{/vendorExtensions.x-is-jackson-optional-nullable}}
    return this;
  }

  {{! START FIX }}{{/isReadOnly}}{{! END FIX }}{{/isOverridden}}
  {{/allVars}}
  {{/parent}}

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 Java/pojo.mustache and reproduce the issue using the supplied OpenAPI declaration, the Java generator, and REF_AS_PARENT_IN_ALLOF=true. Check the generated Child code for fluent setters on readOnly inherited properties; done means the generated source compiles without those setters while writable properties retain their expected setters.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.