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

[core][maven][gradle] User-defined server variable substitutions #3363

Merged
merged 6 commits into from
Aug 11, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ NAME
SYNOPSIS
openapi-generator-cli generate
[(-a <authorization> | --auth <authorization>)]
[--additional-properties <additional properties>...]
[--api-package <api package>] [--artifact-id <artifact id>]
[--artifact-version <artifact version>]
[(-c <configuration file> | --config <configuration file>)]
Expand All @@ -450,12 +449,13 @@ SYNOPSIS
[--model-name-prefix <model name prefix>]
[--model-name-suffix <model name suffix>]
[--model-package <model package>]
[(-o <output directory> | --output <output directory>)]
[(-o <output directory> | --output <output directory>)]
[(-p <additional properties> | --additional-properties <additional properties>)...]
[--package-name <package name>] [--release-note <release note>]
[--remove-operation-id-prefix]
[--reserved-words-mappings <reserved word mappings>...]
[(-s | --skip-overwrite)] [--skip-validate-spec]
[--strict-spec <true/false strict behavior>]
[(-s | --skip-overwrite)] [--server-variables <server variables>...]
[--skip-validate-spec] [--strict-spec <true/false strict behavior>]
[(-t <template directory> | --template-dir <template directory>)]
[--type-mappings <type mappings>...] [(-v | --verbose)]

Expand Down
30 changes: 18 additions & 12 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ NAME
SYNOPSIS
openapi-generator-cli generate
[(-a <authorization> | --auth <authorization>)]
[--additional-properties <additional properties>...]
[--api-package <api package>] [--artifact-id <artifact id>]
[--artifact-version <artifact version>]
[(-c <configuration file> | --config <configuration file>)]
Expand All @@ -256,15 +255,15 @@ SYNOPSIS
[--model-name-prefix <model name prefix>]
[--model-name-suffix <model name suffix>]
[--model-package <model package>]
[(-o <output directory> | --output <output directory>)]
[(-o <output directory> | --output <output directory>)]
[(-p <additional properties> | --additional-properties <additional properties>)...]
[--package-name <package name>] [--release-note <release note>]
[--remove-operation-id-prefix]
[--reserved-words-mappings <reserved word mappings>...]
[(-s | --skip-overwrite)] [--skip-validate-spec]
[--strict-spec <true/false strict behavior>]
[(-s | --skip-overwrite)] [--server-variables <server variables>...]
[--skip-validate-spec] [--strict-spec <true/false strict behavior>]
[(-t <template directory> | --template-dir <template directory>)]
[--type-mappings <type mappings>...] [(-v | --verbose)]

