Skip to content

Commit 0dbc108

Browse files
authored
Add openapiGeneratorIgnoreList option to pre-populate .openapi-generator-ignore (#17164)
* add openapiGeneratorIgnoreList option to pre-populate .openapi-generator-ignore * minor fix * better code format * add tests
1 parent a93bab0 commit 0dbc108

File tree

13 files changed

+270
-25
lines changed

13 files changed

+270
-25
lines changed

docs/customization.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ Upon first code generation, you may also pass the CLI option `--ignore-file-over
266266

267267
Editor support for `.openapi-generator-ignore` files is available in IntelliJ via the [.ignore plugin](https://plugins.jetbrains.com/plugin/7495--ignore).
268268

269+
One may want to pre-populate `.openapi-generator-ignore` with a list of entries during the code generation process and the global/general option `openapiGeneatorIgnoreList` (e.g. --openapi-generator-ignore-list in CLI) can do exactly that. For example,
270+
```
271+
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g spring -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -o /tmp/spring --additional-properties useTags=true --openapi-generator-ignore-list "README.md,pom.xml,docs/*.md,src/main/java/org/openapitools/model/*"
272+
```
273+
269274
## Customizing the generator
270275

271276
There are different aspects of customizing the code generator beyond just creating or modifying templates. Each language has a supporting configuration file to handle different type mappings, etc:

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ public class ConfigHelp extends OpenApiGeneratorCommand {
101101
@Option(name = {"--language-specific-primitive"}, title = "language specific primitives", description = "displays the language specific primitives (types which require no additional imports, or which may conflict with user defined model names)")
102102
private Boolean languageSpecificPrimitives;
103103

104+
@Option(name = {"--openapi-generator-ignore-list"}, title = "openapi generator ignore list", description = "displays the openapi generator ignore list")
105+
private Boolean openapiGeneratorIgnoreList;
106+
104107
@Option(name = {"--reserved-words"}, title = "language specific reserved words", description = "displays the reserved words which may result in renamed model or property names")
105108
private Boolean reservedWords;
106109

@@ -588,6 +591,13 @@ private void generatePlainTextHelp(StringBuilder sb, CodegenConfig config) {
588591
sb.append(newline);
589592
}
590593

594+
if (Boolean.TRUE.equals(openapiGeneratorIgnoreList)) {
595+
sb.append(newline).append("OPENAPI GENERATOR IGNORE LIST").append(newline).append(newline);
596+
String[] arr = config.openapiGeneratorIgnoreList().stream().sorted().toArray(String[]::new);
597+
writePlainTextFromArray(sb, arr, optIndent);
598+
sb.append(newline);
599+
}
600+
591601
if (Boolean.TRUE.equals(reservedWords)) {
592602
sb.append(newline).append("RESERVED WORDS").append(newline).append(newline);
593603
String[] arr = config.reservedWords().stream().sorted().toArray(String[]::new);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ public class Generate extends OpenApiGeneratorCommand {
160160
+ " You can also have multiple occurrences of this option.")
161161
private List<String> languageSpecificPrimitives = new ArrayList<>();
162162

163+
@Option(
164+
name = {"--openapi-generator-ignore-list"},
165+
title = ".openapi-generaotr-ignore list",
166+
description = "specifies entries in the .openapi-generator-ignore file relative/path/to/file1,relative/path/to/file2. For example: README.md,pom.xml"
167+
+ " You can also have multiple occurrences of this option.")
168+
private List<String> openapiGeneratorIgnoreList = new ArrayList<>();
169+
163170
@Option(
164171
name = {"--import-mappings"},
165172
title = "import mappings",
@@ -504,6 +511,7 @@ public void execute() {
504511
applyTypeMappingsKvpList(typeMappings, configurator);
505512
applyAdditionalPropertiesKvpList(additionalProperties, configurator);
506513
applyLanguageSpecificPrimitivesCsvList(languageSpecificPrimitives, configurator);
514+
applyOpenAPIGeneratorIgnoreListCsvList(openapiGeneratorIgnoreList, configurator);
507515
applyReservedWordsMappingsKvpList(reservedWordsMappings, configurator);
508516
applyServerVariablesKvpList(serverVariableOverrides, configurator);
509517

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public final class GeneratorSettings implements Serializable {
5959
private final Map<String, String> enumNameMappings;
6060
private final Map<String, String> openapiNormalizer;
6161
private final Set<String> languageSpecificPrimitives;
62+
private final Set<String> openapiGeneratorIgnoreList;
6263
private final Map<String, String> reservedWordsMappings;
6364
private final Map<String, String> serverVariables;
6465

@@ -330,6 +331,15 @@ public Set<String> getLanguageSpecificPrimitives() {
330331
return languageSpecificPrimitives;
331332
}
332333

334+
/**
335+
* Gets openapi generator ignore list.
336+
*
337+
* @return the openapi generator ignore list
338+
*/
339+
public Set<String> getOpenAPIGeneratorIgnoreList() {
340+
return openapiGeneratorIgnoreList;
341+
}
342+
333343
/**
334344
* Gets reserved word mappings. Values defined here define how a reserved word should be escaped.
335345
* <p>
@@ -438,6 +448,7 @@ private GeneratorSettings(Builder builder) {
438448
enumNameMappings = Collections.unmodifiableMap(builder.enumNameMappings);
439449
openapiNormalizer = Collections.unmodifiableMap(builder.openapiNormalizer);
440450
languageSpecificPrimitives = Collections.unmodifiableSet(builder.languageSpecificPrimitives);
451+
openapiGeneratorIgnoreList = Collections.unmodifiableSet(builder.openapiGeneratorIgnoreList);
441452
reservedWordsMappings = Collections.unmodifiableMap(builder.reservedWordsMappings);
442453
serverVariables = Collections.unmodifiableMap(builder.serverVariables);
443454
gitHost = builder.gitHost;
@@ -516,6 +527,7 @@ public GeneratorSettings() {
516527
enumNameMappings = Collections.unmodifiableMap(new HashMap<>(0));
517528
openapiNormalizer = Collections.unmodifiableMap(new HashMap<>(0));
518529
languageSpecificPrimitives = Collections.unmodifiableSet(new HashSet<>(0));
530+
openapiGeneratorIgnoreList = Collections.unmodifiableSet(new HashSet<>(0));
519531
reservedWordsMappings = Collections.unmodifiableMap(new HashMap<>(0));
520532
serverVariables = Collections.unmodifiableMap(new HashMap<>(0));
521533
}
@@ -593,6 +605,9 @@ public static Builder newBuilder(GeneratorSettings copy) {
593605
if (copy.getLanguageSpecificPrimitives() != null) {
594606
builder.languageSpecificPrimitives.addAll(copy.getLanguageSpecificPrimitives());
595607
}
608+
if (copy.getOpenAPIGeneratorIgnoreList() != null) {
609+
builder.openapiGeneratorIgnoreList.addAll(copy.getOpenAPIGeneratorIgnoreList());
610+
}
596611
if (copy.getReservedWordsMappings() != null) {
597612
builder.reservedWordsMappings.putAll(copy.getReservedWordsMappings());
598613
}
@@ -638,6 +653,7 @@ public static final class Builder {
638653
private Map<String, String> enumNameMappings;
639654
private Map<String, String> openapiNormalizer;
640655
private Set<String> languageSpecificPrimitives;
656+
private Set<String> openapiGeneratorIgnoreList;
641657
private Map<String, String> reservedWordsMappings;
642658
private Map<String, String> serverVariables;
643659
private String gitHost;
@@ -663,6 +679,7 @@ public Builder() {
663679
enumNameMappings = new HashMap<>();
664680
openapiNormalizer = new HashMap<>();
665681
languageSpecificPrimitives = new HashSet<>();
682+
openapiGeneratorIgnoreList = new HashSet<>();
666683
reservedWordsMappings = new HashMap<>();
667684
serverVariables = new HashMap<>();
668685

@@ -1137,6 +1154,31 @@ public Builder withLanguageSpecificPrimitive(String value) {
11371154
return this;
11381155
}
11391156

1157+
/**
1158+
* Sets the {@code openapiGeneratorIgnoreList} and returns a reference to this Builder so that the methods can be chained together.
1159+
*
1160+
* @param openapiGeneratorIgnoreList the {@code openapiGeneratorIgnoreList} to set
1161+
* @return a reference to this Builder
1162+
*/
1163+
public Builder withOpenAPIGeneratorIgnoreList(Set<String> openapiGeneratorIgnoreList) {
1164+
this.openapiGeneratorIgnoreList = openapiGeneratorIgnoreList;
1165+
return this;
1166+
}
1167+
1168+
/**
1169+
* Sets a single {@code openapiGeneratorIgnoreList} and returns a reference to this Builder so that the methods can be chained together.
1170+
*
1171+
* @param value The value of entry to set
1172+
* @return a reference to this Builder
1173+
*/
1174+
public Builder withOpenAPIGeneratorIgnoreList(String value) {
1175+
if (this.openapiGeneratorIgnoreList == null) {
1176+
this.openapiGeneratorIgnoreList = new HashSet<>();
1177+
}
1178+
this.openapiGeneratorIgnoreList.add(value);
1179+
return this;
1180+
}
1181+
11401182
/**
11411183
* Sets the {@code reservedWordsMappings} and returns a reference to this Builder so that the methods can be chained together.
11421184
*
@@ -1266,6 +1308,7 @@ public String toString() {
12661308
", additionalProperties=" + additionalProperties +
12671309
", importMappings=" + importMappings +
12681310
", languageSpecificPrimitives=" + languageSpecificPrimitives +
1311+
", openapiGeneratorIgnoreList=" + openapiGeneratorIgnoreList +
12691312
", reservedWordsMappings=" + reservedWordsMappings +
12701313
", gitHost='" + gitHost + '\'' +
12711314
", gitUserId='" + gitUserId + '\'' +
@@ -1305,6 +1348,7 @@ public boolean equals(Object o) {
13051348
Objects.equals(getEnumNameMappings(), that.getEnumNameMappings()) &&
13061349
Objects.equals(getOpenAPINormalizer(), that.getOpenAPINormalizer()) &&
13071350
Objects.equals(getLanguageSpecificPrimitives(), that.getLanguageSpecificPrimitives()) &&
1351+
Objects.equals(getOpenAPIGeneratorIgnoreList(), that.getOpenAPIGeneratorIgnoreList()) &&
13081352
Objects.equals(getReservedWordsMappings(), that.getReservedWordsMappings()) &&
13091353
Objects.equals(getGitHost(), that.getGitHost()) &&
13101354
Objects.equals(getGitUserId(), that.getGitUserId()) &&
@@ -1341,6 +1385,7 @@ public int hashCode() {
13411385
getEnumNameMappings(),
13421386
getOpenAPINormalizer(),
13431387
getLanguageSpecificPrimitives(),
1388+
getOpenAPIGeneratorIgnoreList(),
13441389
getReservedWordsMappings(),
13451390
getGitHost(),
13461391
getGitUserId(),

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/extensions/OpenApiGeneratorGenerateExtension.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
147147
*/
148148
val languageSpecificPrimitives = project.objects.listProperty<String>()
149149

150+
/**
151+
* Specifies .openapi-generator-ignore list in the form of relative/path/to/file1,relative/path/to/file2. For example: README.md,pom.xml.
152+
*/
153+
val openapiGeneratorIgnoreList = project.objects.listProperty<String>()
154+
150155
/**
151156
* Specifies mappings between a given class and the import that should be used for that class.
152157
*/

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ open class GenerateTask @Inject constructor(private val objectFactory: ObjectFac
244244
@Input
245245
val languageSpecificPrimitives = project.objects.listProperty<String>()
246246

247+
/**
248+
* Specifies .openapi-generator-ignore list in the form of relative/path/to/file1,relative/path/to/file2. For example: README.md,pom.xml.
249+
*/
250+
@Optional
251+
@Input
252+
val openapiGeneratorIgnoreList = project.objects.listProperty<String>()
253+
247254
/**
248255
* Specifies mappings between a given class and the import that should be used for that class.
249256
*/
@@ -895,6 +902,12 @@ open class GenerateTask @Inject constructor(private val objectFactory: ObjectFac
895902
}
896903
}
897904

905+
if (openapiGeneratorIgnoreList.isPresent) {
906+
openapiGeneratorIgnoreList.get().forEach {
907+
configurator.addOpenAPIGeneratorIgnoreList(it)
908+
}
909+
}
910+
898911
if (reservedWordsMappings.isPresent) {
899912
reservedWordsMappings.get().forEach { entry ->
900913
configurator.addAdditionalReservedWordMapping(entry.key, entry.value)

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,12 @@ public class CodeGenMojo extends AbstractMojo {
374374
@Parameter(name = "languageSpecificPrimitives", property = "openapi.generator.maven.plugin.languageSpecificPrimitives")
375375
private List<String> languageSpecificPrimitives;
376376

377+
/**
378+
* A list of openapi-generator-ignore entries
379+
*/
380+
@Parameter(name = "openapiGeneratorIgnoreList", property = "openapi.generator.maven.plugin.openapiGeneratorIgnoreList")
381+
private List<String> openapiGeneratorIgnoreList;
382+
377383
/**
378384
* A map of additional properties that can be referenced by the mustache templates
379385
*/
@@ -783,6 +789,12 @@ public void execute() throws MojoExecutionException {
783789
.get("language-specific-primitives").toString(), configurator);
784790
}
785791

792+
// Retained for backwards-compatibility with configOptions -> openapi-generator-ignore-list
793+
if (openapiGeneratorIgnoreList == null && configOptions.containsKey("openapi-generator-ignore-list")) {
794+
applyOpenAPIGeneratorIgnoreListCsv(configOptions
795+
.get("openapi-generator-ignore-list").toString(), configurator);
796+
}
797+
786798
// Retained for backwards-compatibility with configOptions -> additional-properties
787799
if (additionalProperties == null && configOptions.containsKey("additional-properties")) {
788800
applyAdditionalPropertiesKvp(configOptions.get("additional-properties").toString(),
@@ -861,6 +873,12 @@ public void execute() throws MojoExecutionException {
861873
applyLanguageSpecificPrimitivesCsvList(languageSpecificPrimitives, configurator);
862874
}
863875

876+
// Apply Language Specific Primitives
877+
if (openapiGeneratorIgnoreList != null
878+
&& (configOptions == null || !configOptions.containsKey("openapi-generator-ignore-list"))) {
879+
applyOpenAPIGeneratorIgnoreListCsvList(openapiGeneratorIgnoreList, configurator);
880+
}
881+
864882
// Apply Additional Properties
865883
if (additionalProperties != null && (configOptions == null || !configOptions.containsKey("additional-properties"))) {
866884
applyAdditionalPropertiesKvpList(additionalProperties, configurator);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ public interface CodegenConfig {
173173

174174
Set<String> languageSpecificPrimitives();
175175

176+
Set<String> openapiGeneratorIgnoreList();
177+
176178
Map<String, String> reservedWordsMappings();
177179

178180
void preprocessOpenAPI(OpenAPI openAPI);
@@ -349,4 +351,6 @@ public interface CodegenConfig {
349351

350352
boolean getUseOpenAPINormalizer();
351353

354+
Set<String> getOpenAPIGeneratorIgnoreList();
355+
352356
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ public class DefaultCodegen implements CodegenConfig {
160160
protected Map<String, String> instantiationTypes;
161161
protected Set<String> reservedWords;
162162
protected Set<String> languageSpecificPrimitives = new HashSet<>();
163+
protected Set<String> openapiGeneratorIgnoreList = new HashSet<>();
163164
protected Map<String, String> importMapping = new HashMap<>();
164165
// a map to store the mapping between a schema and the new one
165166
protected Map<String, String> schemaMapping = new HashMap<>();
@@ -1211,6 +1212,11 @@ public Set<String> languageSpecificPrimitives() {
12111212
return languageSpecificPrimitives;
12121213
}
12131214

1215+
@Override
1216+
public Set<String> openapiGeneratorIgnoreList() {
1217+
return openapiGeneratorIgnoreList;
1218+
}
1219+
12141220
@Override
12151221
public Map<String, String> importMapping() {
12161222
return importMapping;
@@ -8317,6 +8323,11 @@ public List<VendorExtension> getSupportedVendorExtensions() {
83178323
@Override
83188324
public boolean getUseOpenAPINormalizer() { return true; }
83198325

8326+
@Override
8327+
public Set<String> getOpenAPIGeneratorIgnoreList() {
8328+
return openapiGeneratorIgnoreList;
8329+
}
8330+
83208331
/*
83218332
A function to convert yaml or json ingested strings like property names
83228333
And convert special characters like newline, tab, carriage return

0 commit comments

Comments
 (0)