Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add openapiGeneratorIgnoreList option to pre-populate .openapi-generator-ignore #17164

Merged
merged 4 commits into from
Nov 23, 2023
Merged
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
5 changes: 5 additions & 0 deletions docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ Upon first code generation, you may also pass the CLI option `--ignore-file-over

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

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,
```
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/*"
```

## Customizing the generator

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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public class ConfigHelp extends OpenApiGeneratorCommand {
@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)")
private Boolean languageSpecificPrimitives;

@Option(name = {"--openapi-generator-ignore-list"}, title = "openapi generator ignore list", description = "displays the openapi generator ignore list")
private Boolean openapiGeneratorIgnoreList;

@Option(name = {"--reserved-words"}, title = "language specific reserved words", description = "displays the reserved words which may result in renamed model or property names")
private Boolean reservedWords;

Expand Down Expand Up @@ -588,6 +591,13 @@ private void generatePlainTextHelp(StringBuilder sb, CodegenConfig config) {
sb.append(newline);
}

if (Boolean.TRUE.equals(openapiGeneratorIgnoreList)) {
sb.append(newline).append("OPENAPI GENERATOR IGNORE LIST").append(newline).append(newline);
String[] arr = config.openapiGeneratorIgnoreList().stream().sorted().toArray(String[]::new);
writePlainTextFromArray(sb, arr, optIndent);
sb.append(newline);
}

if (Boolean.TRUE.equals(reservedWords)) {
sb.append(newline).append("RESERVED WORDS").append(newline).append(newline);
String[] arr = config.reservedWords().stream().sorted().toArray(String[]::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ public class Generate extends OpenApiGeneratorCommand {
+ " You can also have multiple occurrences of this option.")
private List<String> languageSpecificPrimitives = new ArrayList<>();

@Option(
name = {"--openapi-generator-ignore-list"},
title = ".openapi-generaotr-ignore list",
description = "specifies entries in the .openapi-generator-ignore file relative/path/to/file1,relative/path/to/file2. For example: README.md,pom.xml"
+ " You can also have multiple occurrences of this option.")
private List<String> openapiGeneratorIgnoreList = new ArrayList<>();

@Option(
name = {"--import-mappings"},
title = "import mappings",
Expand Down Expand Up @@ -504,6 +511,7 @@ public void execute() {
applyTypeMappingsKvpList(typeMappings, configurator);
applyAdditionalPropertiesKvpList(additionalProperties, configurator);
applyLanguageSpecificPrimitivesCsvList(languageSpecificPrimitives, configurator);
applyOpenAPIGeneratorIgnoreListCsvList(openapiGeneratorIgnoreList, configurator);
applyReservedWordsMappingsKvpList(reservedWordsMappings, configurator);
applyServerVariablesKvpList(serverVariableOverrides, configurator);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public final class GeneratorSettings implements Serializable {
private final Map<String, String> enumNameMappings;
private final Map<String, String> openapiNormalizer;
private final Set<String> languageSpecificPrimitives;
private final Set<String> openapiGeneratorIgnoreList;
private final Map<String, String> reservedWordsMappings;
private final Map<String, String> serverVariables;

Expand Down Expand Up @@ -330,6 +331,15 @@ public Set<String> getLanguageSpecificPrimitives() {
return languageSpecificPrimitives;
}

/**
* Gets openapi generator ignore list.
*
* @return the openapi generator ignore list
*/
public Set<String> getOpenAPIGeneratorIgnoreList() {
return openapiGeneratorIgnoreList;
}