```

<details>
Expand All @@ -277,19 +276,16 @@ OPTIONS
remotely. Pass in a URL-encoded string of name:header with a comma
separating multiple values

--additional-properties <additional properties>
sets additional properties that can be referenced by the mustache
templates in the format of name=value,name=value. You can also have
multiple occurrences of this option.

--api-package <api package>
package for generated api classes

--artifact-id <artifact id>
artifactId in generated pom.xml
artifactId in generated pom.xml. This also becomes part of the
generated library's filename

--artifact-version <artifact version>
artifact version in generated pom.xml
artifact version in generated pom.xml. This also becomes part of the
generated library's filename

-c <configuration file>, --config <configuration file>
Path to configuration file configuration file. It can be json or
Expand Down Expand Up @@ -382,6 +378,12 @@ OPTIONS
-o <output directory>, --output <output directory>
where to write the generated files (current dir by default)

-p <additional properties>, --additional-properties <additional
properties>
sets additional properties that can be referenced by the mustache
templates in the format of name=value,name=value. You can also have
multiple occurrences of this option.

--package-name <package name>
package for generated classes (where supported)

Expand All @@ -400,6 +402,10 @@ OPTIONS
specifies if the existing files should be overwritten during the
generation.

--server-variables <server variables>
sets server variables for spec documents which support variable
templating of servers.

--skip-validate-spec
Skips the default behavior of validating an input specification.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
package org.openapitools.codegen.cmd;

import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyAdditionalPropertiesKvpList;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyImportMappingsKvpList;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyInstantiationTypesKvpList;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyLanguageSpecificPrimitivesCsvList;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyReservedWordsMappingsKvpList;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applySystemPropertiesKvpList;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyTypeMappingsKvpList;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.*;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.spi.FilterAttachable;
Expand Down Expand Up @@ -160,6 +154,12 @@ public class Generate implements Runnable {
+ " You can also have multiple occurrences of this option.")
private List<String> importMappings = new ArrayList<>();

@Option(
name = {"--server-variables"},
title = "server variables",
description = "sets server variables overrides for spec documents which support variable templating of servers.")
private List<String> serverVariableOverrides = new ArrayList<>();

@Option(name = {"--invoker-package"}, title = "invoker package",
description = CodegenConstants.INVOKER_PACKAGE_DESC)
private String invokerPackage;
Expand Down Expand Up @@ -393,6 +393,7 @@ public void run() {
applyAdditionalPropertiesKvpList(additionalProperties, configurator);
applyLanguageSpecificPrimitivesCsvList(languageSpecificPrimitives, configurator);
applyReservedWordsMappingsKvpList(reservedWordsMappings, configurator);
applyServerVariablesKvpList(serverVariableOverrides, configurator);

try {
final ClientOptInput clientOptInput = configurator.toClientOptInput();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public final class GeneratorSettings implements Serializable {
private ImmutableMap<String, String> importMappings;
private ImmutableSet<String> languageSpecificPrimitives;
private ImmutableMap<String, String> reservedWordMappings;
private ImmutableMap<String, String> serverVariables;

private String gitUserId;
private String gitRepoId;
Expand Down Expand Up @@ -245,6 +246,17 @@ public Map<String, String> getReservedWordMappings() {
return reservedWordMappings;
}


/**
* Gets server variable. Values defined here will be attempted to be replaced within a templated server object.
*
* @return the server variables
*/
public Map<String, String> getServerVariables() {
return serverVariables;
}


/**
* Gets git user id. e.g. <strong>openapitools</strong>.
* <p>
Expand Down Expand Up @@ -311,6 +323,7 @@ private GeneratorSettings(Builder builder) {
importMappings = ImmutableMap.copyOf(builder.importMappings);
languageSpecificPrimitives = ImmutableSet.copyOf(builder.languageSpecificPrimitives);
reservedWordMappings = ImmutableMap.copyOf(builder.reservedWordMappings);
serverVariables = ImmutableMap.copyOf(builder.serverVariables);
gitUserId = builder.gitUserId;
gitRepoId = builder.gitRepoId;
releaseNote = builder.releaseNote;
Expand Down Expand Up @@ -373,6 +386,7 @@ public GeneratorSettings() {
importMappings = ImmutableMap.of();
languageSpecificPrimitives = ImmutableSet.of();
reservedWordMappings = ImmutableMap.of();
serverVariables = ImmutableMap.of();
}

private void setDefaults() {
Expand All @@ -394,12 +408,6 @@ public static Builder newBuilder() {
return new Builder();
}

/**
* New builder builder.
*
* @param copy the copy
* @return the builder
*/
public static Builder newBuilder(GeneratorSettings copy) {
Builder builder = new Builder();
builder.generatorName = copy.getGeneratorName();
Expand All @@ -419,6 +427,7 @@ public static Builder newBuilder(GeneratorSettings copy) {
builder.importMappings = new HashMap<>(copy.getImportMappings());
builder.languageSpecificPrimitives = new HashSet<>(copy.getLanguageSpecificPrimitives());
builder.reservedWordMappings = new HashMap<>(copy.getReservedWordMappings());
builder.serverVariables = new HashMap<>(copy.getServerVariables());
builder.gitUserId = copy.getGitUserId();
builder.gitRepoId = copy.getGitRepoId();
builder.releaseNote = copy.getReleaseNote();
Expand Down Expand Up @@ -449,6 +458,7 @@ public static final class Builder {
private Map<String, String> importMappings;
private Set<String> languageSpecificPrimitives;
private Map<String, String> reservedWordMappings;
private Map<String, String> serverVariables;
private String gitUserId;
private String gitRepoId;
private String releaseNote;
Expand All @@ -464,6 +474,7 @@ public Builder() {
importMappings = new HashMap<>();
languageSpecificPrimitives = new HashSet<>();
reservedWordMappings = new HashMap<>();
serverVariables = new HashMap<>();

gitUserId = DEFAULT_GIT_USER_ID;
gitRepoId = DEFAULT_GIT_REPO_ID;
Expand Down Expand Up @@ -617,6 +628,17 @@ public Builder withInstantiationType(String key, String value) {
return this;
}

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

/**
* Sets the {@code typeMappings} and returns a reference to this Builder so that the methods can be chained together.
*
Expand Down Expand Up @@ -731,6 +753,22 @@ public Builder withReservedWordMapping(String key, String value) {
return this;
}


/**
* Sets a single {@code serverVariables} and returns a reference to this Builder so that the methods can be chained together.
*
* @param key A key for some server variable
* @param value The value of some server variable to be replaced in a templated server object.
* @return a reference to this Builder
*/
public Builder withServerVariable(String key, String value) {
if (this.serverVariables == null) {
this.serverVariables = new HashMap<>();
}
this.serverVariables.put(key, value);
return this;
}

/**
* Sets the {@code gitUserId} and returns a reference to this Builder so that the methods can be chained together.
*
Expand Down
1 change: 1 addition & 0 deletions modules/openapi-generator-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
buildscript {
ext.kotlin_version = '1.2.61'
repositories {
mavenLocal()
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
instantiationTypes.set(generate.instantiationTypes)
typeMappings.set(generate.typeMappings)
additionalProperties.set(generate.additionalProperties)
serverVariables.set(generate.serverVariables)
languageSpecificPrimitives.set(generate.languageSpecificPrimitives)
importMappings.set(generate.importMappings)
invokerPackage.set(generate.invokerPackage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
*/
val additionalProperties = project.objects.property<Map<String, String>>()

/**
* Sets server variable for server URL template substitution, in the format of name=value,name=value.
*/
val serverVariables = project.objects.property<Map<String, String>>()

/**
* Specifies additional language specific primitive types in the format of type1,type2,type3,type3. For example: String,boolean,Boolean,Double.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ open class GenerateTask : DefaultTask() {
@get:Internal
val additionalProperties = project.objects.property<Map<String, String>>()

/**
* Sets server variable for server URL template substitution, in the format of name=value,name=value.
* You can also have multiple occurrences of this option.
*/
@get:Internal
val serverVariables = project.objects.property<Map<String, String>>()

/**
* Specifies additional language specific primitive types in the format of type1,type2,type3,type3. For example: String,boolean,Boolean,Double.
*/
Expand Down Expand Up @@ -573,6 +580,12 @@ open class GenerateTask : DefaultTask() {
}
}

if (serverVariables.isPresent) {
serverVariables.get().forEach { entry ->
configurator.addServerVariable(entry.key, entry.value)
}
}

if (languageSpecificPrimitives.isPresent) {
languageSpecificPrimitives.get().forEach {
configurator.addLanguageSpecificPrimitive(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,8 @@

package org.openapitools.codegen.plugin;

import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyAdditionalPropertiesKvp;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyImportMappingsKvp;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyInstantiationTypesKvp;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyLanguageSpecificPrimitivesCsv;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyTypeMappingsKvp;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyReservedWordsMappingsKvp;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyAdditionalPropertiesKvpList;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyImportMappingsKvpList;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyInstantiationTypesKvpList;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyLanguageSpecificPrimitivesCsvList;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyTypeMappingsKvpList;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.applyReservedWordsMappingsKvpList;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.*;

import java.io.File;
import java.util.HashMap;
Expand Down Expand Up @@ -289,6 +278,12 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(name = "additionalProperties", property = "openapi.generator.maven.plugin.additionalProperties")
private List<String> additionalProperties;

/**
* A map of server variable overrides for specs that support server URL templating
*/
@Parameter(name = "serverVariableOverrides", property = "openapi.generator.maven.plugin.serverVariableOverrides")
private List<String> serverVariables;

/**
* A map of reserved names and how they should be escaped
*/
Expand Down Expand Up @@ -615,6 +610,10 @@ public void execute() throws MojoExecutionException {
configurator);
}

if (serverVariables == null && configOptions.containsKey("server-variables")) {
applyServerVariablesKvp(configOptions.get("server-variables").toString(), configurator);
}

// Retained for backwards-compataibility with configOptions -> reserved-words-mappings
if (reservedWordsMappings == null && configOptions.containsKey("reserved-words-mappings")) {
applyReservedWordsMappingsKvp(configOptions.get("reserved-words-mappings")
Expand Down Expand Up @@ -648,6 +647,10 @@ public void execute() throws MojoExecutionException {
applyAdditionalPropertiesKvpList(additionalProperties, configurator);
}

if (serverVariables != null && (configOptions == null || !configOptions.containsKey("server-variables"))) {
applyServerVariablesKvpList(serverVariables, configurator);
}

// Apply Reserved Words Mappings
if (reservedWordsMappings != null && (configOptions == null || !configOptions.containsKey("reserved-words-mappings"))) {
applyReservedWordsMappingsKvpList(reservedWordsMappings, configurator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public interface CodegenConfig {

Map<String, Object> additionalProperties();

Map<String, String> serverVariableOverrides();

Map<String, Object> vendorExtensions();

String testPackage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public class CodegenServerVariable {
public String defaultValue;
public String description;
public List<String> enumValues;
public String value;
}
Loading