Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #13025
Closed
Open
Issue created Jul 27, 2022 by Administrator@rootContributor

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

Created by: 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: https://github.com/OpenAPITools/openapi-generator/issues/3585#issuecomment-519450159

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

  // ...
}
Assignee
Assign to
Time tracking