Skip to content

Commit 4811da4

Browse files
committed
Issue 21890: Added Test
1 parent 1704429 commit 4811da4

3 files changed

Lines changed: 37 additions & 33 deletions

File tree

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
import io.swagger.v3.parser.core.models.ParseOptions;
2626
import org.mockito.Answers;
2727
import org.mockito.Mockito;
28-
import org.openapitools.codegen.*;
28+
import org.openapitools.codegen.CodegenConstants;
29+
import org.openapitools.codegen.CodegenModel;
30+
import org.openapitools.codegen.CodegenParameter;
31+
import org.openapitools.codegen.TestUtils;
2932
import org.openapitools.codegen.languages.AbstractJavaCodegen;
3033
import org.openapitools.codegen.testutils.ConfigAssert;
3134
import org.openapitools.codegen.utils.ModelUtils;
@@ -752,20 +755,6 @@ public void nullDefaultValueForModelWithDynamicProperties() {
752755
Assert.assertEquals(defaultValue, "null");
753756
}
754757

755-
@Test
756-
public void defaultEmptyListForModelWithAdditionalPropertyDefaultToEmptyContainer() {
757-
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_1/java/issue_21890.yaml");
758-
codegen.additionalProperties().put(CodegenConstants.DEFAULT_TO_EMPTY_CONTAINER, "map");
759-
codegen.processOpts();
760-
codegen.setOpenAPI(openAPI);
761-
762-
Schema<?> schema = openAPI.getComponents().getSchemas().get("Pet_parents");
763-
764-
CodegenProperty parents = codegen.fromProperty("parents", schema, true);
765-
String defaultValue = codegen.toDefaultValue(parents, schema);
766-
Assert.assertEquals(defaultValue, "new HashMap()");
767-
}
768-
769758
@Test
770759
public void maplikeDefaultValueForModelWithStringToStringMapping() {
771760
final OpenAPI openAPI = FLATTENED_SPEC.get("3_0/mapSchemas");

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<>();

modules/openapi-generator/src/test/resources/3_1/java/issue_21890.yaml renamed to modules/openapi-generator/src/test/resources/3_0/java/issue_21890.yaml

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
openapi: 3.1.0
1+
openapi: 3.0.0
22
servers:
33
- url: 'http://petstore.swagger.io/v2'
44
info:
55
description: >-
6-
This is a sample server Petstore server. For this sample, you can use the api key
7-
`special-key` to test the authorization filters.
6+
This is a sample server Petstore server
87
version: 1.0.0
98
title: OpenAPI Petstore
109
license:
@@ -33,25 +32,17 @@ paths:
3332
application/json:
3433
schema:
3534
$ref: '#/components/schemas/Pet'
36-
'400':
37-
description: Invalid ID supplied
38-
'404':
39-
description: Pet not found
4035
components:
4136
schemas:
4237
Pet:
4338
title: a Pet
4439
description: A pet for sale in the pet store
4540
type: object
46-
properties:
47-
parents:
48-
type: object
49-
properties:
50-
mother:
51-
type: string
52-
father:
53-
type: string
5441
required:
55-
- parents
56-
57-
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)