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] [7.0.0] Fluent setters generated for readOnly properties #16498

Open
5 of 6 tasks
wsalembi opened this issue Sep 4, 2023 · 1 comment
Open
5 of 6 tasks

Comments

@wsalembi
Copy link

wsalembi commented Sep 4, 2023

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 #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}}
@wing328
Copy link
Member

wing328 commented Sep 12, 2023

can you please file a PR with the suggested fix when you've time? thanks.

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

2 participants