/**
* Gets reserved word mappings. Values defined here define how a reserved word should be escaped.
* <p>
Expand Down Expand Up @@ -438,6 +448,7 @@ private GeneratorSettings(Builder builder) {
enumNameMappings = Collections.unmodifiableMap(builder.enumNameMappings);
openapiNormalizer = Collections.unmodifiableMap(builder.openapiNormalizer);
languageSpecificPrimitives = Collections.unmodifiableSet(builder.languageSpecificPrimitives);
openapiGeneratorIgnoreList = Collections.unmodifiableSet(builder.openapiGeneratorIgnoreList);
reservedWordsMappings = Collections.unmodifiableMap(builder.reservedWordsMappings);
serverVariables = Collections.unmodifiableMap(builder.serverVariables);
gitHost = builder.gitHost;
Expand Down Expand Up @@ -516,6 +527,7 @@ public GeneratorSettings() {
enumNameMappings = Collections.unmodifiableMap(new HashMap<>(0));
openapiNormalizer = Collections.unmodifiableMap(new HashMap<>(0));
languageSpecificPrimitives = Collections.unmodifiableSet(new HashSet<>(0));
openapiGeneratorIgnoreList = Collections.unmodifiableSet(new HashSet<>(0));
reservedWordsMappings = Collections.unmodifiableMap(new HashMap<>(0));
serverVariables = Collections.unmodifiableMap(new HashMap<>(0));
}
Expand Down Expand Up @@ -593,6 +605,9 @@ public static Builder newBuilder(GeneratorSettings copy) {
if (copy.getLanguageSpecificPrimitives() != null) {
builder.languageSpecificPrimitives.addAll(copy.getLanguageSpecificPrimitives());
}
if (copy.getOpenAPIGeneratorIgnoreList() != null) {
builder.openapiGeneratorIgnoreList.addAll(copy.getOpenAPIGeneratorIgnoreList());
}
if (copy.getReservedWordsMappings() != null) {
builder.reservedWordsMappings.putAll(copy.getReservedWordsMappings());
}
Expand Down Expand Up @@ -638,6 +653,7 @@ public static final class Builder {
private Map<String, String> enumNameMappings;
private Map<String, String> openapiNormalizer;
private Set<String> languageSpecificPrimitives;
private Set<String> openapiGeneratorIgnoreList;
private Map<String, String> reservedWordsMappings;
private Map<String, String> serverVariables;
private String gitHost;
Expand All @@ -663,6 +679,7 @@ public Builder() {
enumNameMappings = new HashMap<>();
openapiNormalizer = new HashMap<>();
languageSpecificPrimitives = new HashSet<>();
openapiGeneratorIgnoreList = new HashSet<>();
reservedWordsMappings = new HashMap<>();
serverVariables = new HashMap<>();

Expand Down Expand Up @@ -1137,6 +1154,31 @@ public Builder withLanguageSpecificPrimitive(String value) {
return this;
}

/**
* Sets the {@code openapiGeneratorIgnoreList} and returns a reference to this Builder so that the methods can be chained together.
*
* @param openapiGeneratorIgnoreList the {@code openapiGeneratorIgnoreList} to set
* @return a reference to this Builder
*/
public Builder withOpenAPIGeneratorIgnoreList(Set<String> openapiGeneratorIgnoreList) {
this.openapiGeneratorIgnoreList = openapiGeneratorIgnoreList;
return this;
}

/**
* Sets a single {@code openapiGeneratorIgnoreList} and returns a reference to this Builder so that the methods can be chained together.
*
* @param value The value of entry to set
* @return a reference to this Builder
*/
public Builder withOpenAPIGeneratorIgnoreList(String value) {
if (this.openapiGeneratorIgnoreList == null) {
this.openapiGeneratorIgnoreList = new HashSet<>();
}
this.openapiGeneratorIgnoreList.add(value);
return this;
}

/**
* Sets the {@code reservedWordsMappings} and returns a reference to this Builder so that the methods can be chained together.
*
Expand Down Expand Up @@ -1266,6 +1308,7 @@ public String toString() {
", additionalProperties=" + additionalProperties +
", importMappings=" + importMappings +
", languageSpecificPrimitives=" + languageSpecificPrimitives +
", openapiGeneratorIgnoreList=" + openapiGeneratorIgnoreList +
", reservedWordsMappings=" + reservedWordsMappings +
", gitHost='" + gitHost + '\'' +
", gitUserId='" + gitUserId + '\'' +
Expand Down Expand Up @@ -1305,6 +1348,7 @@ public boolean equals(Object o) {
Objects.equals(getEnumNameMappings(), that.getEnumNameMappings()) &&
Objects.equals(getOpenAPINormalizer(), that.getOpenAPINormalizer()) &&
Objects.equals(getLanguageSpecificPrimitives(), that.getLanguageSpecificPrimitives()) &&
Objects.equals(getOpenAPIGeneratorIgnoreList(), that.getOpenAPIGeneratorIgnoreList()) &&
Objects.equals(getReservedWordsMappings(), that.getReservedWordsMappings()) &&
Objects.equals(getGitHost(), that.getGitHost()) &&
Objects.equals(getGitUserId(), that.getGitUserId()) &&
Expand Down Expand Up @@ -1341,6 +1385,7 @@ public int hashCode() {
getEnumNameMappings(),
getOpenAPINormalizer(),
getLanguageSpecificPrimitives(),
getOpenAPIGeneratorIgnoreList(),
getReservedWordsMappings(),
getGitHost(),
getGitUserId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
*/
val languageSpecificPrimitives = project.objects.listProperty<String>()

/**
* Specifies .openapi-generator-ignore list in the form of relative/path/to/file1,relative/path/to/file2. For example: README.md,pom.xml.
*/
val openapiGeneratorIgnoreList = project.objects.listProperty<String>()

