Repository navigation
Open
Labels
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.
Activity
wing328 commentedon Jul 21, 2025
yes, please files a PR with the patch and we will review accordingly
nicolaideffremo commentedon Aug 6, 2025
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:
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.
wing328 commentedon Aug 7, 2025
@nicolaideffremo I've filed #21713 (draft) to workaround the issue (default value set to empty array) so that the output at least compiles
nicolaideffremo commentedon Aug 7, 2025
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:
Might be worth considering this option here as well?
wing328 commentedon Aug 7, 2025
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)?
nicolaideffremo commentedon Aug 8, 2025
You are right. Sorry, I missed the lazy initialisation, when first adding any elements. Just looked at the field and got confused.
wing328 commentedon Aug 8, 2025
No problem. Just merged it. Thanks for testing it.
We will leave this issue opened until someone found a better fix.
marcelstoer commentedon Aug 12, 2025
I guess what the developer-me would be happy with: