Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1326,8 +1326,8 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
// if default to empty container option is set, respect the default values provided in the spec
return toArrayDefaultValue(cp, schema);
} else if (schema.getDefault() == null) {
// nullable or containerDefaultToNull set to true
if (cp.isNullable || containerDefaultToNull) {
// nullable or containerDefaultToNull set to true or an optional array with minItems > 0
if (cp.isNullable || containerDefaultToNull || (cp.minItems != null && cp.minItems > 0 && !cp.getHasRequired())) {
return null;
}
return getDefaultCollectionType(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1590,7 +1590,6 @@ public void shouldGenerateDefaultValueForEnumRequestParameter() throws IOExcepti
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/api/GetApi.java"),
"@RequestParam(value = \"testParameter1\", required = false, defaultValue = \"BAR\")",
"@RequestParam(value = \"TestParameter2\", required = false, defaultValue = \"BAR\")");

}

/**
Expand Down Expand Up @@ -5075,7 +5074,36 @@ public void optionalListShouldBeEmpty() throws IOException {
JavaFileAssert.assertThat(files.get("PetDto.java"))
.fileContains("private List<@Valid TagDto> tags = new ArrayList<>();")
.fileContains("private List<String> photoUrls = new ArrayList<>();");
}

@Test
public void testOptionalListWithMinItems1ShouldBeNull_issue_22784() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_1/issue_22784.yaml", null, new ParseOptions()).getOpenAPI();
SpringCodegen codegen = new SpringCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(INTERFACE_ONLY, "true");
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "xyz.model");
codegen.additionalProperties().put(CodegenConstants.API_NAME_SUFFIX, "Controller");
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xyz.controller");
codegen.additionalProperties().put(CodegenConstants.MODEL_NAME_SUFFIX, "Dto");

ClientOptInput input = new ClientOptInput()
.openAPI(openAPI)
.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
generator.setGenerateMetadata(false); // skip metadata and ↓ only generate models
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");

Map<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));

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

@Test
Expand Down
45 changes: 45 additions & 0 deletions modules/openapi-generator/src/test/resources/3_1/issue_22784.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
openapi: 3.1.1
info:
title: Optional Array with minItems 1 OpenAPI Example
version: 1.0.0
paths:
/foo-bar:
put:
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/FooBarRequest'
responses:
204:
description: success

components:
schemas:
FooBarRequest:
type: object
properties:
foo:
properties:
name:
type: string
required:
- name
barList:
description: |
This array is optional. If provided, it must contain between 1 and 3 items
type: array
items:
$ref: '#/components/schemas/Bar'
minItems: 1
maxItems: 3
required:
- foo
Bar:
type: object
properties:
someProperty:
type: string
required:
- someProperty