/**
* Specifies mappings between a given class and the import that should be used for that class.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ open class GenerateTask @Inject constructor(private val objectFactory: ObjectFac
@Input
val languageSpecificPrimitives = project.objects.listProperty<String>()

/**
* Specifies .openapi-generator-ignore list in the form of relative/path/to/file1,relative/path/to/file2. For example: README.md,pom.xml.
*/
@Optional
@Input
val openapiGeneratorIgnoreList = project.objects.listProperty<String>()

/**
* Specifies mappings between a given class and the import that should be used for that class.
*/
Expand Down Expand Up @@ -895,6 +902,12 @@ open class GenerateTask @Inject constructor(private val objectFactory: ObjectFac
}
}

if (openapiGeneratorIgnoreList.isPresent) {
openapiGeneratorIgnoreList.get().forEach {
configurator.addOpenAPIGeneratorIgnoreList(it)
}
}

if (reservedWordsMappings.isPresent) {
reservedWordsMappings.get().forEach { entry ->
configurator.addAdditionalReservedWordMapping(entry.key, entry.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,12 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(name = "languageSpecificPrimitives", property = "openapi.generator.maven.plugin.languageSpecificPrimitives")
private List<String> languageSpecificPrimitives;

/**
* A list of openapi-generator-ignore entries
*/
@Parameter(name = "openapiGeneratorIgnoreList", property = "openapi.generator.maven.plugin.openapiGeneratorIgnoreList")
private List<String> openapiGeneratorIgnoreList;

/**
* A map of additional properties that can be referenced by the mustache templates
*/
Expand Down Expand Up @@ -783,6 +789,12 @@ public void execute() throws MojoExecutionException {
.get("language-specific-primitives").toString(), configurator);
}

// Retained for backwards-compatibility with configOptions -> openapi-generator-ignore-list
if (openapiGeneratorIgnoreList == null && configOptions.containsKey("openapi-generator-ignore-list")) {
applyOpenAPIGeneratorIgnoreListCsv(configOptions
.get("openapi-generator-ignore-list").toString(), configurator);
}

// Retained for backwards-compatibility with configOptions -> additional-properties
if (additionalProperties == null && configOptions.containsKey("additional-properties")) {
applyAdditionalPropertiesKvp(configOptions.get("additional-properties").toString(),
Expand Down Expand Up @@ -861,6 +873,12 @@ public void execute() throws MojoExecutionException {
applyLanguageSpecificPrimitivesCsvList(languageSpecificPrimitives, configurator);
}

// Apply Language Specific Primitives
if (openapiGeneratorIgnoreList != null
&& (configOptions == null || !configOptions.containsKey("openapi-generator-ignore-list"))) {
applyOpenAPIGeneratorIgnoreListCsvList(openapiGeneratorIgnoreList, configurator);
}

// Apply Additional Properties
if (additionalProperties != null && (configOptions == null || !configOptions.containsKey("additional-properties"))) {
applyAdditionalPropertiesKvpList(additionalProperties, configurator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ public interface CodegenConfig {

Set<String> languageSpecificPrimitives();

Set<String> openapiGeneratorIgnoreList();

Map<String, String> reservedWordsMappings();

void preprocessOpenAPI(OpenAPI openAPI);
Expand Down Expand Up @@ -349,4 +351,6 @@ public interface CodegenConfig {

boolean getUseOpenAPINormalizer();

Set<String> getOpenAPIGeneratorIgnoreList();

}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public class DefaultCodegen implements CodegenConfig {
protected Map<String, String> instantiationTypes;
protected Set<String> reservedWords;
protected Set<String> languageSpecificPrimitives = new HashSet<>();
protected Set<String> openapiGeneratorIgnoreList = new HashSet<>();
protected Map<String, String> importMapping = new HashMap<>();
// a map to store the mapping between a schema and the new one
protected Map<String, String> schemaMapping = new HashMap<>();
Expand Down Expand Up @@ -1211,6 +1212,11 @@ public Set<String> languageSpecificPrimitives() {
return languageSpecificPrimitives;
}

@Override
public Set<String> openapiGeneratorIgnoreList() {
return openapiGeneratorIgnoreList;
}

@Override
public Map<String, String> importMapping() {
return importMapping;
Expand Down Expand Up @@ -8317,6 +8323,11 @@ public List<VendorExtension> getSupportedVendorExtensions() {
@Override
public boolean getUseOpenAPINormalizer() { return true; }

@Override
public Set<String> getOpenAPIGeneratorIgnoreList() {
return openapiGeneratorIgnoreList;
}

/*
A function to convert yaml or json ingested strings like property names
And convert special characters like newline, tab, carriage return
Expand Down
Loading
Loading