Skip to content

Commit 9e01e75

Browse files
MarcoZurichMarco
authored andcommitted
Fix "defaultToEmptyContainer" not properly implemented for non-nullable, required Map (OpenAPITools#21953)
* Issue 21890: Fixed flag to use for check of required, non-nullable map * Issue 21890: added test (first draft) * Issue 21890: Added Test --------- Co-authored-by: Marco <[email protected]>
1 parent 84d54eb commit 9e01e75

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,4 +454,6 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
454454
public static final String WAIT_TIME_OF_THREAD = "waitTimeMillis";
455455

456456
public static final String USE_DEFAULT_VALUES_FOR_REQUIRED_VARS = "useDefaultValuesForRequiredVars";
457+
458+
public static final String DEFAULT_TO_EMPTY_CONTAINER = "defaultToEmptyContainer";
457459
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
import java.util.stream.Collectors;
8989
import java.util.stream.Stream;
9090

91+
import static org.openapitools.codegen.CodegenConstants.DEFAULT_TO_EMPTY_CONTAINER;
9192
import static org.openapitools.codegen.CodegenConstants.UNSUPPORTED_V310_SPEC_MSG;
9293
import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER;
9394
import static org.openapitools.codegen.utils.OnceLogger.once;
@@ -338,7 +339,7 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
338339
@Setter @Getter boolean arrayDefaultToEmpty, arrayNullableDefaultToEmpty, arrayOptionalNullableDefaultToEmpty, arrayOptionalDefaultToEmpty;
339340
@Setter @Getter boolean mapDefaultToEmpty, mapNullableDefaultToEmpty, mapOptionalNullableDefaultToEmpty, mapOptionalDefaultToEmpty;
340341
@Setter @Getter protected boolean defaultToEmptyContainer;
341-
final String DEFAULT_TO_EMPTY_CONTAINER = "defaultToEmptyContainer";
342+
342343
final List EMPTY_LIST = new ArrayList();
343344

344345
@Override
@@ -4289,7 +4290,7 @@ void updateDefaultToEmptyContainer(CodegenProperty cp, Schema p) {
42894290
} else { // required
42904291
if (cp.isNullable && mapNullableDefaultToEmpty) { // nullable
42914292
p.setDefault(EMPTY_LIST);
4292-
} else if (!cp.isNullable && mapOptionalDefaultToEmpty) { // non-nullable
4293+
} else if (!cp.isNullable && mapDefaultToEmpty) { // non-nullable
42934294
p.setDefault(EMPTY_LIST);
42944295
}
42954296
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5634,6 +5634,30 @@ public void testCollectionTypesWithDefaults_issue_collection() throws IOExceptio
56345634
.fileContains("private List<String> stringRequiredList = new ArrayList<>();");
56355635
}
56365636

5637+
@Test
5638+
public void testDefaultForRequiredNonNullableMap() throws IOException {
5639+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
5640+
output.deleteOnExit();
5641+
5642+
OpenAPI openAPI = new OpenAPIParser()
5643+
.readLocation("src/test/resources/3_0/java/issue_21890.yaml", null, new ParseOptions()).getOpenAPI();
5644+
SpringCodegen codegen = new SpringCodegen();
5645+
codegen.setLibrary(SPRING_BOOT);
5646+
codegen.setOutputDir(output.getAbsolutePath());
5647+
codegen.additionalProperties().put("defaultToEmptyContainer", "map");
5648+
5649+
ClientOptInput input = new ClientOptInput()
5650+
.openAPI(openAPI)
5651+
.config(codegen);
5652+
5653+
DefaultGenerator generator = new DefaultGenerator();
5654+
Map<String, File> files = generator.opts(input).generate().stream()
5655+
.collect(Collectors.toMap(File::getName, Function.identity()));
5656+
5657+
JavaFileAssert.assertThat(files.get("Pet.java"))
5658+
.fileContains("private Map<String, String> requiredNonNullableMap = new HashMap<>();");
5659+
}
5660+
56375661
@Test
56385662
public void testGenericReturnTypeWhenUsingResponseEntity_issue1096() throws IOException {
56395663
Map<String, Object> additionalProperties = new HashMap<>();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
openapi: 3.0.0
2+
servers:
3+
- url: 'http://petstore.swagger.io/v2'
4+
info:
5+
description: >-
6+
This is a sample server Petstore server
7+
version: 1.0.0
8+
title: OpenAPI Petstore
9+
license:
10+
name: Apache-2.0
11+
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
12+
paths:
13+
'/pet/{petId}':
14+
get:
15+
tags:
16+
- pet
17+
summary: Find pet by ID
18+
description: Returns a single pet
19+
operationId: getPetById
20+
parameters:
21+
- name: petId
22+
in: path
23+
description: ID of pet to return
24+
required: true
25+
schema:
26+
type: integer
27+
format: int64
28+
responses:
29+
'200':
30+
description: successful operation
31+
content:
32+
application/json:
33+
schema:
34+
$ref: '#/components/schemas/Pet'
35+
components:
36+
schemas:
37+
Pet:
38+
title: a Pet
39+
description: A pet for sale in the pet store
40+
type: object
41+
required:
42+
- requiredNonNullableMap
43+
properties:
44+
# Test for issue 21890.
45+
# As this map is required and non-nullable, the generated java class should initialise this Map with new HashMap()
46+
requiredNonNullableMap:
47+
additionalProperties:
48+
type: string

0 commit comments

Comments
 (0)