Skip to content

Commit 61e4916

Browse files
kingofdisasterrSandro Kojan
authored andcommitted
[BUG][Java][Spring][SpringBoot] optional array with 'minItems' set generates to uninitialized list
1 parent ea6f4c8 commit 61e4916

3 files changed

Lines changed: 76 additions & 3 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,8 +1326,8 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
13261326
// if default to empty container option is set, respect the default values provided in the spec
13271327
return toArrayDefaultValue(cp, schema);
13281328
} else if (schema.getDefault() == null) {
1329-
// nullable or containerDefaultToNull set to true
1330-
if (cp.isNullable || containerDefaultToNull) {
1329+
// nullable or containerDefaultToNull set to true or an optional array with minItems > 0
1330+
if (cp.isNullable || containerDefaultToNull || (cp.minItems != null && cp.minItems > 0 && !cp.getHasRequired())) {
13311331
return null;
13321332
}
13331333
return getDefaultCollectionType(schema);

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,6 @@ public void shouldGenerateDefaultValueForEnumRequestParameter() throws IOExcepti
15901590
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/api/GetApi.java"),
15911591
"@RequestParam(value = \"testParameter1\", required = false, defaultValue = \"BAR\")",
15921592
"@RequestParam(value = \"TestParameter2\", required = false, defaultValue = \"BAR\")");
1593-
15941593
}
15951594

15961595
/**
@@ -5075,7 +5074,36 @@ public void optionalListShouldBeEmpty() throws IOException {
50755074
JavaFileAssert.assertThat(files.get("PetDto.java"))
50765075
.fileContains("private List<@Valid TagDto> tags = new ArrayList<>();")
50775076
.fileContains("private List<String> photoUrls = new ArrayList<>();");
5077+
}
5078+
5079+
@Test
5080+
public void testOptionalListWithMinItems1ShouldBeNull_issue_22784() throws IOException {
5081+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
5082+
output.deleteOnExit();
5083+
5084+
OpenAPI openAPI = new OpenAPIParser()
5085+
.readLocation("src/test/resources/3_1/issue_22784.yaml", null, new ParseOptions()).getOpenAPI();
5086+
SpringCodegen codegen = new SpringCodegen();
5087+
codegen.setOutputDir(output.getAbsolutePath());
5088+
codegen.additionalProperties().put(INTERFACE_ONLY, "true");
5089+
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "xyz.model");
5090+
codegen.additionalProperties().put(CodegenConstants.API_NAME_SUFFIX, "Controller");
5091+
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xyz.controller");
5092+
codegen.additionalProperties().put(CodegenConstants.MODEL_NAME_SUFFIX, "Dto");
5093+
5094+
ClientOptInput input = new ClientOptInput()
5095+
.openAPI(openAPI)
5096+
.config(codegen);
5097+
5098+
DefaultGenerator generator = new DefaultGenerator();
5099+
generator.setGenerateMetadata(false); // skip metadata and ↓ only generate models
5100+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
5101+
5102+
Map<String, File> files = generator.opts(input).generate().stream()
5103+
.collect(Collectors.toMap(File::getName, Function.identity()));
50785104

5105+
JavaFileAssert.assertThat(files.get("FooBarRequestDto.java"))
5106+
.fileContains("private @Nullable List<@Valid BarDto> barList;");
50795107
}
50805108

50815109
@Test
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
openapi: 3.1.1
2+
info:
3+
title: Optional Array with minItems 1 OpenAPI Example
4+
version: 1.0.0
5+
paths:
6+
/foo-bar:
7+
put:
8+
requestBody:
9+
required: true
10+
content:
11+
application/json:
12+
schema:
13+
$ref: '#/components/schemas/FooBarRequest'
14+
responses:
15+
204:
16+
description: success
17+
18+
components:
19+
schemas:
20+
FooBarRequest:
21+
type: object
22+
properties:
23+
foo:
24+
properties:
25+
name:
26+
type: string
27+
required:
28+
- name
29+
barList:
30+
description: |
31+
This array is optional. If provided, it must contain between 1 and 3 items
32+
type: array
33+
items:
34+
$ref: '#/components/schemas/Bar'
35+
minItems: 1
36+
maxItems: 3
37+
required:
38+
- foo
39+
Bar:
40+
type: object
41+
properties:
42+
someProperty:
43+
type: string
44+
required:
45+
- someProperty

0 commit comments

Comments
 (0)