Skip to content

How can we set List to null by default in models? #1861

Closed
@harikrishnanmurthy

Description

@harikrishnanmurthy

While generating Java models from OpenAPI spec (v3.0.0) using openapi-generator-maven-plugin, all the Lists in java class are initialized by default to new ArrayList<>(). How do we prevent this?

Description

We have an attribute defined as an array in our OpenAPI spec. While trying to generate the Java model classes, the resulting List is initialized by default to new ArrayList<>(). This is causing problems in our application since an empty arraylist and null are treated differently.

openapi-generator version

openapi-generator-maven-plugin
version : 3.3.0

OpenAPI declaration file content or url
    "references": {"type" : "array", "items" : {
        "type": "object",
        "properties" : {
            "type": {"type" : "string"},
            "source": {"type" : "string"},
            "value": {"type" : "string"}
        },
        "required": ["type", "value"],
        "additionalProperties": false
    }}

When the maven plugin is run, the resulting java code is

private List references= new ArrayList<>();

The reference field is not mandatory(optional)

What we want is:

private List references = null;

Command line used for generation

Generated using openapi-generator-maven-plugin version : 3.3.0

Steps to reproduce
Related issues/PRs
Suggest a fix

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