Skip to content

Commit 2957dd4

Browse files
authored
[cli] Add --global-property for -D replacement (#5687)
-D option has been deprecated as it was previously used to: * Pass "system properties" * Pass additional properties This was confusing because we already have --additional-properties and because Java System Properties are passed as -D before program arguments. Confusion around the -D option had existed for some time, but when we introduced the thread-safe GlobalSettings to avoid overwriting Java System Properties, we created a hard break from Java System Properties in the generator. This also disconnected the previous "system properties" from accepting additional properties. Once these newly deprecated methods are removed, we will have a clear separation of concerns between: * Java System Properties * Global generator properties (used as workflow context) * Additional properties (used as generator options) This commit marks multiple places for cleanup in 5.0. These will be breaking changes, and lower effort to break in 5.0 with deprecation warnings now rather than adding sibling properties throughout the code and potentially introducing logic errors.
1 parent e14e5fc commit 2957dd4

6 files changed

Lines changed: 58 additions & 12 deletions

File tree

modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Generate.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ public class Generate extends OpenApiGeneratorCommand {
7171
+ "Pass in a URL-encoded string of name:header with a comma separating multiple values")
7272
private String auth;
7373

74+
// TODO: Remove -D short option in 5.0
7475
@Option(
75-
name = {"-D"},
76-
title = "system properties",
77-
description = "sets specified system properties in "
76+
name = {"-D", "--global-property"},
77+
title = "global properties",
78+
description = "sets specified global properties (previously called 'system properties') in "
7879
+ "the format of name=value,name=value (or multiple options, each with name=value)")
79-
private List<String> systemProperties = new ArrayList<>();
80+
private List<String> globalProperties = new ArrayList<>();
8081

8182
@Option(
8283
name = {"-c", "--config"},
@@ -403,9 +404,9 @@ public void execute() {
403404
configurator.setStrictSpecBehavior(strictSpecBehavior);
404405
}
405406

406-
if (systemProperties != null && !systemProperties.isEmpty()) {
407-
System.err.println("[DEPRECATED] -D arguments after 'generate' are application arguments and not Java System Properties, please consider changing to -p, or apply your options to JAVA_OPTS, or move the -D arguments before the jar option.");
408-
applySystemPropertiesKvpList(systemProperties, configurator);
407+
if (globalProperties != null && !globalProperties.isEmpty()) {
408+
System.err.println("[DEPRECATED] -D arguments after 'generate' are application arguments and not Java System Properties, please consider changing to --global-property, apply your system properties to JAVA_OPTS, or move the -D arguments before the jar option.");
409+
applyGlobalPropertiesKvpList(globalProperties, configurator);
409410
}
410411
applyInstantiationTypesKvpList(instantiationTypes, configurator);
411412
applyImportMappingsKvpList(importMappings, configurator);

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828
import java.util.HashMap;
2929
import java.util.Map;
3030
import java.util.Objects;
31+
import java.util.concurrent.atomic.AtomicLong;
3132

3233
/**
3334
* Represents those settings applied to a generation workflow.
3435
*/
3536
@SuppressWarnings("WeakerAccess")
3637
public class WorkflowSettings {
37-
38+
private static final AtomicLong lastWarning = new AtomicLong(0);
3839
private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowSettings.class);
3940
public static final String DEFAULT_OUTPUT_DIR = ".";
4041
public static final boolean DEFAULT_VERBOSE = false;
@@ -77,7 +78,15 @@ private WorkflowSettings(Builder builder) {
7778
this.templateDir = builder.templateDir;
7879
this.templatingEngineName = builder.templatingEngineName;
7980
this.ignoreFileOverride = builder.ignoreFileOverride;
81+
// TODO: rename to globalProperties for 5.0
8082
this.systemProperties = ImmutableMap.copyOf(builder.systemProperties);
83+
if (this.systemProperties.size() > 0) {
84+
// write no more than every 5s. This is temporary until version 5.0 as once(Logger) is not accessible here.
85+
// thread contention may cause this to write more than once, but this is just an attempt to reduce noise
86+
if (System.currentTimeMillis() - lastWarning.getAndUpdate(x -> System.currentTimeMillis()) > 5000) {
87+
LOGGER.warn("systemProperties will be renamed to globalProperties in version 5.0");
88+
}
89+
}
8190
}
8291

8392
/**

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ open class GenerateTask : DefaultTask() {
574574
}
575575

576576
if (systemProperties.isPresent) {
577+
// TODO: rename to globalProperties in 5.0
577578
systemProperties.get().forEach { entry ->
578579
configurator.addSystemProperty(entry.key, entry.value)
579580
}

modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ public class CodeGenMojo extends AbstractMojo {
408408
@Parameter(defaultValue = "true", property = "openapi.generator.maven.plugin.addCompileSourceRoot")
409409
private boolean addCompileSourceRoot = true;
410410

411+
// TODO: Rename to global properties in version 5.0
411412
@Parameter
412413
protected Map<String, String> environmentVariables = new HashMap<>();
413414

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ public CodegenConfigurator addLanguageSpecificPrimitive(String value) {
178178
return this;
179179
}
180180

181+
// TODO: rename this and other references to "global property" rather than "system property"
181182
public CodegenConfigurator addSystemProperty(String key, String value) {
182183
this.systemProperties.put(key, value);
183184
workflowSettingsBuilder.withSystemProperty(key, value);

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,47 @@
4242
*/
4343
public final class CodegenConfiguratorUtils {
4444

45+
/**
46+
* Applies "system" properties to the configurator as global properties.
47+
*
48+
* @deprecated
49+
* This method is deprecated due to confusion around the tool's use of system properties. We called these system properties
50+
* in the past and accepted them via CLI option -D. This lead to confusion between true Java System Properties and generator-specific
51+
* "system level properties". They've since been renamed as "Global Properties". Please use {@link CodegenConfiguratorUtils#applyGlobalPropertiesKvpList(List, CodegenConfigurator)}.
52+
*
53+
* @param systemProperties List of properties to be globally available throughout the generator execution.
54+
* @param configurator The {@link CodegenConfigurator} instance to configure.
55+
*/
56+
@Deprecated
4557
public static void applySystemPropertiesKvpList(List<String> systemProperties, CodegenConfigurator configurator) {
46-
for(String propString : systemProperties) {
47-
applySystemPropertiesKvp(propString, configurator);
58+
// TODO: Remove in 5.0
59+
applyGlobalPropertiesKvpList(systemProperties, configurator);
60+
}
61+
62+
/**
63+
* Applies a key-value pair of strings as "system" properties to the configurator as global properties.
64+
*
65+
* @deprecated
66+
* This method is deprecated due to confusing between Java Sytsem Properties and generator-specific "system-level properties".
67+
* They've since been renamed as "Global Properties". Please use {@link CodegenConfiguratorUtils#applyGlobalPropertiesKvp(String, CodegenConfigurator)}.
68+
*
69+
* @param systemProperties List of properties to be globally available throughout the generator execution.
70+
* @param configurator The {@link CodegenConfigurator} instance to configure.
71+
*/
72+
@Deprecated
73+
public static void applySystemPropertiesKvp(String systemProperties, CodegenConfigurator configurator) {
74+
// TODO: Remove in 5.0
75+
applyGlobalPropertiesKvp(systemProperties, configurator);
76+
}
77+
78+
public static void applyGlobalPropertiesKvpList(List<String> globalProperties, CodegenConfigurator configurator) {
79+
for(String propString : globalProperties) {
80+
applyGlobalPropertiesKvp(propString, configurator);
4881
}
4982
}
5083

51-
public static void applySystemPropertiesKvp(String systemProperties, CodegenConfigurator configurator) {
52-
final Map<String, String> map = createMapFromKeyValuePairs(systemProperties);
84+
public static void applyGlobalPropertiesKvp(String globalProperties, CodegenConfigurator configurator) {
85+
final Map<String, String> map = createMapFromKeyValuePairs(globalProperties);
5386
for (Map.Entry<String, String> entry : map.entrySet()) {
5487
configurator.addSystemProperty(entry.getKey(), entry.getValue());
5588
}

0 commit comments

Comments
 (0)