Skip to content

Commit ee7c8a8

Browse files
authored
[BUG][CLI][GENERATOR] NullPointer when not setting outputDir (updated) (#3752)
* Fixes NPE when no outputDir is set * Fix behaviors of default values for values not provided by user * Easier handling of default behavior in settings. * Fixes for dynamic config deserialization (specifically, ruby client sample fix) * Tests for WorkflowSettings (defaults, modified defaults, nulls) * Test modification of WorkflowSettings defaults for both class constructor and builder
1 parent 136c140 commit ee7c8a8

76 files changed

Lines changed: 583 additions & 230 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bin/ruby-petstore-faraday.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"gemName": "petstore",
33
"moduleName": "Petstore",
44
"library": "faraday",
5-
"gemVersion": "1.0.0"
5+
"gemVersion": "1.0.0",
6+
"strictSpecBehavior": false
67
}

bin/ruby-petstore.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"gemName": "petstore",
33
"library": "typhoeus",
44
"moduleName": "Petstore",
5-
"gemVersion": "1.0.0"
5+
"gemVersion": "1.0.0",
6+
"strictSpecBehavior": false
67
}

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,27 @@ public static Builder newBuilder(GeneratorSettings copy) {
421421
builder.artifactId = copy.getArtifactId();
422422
builder.artifactVersion = copy.getArtifactVersion();
423423
builder.library = copy.getLibrary();
424-
builder.instantiationTypes = new HashMap<>(copy.getInstantiationTypes());
425-
builder.typeMappings = new HashMap<>(copy.getTypeMappings());
426-
builder.additionalProperties = new HashMap<>(copy.getAdditionalProperties());
427-
builder.importMappings = new HashMap<>(copy.getImportMappings());
428-
builder.languageSpecificPrimitives = new HashSet<>(copy.getLanguageSpecificPrimitives());
429-
builder.reservedWordMappings = new HashMap<>(copy.getReservedWordMappings());
430-
builder.serverVariables = new HashMap<>(copy.getServerVariables());
424+
if (copy.getInstantiationTypes() != null) {
425+
builder.instantiationTypes.putAll(copy.getInstantiationTypes());
426+
}
427+
if (copy.getTypeMappings() != null) {
428+
builder.typeMappings.putAll(copy.getTypeMappings());
429+
}
430+
if (copy.getAdditionalProperties() != null) {
431+
builder.additionalProperties.putAll(copy.getAdditionalProperties());
432+
}
433+
if (copy.getImportMappings() != null) {
434+
builder.importMappings.putAll(copy.getImportMappings());
435+
}
436+
if (copy.getLanguageSpecificPrimitives() != null) {
437+
builder.languageSpecificPrimitives.addAll(copy.getLanguageSpecificPrimitives());
438+
}
439+
if (copy.getReservedWordMappings() != null) {
440+
builder.reservedWordMappings.putAll(copy.getReservedWordMappings());
441+
}
442+
if (copy.getServerVariables() != null) {
443+
builder.serverVariables.putAll(copy.getServerVariables());
444+
}
431445
builder.gitUserId = copy.getGitUserId();
432446
builder.gitRepoId = copy.getGitRepoId();
433447
builder.releaseNote = copy.getReleaseNote();

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

Lines changed: 78 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -33,61 +33,64 @@
3333
public class WorkflowSettings {
3434

3535
private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowSettings.class);
36+
public static final String DEFAULT_OUTPUT_DIR = ".";
37+
public static final boolean DEFAULT_VERBOSE = false;
38+
public static final boolean DEFAULT_SKIP_OVERWRITE = false;
39+
public static final boolean DEFAULT_REMOVE_OPERATION_ID_PREFIX = false;
40+
public static final boolean DEFAULT_LOG_TO_STDERR = false;
41+
public static final boolean DEFAULT_VALIDATE_SPEC = true;
42+
public static final boolean DEFAULT_ENABLE_POST_PROCESS_FILE = false;
43+
public static final boolean DEFAULT_ENABLE_MINIMAL_UPDATE = false;
44+
public static final boolean DEFAULT_STRICT_SPEC_BEHAVIOR = true;
45+
public static final String DEFAULT_TEMPLATING_ENGINE_NAME = "mustache";
46+
public static final ImmutableMap<String, String> DEFAULT_SYSTEM_PROPERTIES = ImmutableMap.of();
3647

3748
private String inputSpec;
38-
private String outputDir;
39-
private boolean verbose;
40-
private boolean skipOverwrite;
41-
private boolean removeOperationIdPrefix;
42-
private boolean logToStderr;
43-
private boolean validateSpec;
44-
private boolean enablePostProcessFile;
45-
private boolean enableMinimalUpdate;
46-
private boolean strictSpecBehavior;
49+
private String outputDir = DEFAULT_OUTPUT_DIR;
50+
private boolean verbose = DEFAULT_VERBOSE;
51+
private boolean skipOverwrite = DEFAULT_SKIP_OVERWRITE;
52+
private boolean removeOperationIdPrefix = DEFAULT_REMOVE_OPERATION_ID_PREFIX;
53+
private boolean logToStderr = DEFAULT_LOG_TO_STDERR;
54+
private boolean validateSpec = DEFAULT_VALIDATE_SPEC;
55+
private boolean enablePostProcessFile = DEFAULT_ENABLE_POST_PROCESS_FILE;
56+
private boolean enableMinimalUpdate = DEFAULT_ENABLE_MINIMAL_UPDATE;
57+
private boolean strictSpecBehavior = DEFAULT_STRICT_SPEC_BEHAVIOR;
4758
private String templateDir;
48-
private String templatingEngineName;
59+
private String templatingEngineName = DEFAULT_TEMPLATING_ENGINE_NAME;
4960
private String ignoreFileOverride;
50-
private ImmutableMap<String, String> systemProperties;
61+
private ImmutableMap<String, String> systemProperties = DEFAULT_SYSTEM_PROPERTIES;
5162

5263
private WorkflowSettings(Builder builder) {
53-
setDefaults();
54-
inputSpec = builder.inputSpec;
55-
outputDir = builder.outputDir;
56-
verbose = builder.verbose;
57-
skipOverwrite = builder.skipOverwrite;
58-
removeOperationIdPrefix = builder.removeOperationIdPrefix;
59-
logToStderr = builder.logToStderr;
60-
validateSpec = builder.validateSpec;
61-
enablePostProcessFile = builder.enablePostProcessFile;
62-
enableMinimalUpdate = builder.enableMinimalUpdate;
63-
strictSpecBehavior = builder.strictSpecBehavior;
64-
templateDir = builder.templateDir;
65-
templatingEngineName = builder.templatingEngineName;
66-
ignoreFileOverride = builder.ignoreFileOverride;
67-
systemProperties = ImmutableMap.copyOf(builder.systemProperties);
64+
this.inputSpec = builder.inputSpec;
65+
this.outputDir = builder.outputDir;
66+
this.verbose = builder.verbose;
67+
this.skipOverwrite = builder.skipOverwrite;
68+
this.removeOperationIdPrefix = builder.removeOperationIdPrefix;
69+
this.logToStderr = builder.logToStderr;
70+
this.validateSpec = builder.validateSpec;
71+
this.enablePostProcessFile = builder.enablePostProcessFile;
72+
this.enableMinimalUpdate = builder.enableMinimalUpdate;
73+
this.strictSpecBehavior = builder.strictSpecBehavior;
74+
this.templateDir = builder.templateDir;
75+
this.templatingEngineName = builder.templatingEngineName;
76+
this.ignoreFileOverride = builder.ignoreFileOverride;
77+
this.systemProperties = ImmutableMap.copyOf(builder.systemProperties);
6878
}
6979

7080
/**
7181
* Instantiates a new workflow settings.
7282
*/
7383
@SuppressWarnings("unused")
7484
public WorkflowSettings() {
75-
setDefaults();
76-
systemProperties = ImmutableMap.of();
77-
}
7885

79-
private void setDefaults(){
80-
validateSpec = true;
81-
strictSpecBehavior = true;
82-
outputDir = ".";
8386
}
8487

8588
public static Builder newBuilder() {
8689
return new Builder();
8790
}
8891

8992
public static Builder newBuilder(WorkflowSettings copy) {
90-
Builder builder = new Builder();
93+
Builder builder = newBuilder();
9194
builder.inputSpec = copy.getInputSpec();
9295
builder.outputDir = copy.getOutputDir();
9396
builder.verbose = copy.isVerbose();
@@ -257,32 +260,34 @@ public Map<String, String> getSystemProperties() {
257260
@SuppressWarnings("unused")
258261
public static final class Builder {
259262
private String inputSpec;
260-
private String outputDir;
261-
private boolean verbose;
262-
private boolean skipOverwrite;
263-
private boolean removeOperationIdPrefix;
264-
private boolean logToStderr;
265-
private boolean validateSpec;
266-
private boolean enablePostProcessFile;
267-
private boolean enableMinimalUpdate;
268-
private boolean strictSpecBehavior;
263+
private String outputDir = DEFAULT_OUTPUT_DIR;
264+
private Boolean verbose = DEFAULT_VERBOSE;
265+
private Boolean skipOverwrite = DEFAULT_SKIP_OVERWRITE;
266+
private Boolean removeOperationIdPrefix = DEFAULT_REMOVE_OPERATION_ID_PREFIX;
267+
private Boolean logToStderr = DEFAULT_LOG_TO_STDERR;
268+
private Boolean validateSpec = DEFAULT_VALIDATE_SPEC;
269+
private Boolean enablePostProcessFile = DEFAULT_ENABLE_POST_PROCESS_FILE;
270+
private Boolean enableMinimalUpdate = DEFAULT_ENABLE_MINIMAL_UPDATE;
271+
private Boolean strictSpecBehavior = DEFAULT_STRICT_SPEC_BEHAVIOR;
269272
private String templateDir;
270-
private String templatingEngineName;
273+
private String templatingEngineName = DEFAULT_TEMPLATING_ENGINE_NAME;
271274
private String ignoreFileOverride;
272-
private Map<String, String> systemProperties;
275+
private Map<String, String> systemProperties = new HashMap<>();;
273276

274277
private Builder() {
275-
systemProperties = new HashMap<>();
276278
}
277279

280+
278281
/**
279282
* Sets the {@code inputSpec} and returns a reference to this Builder so that the methods can be chained together.
280283
*
281284
* @param inputSpec the {@code inputSpec} to set
282285
* @return a reference to this Builder
283286
*/
284287
public Builder withInputSpec(String inputSpec) {
285-
this.inputSpec = inputSpec;
288+
if (inputSpec != null) {
289+
this.inputSpec = inputSpec;
290+
}
286291
return this;
287292
}
288293

@@ -293,7 +298,11 @@ public Builder withInputSpec(String inputSpec) {
293298
* @return a reference to this Builder
294299
*/
295300
public Builder withOutputDir(String outputDir) {
296-
this.outputDir = Paths.get(outputDir).toAbsolutePath().toString();;
301+
if (outputDir != null ) {
302+
this.outputDir = Paths.get(outputDir).toAbsolutePath().toString();
303+
} else {
304+
this.outputDir = DEFAULT_OUTPUT_DIR;
305+
}
297306
return this;
298307
}
299308

@@ -303,8 +312,8 @@ public Builder withOutputDir(String outputDir) {
303312
* @param verbose the {@code verbose} to set
304313
* @return a reference to this Builder
305314
*/
306-
public Builder withVerbose(boolean verbose) {
307-
this.verbose = verbose;
315+
public Builder withVerbose(Boolean verbose) {
316+
this.verbose = verbose != null ? verbose : Boolean.valueOf(DEFAULT_VERBOSE);
308317
return this;
309318
}
310319

@@ -314,8 +323,8 @@ public Builder withVerbose(boolean verbose) {
314323
* @param skipOverwrite the {@code skipOverwrite} to set
315324
* @return a reference to this Builder
316325
*/
317-
public Builder withSkipOverwrite(boolean skipOverwrite) {
318-
this.skipOverwrite = skipOverwrite;
326+
public Builder withSkipOverwrite(Boolean skipOverwrite) {
327+
this.skipOverwrite = skipOverwrite != null ? skipOverwrite : Boolean.valueOf(DEFAULT_SKIP_OVERWRITE);
319328
return this;
320329
}
321330

@@ -325,8 +334,8 @@ public Builder withSkipOverwrite(boolean skipOverwrite) {
325334
* @param removeOperationIdPrefix the {@code removeOperationIdPrefix} to set
326335
* @return a reference to this Builder
327336
*/
328-
public Builder withRemoveOperationIdPrefix(boolean removeOperationIdPrefix) {
329-
this.removeOperationIdPrefix = removeOperationIdPrefix;
337+
public Builder withRemoveOperationIdPrefix(Boolean removeOperationIdPrefix) {
338+
this.removeOperationIdPrefix = removeOperationIdPrefix != null ? removeOperationIdPrefix : Boolean.valueOf(DEFAULT_REMOVE_OPERATION_ID_PREFIX);
330339
return this;
331340
}
332341

@@ -336,8 +345,8 @@ public Builder withRemoveOperationIdPrefix(boolean removeOperationIdPrefix) {
336345
* @param logToStderr the {@code logToStderr} to set
337346
* @return a reference to this Builder
338347
*/
339-
public Builder withLogToStderr(boolean logToStderr) {
340-
this.logToStderr = logToStderr;
348+
public Builder withLogToStderr(Boolean logToStderr) {
349+
this.logToStderr = logToStderr != null ? logToStderr : Boolean.valueOf(DEFAULT_LOG_TO_STDERR);
341350
return this;
342351
}
343352

@@ -347,8 +356,8 @@ public Builder withLogToStderr(boolean logToStderr) {
347356
* @param validateSpec the {@code validateSpec} to set
348357
* @return a reference to this Builder
349358
*/
350-
public Builder withValidateSpec(boolean validateSpec) {
351-
this.validateSpec = validateSpec;
359+
public Builder withValidateSpec(Boolean validateSpec) {
360+
this.validateSpec = validateSpec != null ? validateSpec : Boolean.valueOf(DEFAULT_VALIDATE_SPEC);
352361
return this;
353362
}
354363

@@ -358,8 +367,8 @@ public Builder withValidateSpec(boolean validateSpec) {
358367
* @param enablePostProcessFile the {@code enablePostProcessFile} to set
359368
* @return a reference to this Builder
360369
*/
361-
public Builder withEnablePostProcessFile(boolean enablePostProcessFile) {
362-
this.enablePostProcessFile = enablePostProcessFile;
370+
public Builder withEnablePostProcessFile(Boolean enablePostProcessFile) {
371+
this.enablePostProcessFile = enablePostProcessFile != null ? enablePostProcessFile : Boolean.valueOf(DEFAULT_ENABLE_POST_PROCESS_FILE);
363372
return this;
364373
}
365374

@@ -369,8 +378,8 @@ public Builder withEnablePostProcessFile(boolean enablePostProcessFile) {
369378
* @param enableMinimalUpdate the {@code enableMinimalUpdate} to set
370379
* @return a reference to this Builder
371380
*/
372-
public Builder withEnableMinimalUpdate(boolean enableMinimalUpdate) {
373-
this.enableMinimalUpdate = enableMinimalUpdate;
381+
public Builder withEnableMinimalUpdate(Boolean enableMinimalUpdate) {
382+
this.enableMinimalUpdate = enableMinimalUpdate != null ? enableMinimalUpdate : Boolean.valueOf(DEFAULT_ENABLE_MINIMAL_UPDATE);
374383
return this;
375384
}
376385

@@ -380,8 +389,8 @@ public Builder withEnableMinimalUpdate(boolean enableMinimalUpdate) {
380389
* @param strictSpecBehavior the {@code strictSpecBehavior} to set
381390
* @return a reference to this Builder
382391
*/
383-
public Builder withStrictSpecBehavior(boolean strictSpecBehavior) {
384-
this.strictSpecBehavior = strictSpecBehavior;
392+
public Builder withStrictSpecBehavior(Boolean strictSpecBehavior) {
393+
this.strictSpecBehavior = strictSpecBehavior != null ? strictSpecBehavior : Boolean.valueOf(DEFAULT_STRICT_SPEC_BEHAVIOR);
385394
return this;
386395
}
387396

@@ -392,9 +401,7 @@ public Builder withStrictSpecBehavior(boolean strictSpecBehavior) {
392401
* @return a reference to this Builder
393402
*/
394403
public Builder withTemplateDir(String templateDir) {
395-
if (templateDir == null) {
396-
this.templateDir = null;
397-
} else {
404+
if (templateDir != null) {
398405
File f = new File(templateDir);
399406

400407
// check to see if the folder exists
@@ -416,7 +423,7 @@ public Builder withTemplateDir(String templateDir) {
416423
* @return a reference to this Builder
417424
*/
418425
public Builder withTemplatingEngineName(String templatingEngineName) {
419-
this.templatingEngineName = templatingEngineName;
426+
this.templatingEngineName = templatingEngineName != null ? templatingEngineName : DEFAULT_TEMPLATING_ENGINE_NAME;
420427
return this;
421428
}
422429

@@ -438,7 +445,9 @@ public Builder withIgnoreFileOverride(String ignoreFileOverride) {
438445
* @return a reference to this Builder
439446
*/
440447
public Builder withSystemProperties(Map<String, String> systemProperties) {
441-
this.systemProperties = systemProperties;
448+
if (systemProperties != null) {
449+
this.systemProperties = systemProperties;
450+
}
442451
return this;
443452
}
444453

0 commit comments

Comments
 (0)