Skip to content

Commit 7bfc53b

Browse files
authored
[core] Fix system properties being immutable (#4447)
When WorkflowSettings was constructed from an existing instance, as is the case when we deserialize from an external configuration file, it would result in an error: Caused by: java.lang.UnsupportedOperationException at com.google.common.collect.ImmutableMap.put(ImmutableMap.java:450) at org.openapitools.codegen.config.WorkflowSettings$Builder.withSystemProperty(WorkflowSettings.java:465) This was due to an error in `newBuilder(WorkflowSettings copy)` which assigned builder.systemProperties with an immutable map. This is incorrect because everything in the builder should be mutable until .build() is invoked. This likely affects CLI/Maven plugin as well for version 4.1.1 through 4.2.0.
1 parent 84d3562 commit 7bfc53b

4 files changed

Lines changed: 40 additions & 1 deletion

File tree

modules/openapi-generator-core/src/main/java/org/openapitools/codegen/config/WorkflowSettings.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ public static Builder newBuilder(WorkflowSettings copy) {
103103
builder.strictSpecBehavior = copy.isStrictSpecBehavior();
104104
builder.templatingEngineName = copy.getTemplatingEngineName();
105105
builder.ignoreFileOverride = copy.getIgnoreFileOverride();
106-
builder.systemProperties = ImmutableMap.copyOf(copy.getSystemProperties());
106+
107+
// this, and any other collections, must be mutable in the builder.
108+
builder.systemProperties = new HashMap<>(copy.getSystemProperties());
107109

108110
// force builder "with" methods to invoke side effects
109111
builder.withTemplateDir(copy.getTemplateDir());
@@ -272,6 +274,8 @@ public static final class Builder {
272274
private String templateDir;
273275
private String templatingEngineName = DEFAULT_TEMPLATING_ENGINE_NAME;
274276
private String ignoreFileOverride;
277+
278+
// NOTE: All collections must be mutable in the builder, and copied to a new immutable collection in .build()
275279
private Map<String, String> systemProperties = new HashMap<>();;
276280

277281
private Builder() {

modules/openapi-generator-core/src/test/java/org/openapitools/codegen/config/WorkflowSettingsTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.testng.annotations.Test;
2020

2121
import java.nio.file.Paths;
22+
import java.util.Map;
2223

2324
import static org.testng.Assert.*;
2425

@@ -48,6 +49,25 @@ public void defaultValuesNotOverriddenByNulls(){
4849
assertTrue(settings.isStrictSpecBehavior());
4950
}
5051

52+
@Test
53+
public void newBuilderFromCopyShouldMutateSystemProperties(){
54+
WorkflowSettings original = WorkflowSettings.newBuilder()
55+
.withOutputDir("output")
56+
.withVerbose(true)
57+
.withSkipOverwrite(false)
58+
.withSystemProperty("first", "1st")
59+
.build();
60+
61+
WorkflowSettings modified = WorkflowSettings.newBuilder(original)
62+
.withSystemProperty("second", "2nd")
63+
.build();
64+
65+
Map<String, String> properties = modified.getSystemProperties();
66+
assertEquals(properties.size(), 2, "System Properties map should allow mutation when invoked via copy constructor");
67+
assertEquals(properties.getOrDefault("first", ""), "1st");
68+
assertEquals(properties.getOrDefault("second", ""), "2nd");
69+
}
70+
5171
private void assertOnChangesToDefaults(WorkflowSettings defaults) {
5272
WorkflowSettings settings = WorkflowSettings.newBuilder()
5373
.withOutputDir("output")

modules/openapi-generator-gradle-plugin/samples/local-spec/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ gradle openApiGenerate
1111
gradle openApiMeta
1212
gradle openApiValidate
1313
gradle buildGoSdk
14+
gradle buildDotnetSdk
1415
gradle generateGoWithInvalidSpec
1516
```
1617

modules/openapi-generator-gradle-plugin/samples/local-spec/build.gradle

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ task buildGoSdk(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTas
6565
]
6666
}
6767

68+
task buildDotnetSdk(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
69+
generatorName = "csharp-netcore"
70+
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
71+
additionalProperties = [
72+
packageGuid: "{321C8C3F-0156-40C1-AE42-D59761FB9B6C}",
73+
useCompareNetObjects: "true"
74+
]
75+
outputDir = "$buildDir/csharp-netcore".toString()
76+
systemProperties = [
77+
models: "",
78+
apis : "",
79+
]
80+
}
81+
6882
task generateGoWithInvalidSpec(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
6983
validateSpec = true
7084
generatorName = "go"

0 commit comments

Comments
 (0)