Skip to content
Merged
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 @@ -454,4 +454,6 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
public static final String WAIT_TIME_OF_THREAD = "waitTimeMillis";

public static final String USE_DEFAULT_VALUES_FOR_REQUIRED_VARS = "useDefaultValuesForRequiredVars";

public static final String DEFAULT_TO_EMPTY_CONTAINER = "defaultToEmptyContainer";
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.openapitools.codegen.CodegenConstants.DEFAULT_TO_EMPTY_CONTAINER;
import static org.openapitools.codegen.CodegenConstants.UNSUPPORTED_V310_SPEC_MSG;
import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER;
import static org.openapitools.codegen.utils.OnceLogger.once;
Expand Down Expand Up @@ -338,7 +339,7 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
@Setter @Getter boolean arrayDefaultToEmpty, arrayNullableDefaultToEmpty, arrayOptionalNullableDefaultToEmpty, arrayOptionalDefaultToEmpty;
@Setter @Getter boolean mapDefaultToEmpty, mapNullableDefaultToEmpty, mapOptionalNullableDefaultToEmpty, mapOptionalDefaultToEmpty;
@Setter @Getter protected boolean defaultToEmptyContainer;
final String DEFAULT_TO_EMPTY_CONTAINER = "defaultToEmptyContainer";

final List EMPTY_LIST = new ArrayList();

@Override
Expand Down Expand Up @@ -4289,7 +4290,7 @@ void updateDefaultToEmptyContainer(CodegenProperty cp, Schema p) {
} else { // required
if (cp.isNullable && mapNullableDefaultToEmpty) { // nullable
p.setDefault(EMPTY_LIST);
} else if (!cp.isNullable && mapOptionalDefaultToEmpty) { // non-nullable
} else if (!cp.isNullable && mapDefaultToEmpty) { // non-nullable
p.setDefault(EMPTY_LIST);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5634,6 +5634,30 @@ public void testCollectionTypesWithDefaults_issue_collection() throws IOExceptio
.fileContains("private List<String> stringRequiredList = new ArrayList<>();");
}

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

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/java/issue_21890.yaml", null, new ParseOptions()).getOpenAPI();
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_BOOT);
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put("defaultToEmptyContainer", "map");

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

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

JavaFileAssert.assertThat(files.get("Pet.java"))
.fileContains("private Map<String, String> requiredNonNullableMap = new HashMap<>();");
}

@Test
public void testGenericReturnTypeWhenUsingResponseEntity_issue1096() throws IOException {
Map<String, Object> additionalProperties = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
openapi: 3.0.0
servers:
- url: 'http://petstore.swagger.io/v2'
info:
description: >-
This is a sample server Petstore server
version: 1.0.0
title: OpenAPI Petstore
license:
name: Apache-2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
paths:
'/pet/{petId}':
get:
tags:
- pet
summary: Find pet by ID
description: Returns a single pet
operationId: getPetById
parameters:
- name: petId
in: path
description: ID of pet to return
required: true
schema:
type: integer
format: int64
responses:
'200':
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
components:
schemas:
Pet:
title: a Pet
description: A pet for sale in the pet store
type: object
required:
- requiredNonNullableMap
properties:
# Test for issue 21890.
# As this map is required and non-nullable, the generated java class should initialise this Map with new HashMap()
requiredNonNullableMap:
additionalProperties:
type: string
Loading