Skip to content

[BUG][JAVA] Default value of required array #12678

Open

Description

@lijau

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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

For Java, the default value of an array is an empty ArrayList as soon as it is declared as "required" in the enclosing object. In our case, we must reject the null value with the appropriate validation: @javax.validation.constraints.NotNull. However, with this default value, we can only trigger another kind of validation: @javax.validation.constraints.NotEmpty, which can be misleading for our API users since the field is not present at all. This behavior does not seem to be consistent with the other types (string, number, integer, boolean, object) which remain at null despite the required property.

openapi-generator version

'org.openapi.generator' version '5.3.0' but reproduced in 6.0.0

OpenAPI declaration file content or url

Given :

    MyEnclosingObject:
      type: object
      required:
        - my_array
      properties:
        my_array:
          type: array
          items:
            $ref: '#/components/schemas/MyItem'

Expected generated source :

	public class MyEnclosingObject {

		@JsonProperty("my_array")
		@Valid
		private List<MyArray> myArray = null;

		public MyEnclosingObject myArray(List<MyArray> myArray) {
			this.myArray = myArray;
			return this;
		}

		public MyEnclosingObject addMyArray(MyItem myItem) {
			if (myArray == null) {
				myArray = new ArrayList<>();
                        }
			this.myArray.add(myItem);
			return this;
		}

		@ApiModelProperty(required = true)
		@NotNull @Valid
		public List<StandardPrice> getStandardPrices() {
			return standardPrices;
		}
	}

Actual generated source :

	public class MyEnclosingObject {

		@JsonProperty("my_array")
		@Valid
		private List<MyArray> myArray = new ArrayList<>();

		public MyEnclosingObject myArray(List<MyArray> myArray) {
			this.myArray = myArray;
			return this;
		}

		public MyEnclosingObject addMyArray(MyItem myItem) {
			this.myArray.add(myItem);
			return this;
		}

		@ApiModelProperty(required = true)
		@NotNull @Valid
		public List<MyItem> getMyArray() {
			return standardPrices;
		}
	}
Generation Details

Maven plugin in generate goal.
org.openapitools
openapi-generator-maven-plugin

Steps to reproduce

Define a contract of an object with a required array and call the API without that array and observe that the received array is empty instead of null.

Related issues/PRs

None found

Suggest a fix

The default value should be moved to addMyArray when we know we have at least one myItem

Activity

  1. changed the title [-]BUG][JAVA] Default value of required array[/-] [+][BUG][JAVA] Default value of required array[/+] on Jun 23, 2022
  2. wing328 commented on Jul 20, 2023

    @wing328
    Member

    can you please try v7.0.0-beta? there are some improvements related to java array default value if I remember correctly.

  3. cpandya231 commented on Aug 8, 2023

    @cpandya231

    @wing328 I tried with 7.0.0-beta, the issue still remains.

  4. wing328 commented on Aug 8, 2023

    @wing328
    Member

    can you please provide a minimal spec to reproduce the issue?

  5. cpandya231 commented on Aug 8, 2023

    @cpandya231
       SampleRequestBody:
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SampleRequest'
    
       SampleRequest:
          allOf:
            - $ref: '#/components/schemas/Roles'
       
       Roles:
          type: object
          properties:
            roles:
              type: array
              items:
                type: string
              default: [ ]
              example: [ Role1, Role2 ]     
    
    

    Generated method

      public SampleRequest addRolesItem(String rolesItem) {
        if (this.roles == null) {
          this.roles = null;
        }
        this.roles.add(rolesItem);
        return this;
      }
    
    
  6. wing328 commented on Aug 8, 2023

    @wing328
    Member

    I did a test with the latest master using java generator (default library: oktthp-gson) and got

    @javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
    public class SampleRequest {
      public static final String SERIALIZED_NAME_ROLES = "roles";
      @SerializedName(SERIALIZED_NAME_ROLES)
      private List<String> roles = new ArrayList<>();
    
      public SampleRequest() {
      }
    
      public SampleRequest roles(List<String> roles) {
        
        this.roles = roles;
        return this;
      }
    
      public SampleRequest addRolesItem(String rolesItem) {
        if (this.roles == null) {
          this.roles = new ArrayList<>();
        }
        this.roles.add(rolesItem);
        return this;
      }

    which looks to be correct.

  7. cpandya231 commented on Aug 9, 2023

    @cpandya231

    @wing328 You are right, it works with 7.0.0-beta

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

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