Skip to content

[BUG] [JAVA] [webclient] Model with readonly, nullable properties generates uncompilable code #12790

@kzander91

Description

@kzander91

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

Model properties with nullable: true and readOnly: true cause invalid code to be generated.

The nullable: true causes the type to be wrapped in JsonNullable<T>, and the readOnly: true causes a @JsonCreator constructor to be generated with argument type T, attempting to set the arg to the class field.

The example spec below results in the following Java class:

// imports, annotations
public class TestObject {
  public static final String JSON_PROPERTY_MY_PROP = "myProp";
  private JsonNullable<String> myProp = JsonNullable.<String>undefined();

  public TestObject() { 
  }

  @JsonCreator
  public TestObject(
    @JsonProperty(JSON_PROPERTY_MY_PROP) String myProp
  ) {
    this();
    this.myProp = myProp; // <--- compiler error here, String cannot by assigned to JsonNullable<String>
  }

// getters, setters
}
openapi-generator version

5.3.1 and above.
I tracked it down to this commit, generating the same spec with 5.3.0 does not generate a @JsonCreator constructor and thus does not cause compiler errors.

OpenAPI declaration file content or url
openapi: 3.0.3
info:
    title: Title
    version: "1"
paths:
    /test:
        get:
            responses:
                200:
                    description: Success
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/TestObject'
components:
    schemas:
        TestObject:
            type: object
            properties:
                myProp:
                    type: string
                    nullable: true
                    readOnly: true
Generation Details

java -jar openapi-generator-cli.jar generate -g java --library webclient -i spec.yaml -o out

Steps to reproduce
  1. Model with attributes being both readOnly and nullable.
  2. Generate with generator java and library webclient.
  3. Model property is of type JsonNullable<String>, constructor arg is of type String. Assignment takes place without wrapping into JsonNullable.
Related issues/PRs

#10086
#10113

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions