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

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

Closed
pmossman opened this issue Jul 27, 2022 · 0 comments · Fixed by #13076

Comments

@pmossman
Copy link

pmossman commented Jul 27, 2022

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() {
  }

  // ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant