Skip to content

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

Open

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.

Activity

  1. wing328 commented on Jul 21, 2025

    @wing328
    Member

    yes, please files a PR with the patch and we will review accordingly

  2. nicolaideffremo commented on Aug 6, 2025

    @nicolaideffremo
    Author

    I had another look and the fix is more complicated than I initially thought. I have not considered hierarchical defined default values. From the spec it looks to me like this is allowed:

      schemas:
        Example:
          type: object
          properties:
            nestedArray:
              type: array
              items:
                type: array
                items:
                  type: string
                default: ["value1", "value2"]
              default:
                [
                  [ "h1", "Header 1" ],
                  [ "h2", "Header 2" ],
                  [ "h3", "Header 3" ],
                  [ "h4", "Header 4" ]
                ]

    The defaults contradict each other and the current implementation only considers the top most default definition. Its not clear to me which should win in the end.

    I have to have another look at a later point, if anyone wants to have a look I pushed my rough draft here https://github.com/nicolaideffremo/openapi-generator/tree/fix_issue21589.

  3. wing328 commented on Aug 7, 2025

    @wing328
    Member

    @nicolaideffremo I've filed #21713 (draft) to workaround the issue (default value set to empty array) so that the output at least compiles

  4. nicolaideffremo commented on Aug 7, 2025

    @nicolaideffremo
    Author

    Thanks! I quickly tested that with our example. At least the class is compilable again.

    Sadly the defaultToEmptyContainer option does not address this as well, because the generated default value (, , ,) is shadowing the override behaviour here:

    private String getDefaultCollectionType(Schema schema, String defaultValues) {
           String arrayFormat = "new %s<>(Arrays.asList(%s))";
    
           if (defaultToEmptyContainer) { 
               // respect the default value in the spec
               if (defaultValues == null) { // <-- Wrongly calculated default (, , ,) runs over this.
                   return null;
               } else if (defaultValues.isEmpty()) { // e.g. [] to indicates empty container
                   arrayFormat = "new %s<>()";
                   return getDefaultCollectionType(arrayFormat, defaultValues, ModelUtils.isSet(schema));
               } else { // default value not empty
                   return getDefaultCollectionType(arrayFormat, defaultValues, ModelUtils.isSet(schema));
               }
           }
    }
    

    Might be worth considering this option here as well?

  5. wing328 commented on Aug 7, 2025

    @wing328
    Member

    With my PR, the default is an empty array (container). Why do you still need to enable the option "defaultToEmptyContiner"?

    Do you mean you want to default to null instead of empty array (container)?

  6. nicolaideffremo commented on Aug 8, 2025

    @nicolaideffremo
    Author

    You are right. Sorry, I missed the lazy initialisation, when first adding any elements. Just looked at the field and got confused.

  7. wing328 commented on Aug 8, 2025

    @wing328
    Member

    No problem. Just merged it. Thanks for testing it.

    We will leave this issue opened until someone found a better fix.

  8. marcelstoer commented on Aug 12, 2025

    @marcelstoer
    Contributor

    The defaults contradict each other and the current implementation only considers the top most default definition.

    I guess what the developer-me would be happy with:

    • An empty container plus a warning in the log if contradictory defaults were detected.
    • The top-most definition being generated otherwise (top-most is the only one).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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