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 @@ -4383,14 +4383,23 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
}

// override defaultValue if it's not set and defaultToEmptyContainer is set
if (p.getDefault() == null && defaultToEmptyContainer) {
boolean emptyContainerDefault = p.getDefault() == null && defaultToEmptyContainer;
boolean defaultSetFlag = p.getDefaultSetFlag();
if (emptyContainerDefault) {
updateDefaultToEmptyContainer(property, p);
}

// set the default value
property.defaultValue = toDefaultValue(property, p);
property.defaultValueWithParam = toDefaultValueWithParam(name, p);

if (emptyContainerDefault) {
// p may be shared with other properties (e.g. via $ref or allOf) and the empty-container
// default depends on this property's required flag, so take it off the schema again
p.setDefault(null);
p.setDefaultSetFlag(defaultSetFlag);
}

LOGGER.debug("debugging from property return: {}", property);
schemaCodegenPropertyCache.put(ns, property);
return property;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5893,6 +5893,32 @@ public void testMicroprofileNullableRequiredContainers_issue24776() {
.doesNotContain(" = ;");
}

@Test
public void testDefaultToEmptyContainerDoesNotLeakAcrossSharedSchemaRefs_issue25522() {
final Path output = newTempFolder();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName(JAVA_GENERATOR)
.setLibrary(JavaClientCodegen.RESTTEMPLATE)
.setAdditionalProperties(Map.of(
CodegenConstants.MODEL_PACKAGE, "org.openapitools.client.model",
"defaultToEmptyContainer", "array|map"
))
.setInputSpec("src/test/resources/3_0/java/issue_25522.yaml")
.setOutputDir(output.toString().replace("\\", "/"));

List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();

validateJavaSourceFiles(files);
// requiredStrings and requiredMap come first and share their schema with the optional ones
assertThat(output.resolve("src/main/java/org/openapitools/client/model/ExampleObject.java")).content()
.contains("private List<String> requiredStrings = new ArrayList<>();")
.contains("private List<String> optionalStrings;")
.contains("private Map<String, String> requiredMap = new HashMap<>();")
.contains("private Map<String, String> optionalMap;");
// the spec declares no default, so none may leak into the exported copy
assertThat(output.resolve("api/openapi.yaml")).content().doesNotContain("default:");
}

private static JavaClientCodegen newRetrofit2Codegen(Map<String, Object> properties) {
JavaClientCodegen codegen = new JavaClientCodegen();
codegen.setLibrary(JavaClientCodegen.RETROFIT_2);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
openapi: 3.0.0
info:
version: 1.0.0
title: Example API
paths:
/info:
get:
operationId: getInfo
responses:
'200':
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/ExampleObject'
components:
schemas:
StringArray:
type: array
items:
type: string
StringMap:
type: object
additionalProperties:
type: string
ExampleObject:
type: object
properties:
requiredStrings:
$ref: '#/components/schemas/StringArray'
optionalStrings:
$ref: '#/components/schemas/StringArray'
requiredMap:
$ref: '#/components/schemas/StringMap'
optionalMap:
$ref: '#/components/schemas/StringMap'
required:
- requiredStrings
- requiredMap
Loading
Loading