Skip to content

[BUG] [JAVA] Defaults for nested arrays produce invalid code #21589

@nicolaideffremo

Description

@nicolaideffremo

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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

When I define a default value for a nested array structure the resulting Java code can not be compiled. I attached a specification, that reproduces the issue.

openapi-generator version

7.14

OpenAPI declaration file content or url
openapi: 3.0.4
info:
  title: Title
  description: Title
  version: 1.0.0
servers:
  - url: 'https'
paths: {}
components:
  schemas:
    Example:
      type: object
      properties:
        nestedArray:
          type: array
          items:
            type: array
            items:
              type: string
          default:
            [
              [ "h1", "Header 1" ],
              [ "h2", "Header 2" ],
              [ "h3", "Header 3" ],
              [ "h4", "Header 4" ]
            ]
Generation Details
                    <groupId>org.openapitools</groupId>
                    <artifactId>openapi-generator-maven-plugin</artifactId>
                    <version>${openapi-generator-version}</version>
                    <configuration>
                        <configOptions>
                            <library>resttemplate</library>
                        </configOptions>  
                        <generateApis>false</generateApis>
                        <generateApiDocumentation>false</generateApiDocumentation>
                        <generateApiTests>false</generateApiTests>
                        <generateModels>true</generateModels>
                        <generateModelDocumentation>false</generateModelDocumentation>
                        <generateModelTests>false</generateModelTests>
                        <generatorName>java</generatorName>
                        <generateSupportingFiles>false</generateSupportingFiles>

                    </configuration>
Steps to reproduce

Generate the Java class with my OpenApi above and try to compile the resulting class.

The issue should look like this: private List<List<String>> nestedArray = new ArrayList<>(Arrays.asList(, , ,,));

Suggest a fix

I traced the issue down to this part of the AbstractJavaCodegen.toArrayDefaultValue()

The problem is that element.asText() receives an ArrayNode, that will return as empty string for every element.

I have played around with a patch, that recursively walks further down the defaults, but its only a rough draft. I you are interested I can share that.

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