openapi-generator: [BUG][JAVA] Since 6.5.0 Generated Lists no longer default to empty List but null

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 nullable arrays, our generated POJOs in 6.5.0 (and 6.6.0) no longer generate initialized to an empty ArrayList (e.g private @Valid List<String> options = new ArrayList<>(); ), but instead are generated just as a non-initialized private variable private @Valid List<String> options;

Attempts to use containerDefaultToNull: 'false', and default: [] to hack our way through are not working: The first has no noticeable effect, and the second fails due to the generator failing to import the java.utils.Arrays package.

openapi-generator version

Using 6.4.0, breaks in 6.5.0 and 6.6.0

OpenAPI declaration file content or url
title: AttributeDto
properties:
    blah:
        type: string
        format: uuid
    ....
    options:
        description: |
            description
        type: array
        items:
            type: string
required:
    - blah
Generation Details

./gradlew generateApi

task cleanGeneratedApi (type: Delete) { delete fileTree(“${project.buildDir}/generated/api”) }

def openApiSpec = “${project.projectDir}/src/main/resources/api.yml” // Generate REST API specification task generateApi( type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask ) { inputSpec = openApiSpec outputDir = “${project.buildDir}/generated/api” templateDir = “${rootDir}/openapi” invokerPackage = ‘com.api’ apiPackage = ‘com.api’ modelPackage = ‘com.model’ generatorName = ‘jaxrs-spec’ configOptions = [ interfaceOnly : ‘true’, returnResponse : ‘true’, useTags: ‘true’ ] // Represent date-time objects as Timestamp rather than Date typeMappings = [Date : “Timestamp”] importMappings = [“java.util.Date” : “java.sql.Timestamp”]

dependsOn(cleanGeneratedApi)

}

Steps to reproduce
  1. Model with attribute type of array, jaxrs-spec generator, configOptions as defined above
  2. Run the gradle generate task with ./gradlew generateApi
  3. Attempting to use obj.getOptions() returns a null value instead of an empty list as before
Related issues/PRs

Potentially related? https://github.com/OpenAPITools/openapi-generator/issues/14583 https://github.com/OpenAPITools/openapi-generator/pull/14959 https://github.com/OpenAPITools/openapi-generator/pull/14961

Suggest a fix

Check if the property’s type is array, initialize it with an empty ArrayList, as before.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 1
  • Comments: 16 (6 by maintainers)

Most upvoted comments

I’m seeing this also after moving from 6.3.0 to 7.0.1 but given #14875 this seems like the intended behavior. But the containerDefaultToNull option seems broken. Based on #14130 it is supposed to allow fallback to the previous behavior but it doesn’t. It only allows you to opt into null defaults, not out of them.

Same issue (Swagger 2.0 specs). Not required arrays should be initialized to null. Required arrays should be initialized with new ...