Skip to content

[REQ][JaxRS-Spec][Model] Optional array fields should default to null instead of an instantiated ArrayList #13025

@pmossman

Description

@pmossman

I'm willing to sponsor work on this issue to the tune of $300 (cc @wing328)

Is your feature request related to a problem? Please describe.

I am trying to implement a PATCH endpoint that only modifies non-null attributes of the incoming request body. The request body has an optional field with type array, that should be set to null for PATCH requests that don't modify this field.

However, the jaxrs-spec codegen forces a default value of new ArrayList<>() instead of null. This means that the server cannot distinguish between a PATCH request that wants to set the field to an empty list, versus a PATCH request that wants to leave that field alone.

Describe the solution you'd like

It looks like the behavior I want is present in the JavaJaxRS/pojo.mustache template here:
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaJaxRS/pojo.mustache#L39-L44

but not in the JavaJaxRS/spec/pojo.mustache template:
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache#L28

(Note that I'm very new to this codebase and doing my best to piece together how this all works, apologies if I'm not linking to the right places in code).

This PR has a very similar discussion and solution for the java generator: #3585 (comment)

Describe alternatives you've considered

I'm open to ideas for workarounds/alternatives!

Additional context

Steps to recreate my problem from scratch:

  1. docker pull openapitools/openapi-generator-cli:latest
  2. Create the following spec.yaml:
openapi: "3.0.3"
info:
  version: 1.0.0
  title: Array Not Null
paths:
  /array/not/null:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Body"
      responses:
        "200":
          description: Success
components:
  schemas:
    Body:
      type: object
      properties:
        arrayThatIWantToBeNull:
          type: array
          nullable: true
          items:
            type: string
  1. docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:latest generate -i local/spec.yaml -g jaxrs-spec -o /local/out/java
  2. Examine the generated Body.java, notice that it defaults to a new ArrayList<>() instead of null:
@JsonTypeName("Body")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen", date = "2022-07-27T00:42:00.516541Z[Etc/UTC]")
public class Body   {
  
  private @Valid List<String> arrayThatIWantToBeNull = new ArrayList<>();
 
 // ...
}
  1. Compare this with using the java generator, for example, which generates this Body.java:
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2022-07-27T00:46:18.861699Z[Etc/UTC]")
public class Body {
  public static final String SERIALIZED_NAME_ARRAY_THAT_I_WANT_TO_BE_NULL = "arrayThatIWantToBeNull";
  @SerializedName(SERIALIZED_NAME_ARRAY_THAT_I_WANT_TO_BE_NULL)
  private List<String> arrayThatIWantToBeNull = null;

  public Body() {
  }

  // ...
}

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