OpenAPITools / OpenAPITools/openapi-generator

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

Open
#21,589 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java, at toArrayDefaultValue(), and reproduce the issue with the provided OpenAPI declaration using the Java generator. Check the generated nestedArray initializer and compile the resulting model; done means nested array defaults produce valid Java rather than empty elements.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.