Skip to content

implements GeneratorService 2.x / 3.x wrapper and refactor generator module #8524

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

Merged
merged 3 commits into from
Aug 2, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public void run() {

// if a config file wasn't specified or we were unable to read it
if (configurator == null) {
// createa a fresh configurator
// create a fresh configurator
configurator = new CodegenConfigurator();
}

Expand All @@ -231,7 +231,7 @@ public void run() {
}

if (isNotEmpty(spec)) {
configurator.setInputSpec(spec);
configurator.setInputSpecURL(spec);
}

if (isNotEmpty(lang)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ private void setupAndRunTest(String specFlag, final String spec, String langFlag
{
configurator.setLang(lang);
times = 1;
configurator.setInputSpec(spec);
configurator.setInputSpecURL(spec);
times = 1;
configurator.setOutputDir(outputDir);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public void execute() throws MojoExecutionException {
}

if (isNotEmpty(inputSpec)) {
configurator.setInputSpec(inputSpec);
configurator.setInputSpecURL(inputSpec);
}

if (isNotEmpty(gitUserId)) {
Expand Down
41 changes: 26 additions & 15 deletions modules/swagger-codegen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
</reporting>
<properties>
<diffutils-version>1.3.0</diffutils-version>
<swagger-codegen-v2-version>2.3.1</swagger-codegen-v2-version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -223,6 +224,11 @@
<artifactId>swagger-parser</artifactId>
<version>${swagger-parser-version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen</artifactId>
<version>${swagger-codegen-v2-version}</version>
</dependency>
<dependency>
<groupId>com.samskivert</groupId>
<artifactId>jmustache</artifactId>
Expand Down Expand Up @@ -258,6 +264,22 @@
<artifactId>commons-cli</artifactId>
<version>${commons-cli-version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
<version>4.0.6</version>
</dependency>
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>0.9.0</version>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand All @@ -276,35 +298,24 @@
<!-- <version>${jmockit-version}</version> -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>

<dependency>
<groupId>com.googlecode.java-diff-utils</groupId>
<artifactId>diffutils</artifactId>
<version>${diffutils-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.8.47</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
<version>4.0.6</version>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-generators</artifactId>
<version>${swagger-codegen-generators-version}</version>
<scope>test</scope>
</dependency>

</dependencies>
<repositories>
<repository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@
import io.swagger.v3.parser.core.models.AuthorizationValue;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import io.swagger.v3.parser.util.ClasspathHelper;
import io.swagger.v3.parser.util.RemoteUrl;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -44,13 +51,15 @@ public class CodegenConfigurator implements Serializable {

private String lang;
private String inputSpec;
private String inputSpecURL;
private String outputDir;
private boolean verbose;
private boolean skipOverwrite;
private boolean removeOperationIdPrefix;
private String templateDir;
private String templateVersion;
private String auth;
private AuthorizationValue authorizationValue;
private String apiPackage;
private String modelPackage;
private String invokerPackage;
Expand Down Expand Up @@ -95,6 +104,15 @@ public String getInputSpec() {
return inputSpec;
}

public String getInputSpecURL() {
return inputSpecURL;
}

public CodegenConfigurator setInputSpecURL(String inputSpecURL) {
this.inputSpecURL = inputSpecURL;
return this;
}

public String getOutputDir() {
return outputDir;
}
Expand Down Expand Up @@ -196,6 +214,13 @@ public CodegenConfigurator setAuth(String auth) {
return this;
}

public AuthorizationValue getAuthorizationValue() {
return authorizationValue;
}
public void setAuthorizationValue(AuthorizationValue authorizationValue) {
this.authorizationValue = authorizationValue;
}

public String getApiPackage() {
return apiPackage;
}
Expand Down Expand Up @@ -393,78 +418,80 @@ public CodegenConfigurator setIgnoreFileOverride(final String ignoreFileOverride
return this;
}

public String loadSpecContent(String location, List<AuthorizationValue> auths) throws Exception{
location = location.replaceAll("\\\\","/");
String data = "";
if (location.toLowerCase().startsWith("http")) {
data = RemoteUrl.urlToString(location, auths);
} else {
final String fileScheme = "file:";
Path path;
if (location.toLowerCase().startsWith(fileScheme)) {
path = Paths.get(URI.create(location));
} else {
path = Paths.get(location);
}
if (Files.exists(path)) {
data = FileUtils.readFileToString(path.toFile(), "UTF-8");
} else {
data = ClasspathHelper.loadFileFromClasspath(location);
}
}
LOGGER.debug("Loaded raw data: {}", data);
return data;
}

public ClientOptInput toClientOptInput() {

Validate.notEmpty(lang, "language must be specified");
Validate.notEmpty(inputSpec, "input spec must be specified");

if (StringUtils.isBlank(inputSpec) && StringUtils.isBlank(inputSpecURL)) {
throw new IllegalArgumentException("input spec or URL must be specified");
}

setVerboseFlags();
setSystemProperties();

CodegenConfig config = CodegenConfigLoader.forName(lang);

config.setInputSpec(inputSpec);
config.setOutputDir(outputDir);
config.setSkipOverwrite(skipOverwrite);
config.setIgnoreFilePathOverride(ignoreFileOverride);
config.setRemoveOperationIdPrefix(removeOperationIdPrefix);

config.instantiationTypes().putAll(instantiationTypes);
config.typeMapping().putAll(typeMappings);
config.importMapping().putAll(importMappings);
config.languageSpecificPrimitives().addAll(languageSpecificPrimitives);
config.reservedWordsMappings().putAll(reservedWordMappings);

config.setLanguageArguments(codegenArguments);

checkAndSetAdditionalProperty(apiPackage, CodegenConstants.API_PACKAGE);
checkAndSetAdditionalProperty(modelPackage, CodegenConstants.MODEL_PACKAGE);
checkAndSetAdditionalProperty(invokerPackage, CodegenConstants.INVOKER_PACKAGE);
checkAndSetAdditionalProperty(groupId, CodegenConstants.GROUP_ID);
checkAndSetAdditionalProperty(artifactId, CodegenConstants.ARTIFACT_ID);
checkAndSetAdditionalProperty(artifactVersion, CodegenConstants.ARTIFACT_VERSION);
checkAndSetAdditionalProperty(templateDir, toAbsolutePathStr(templateDir), CodegenConstants.TEMPLATE_DIR);
checkAndSetAdditionalProperty(templateVersion, CodegenConstants.TEMPLATE_VERSION);
checkAndSetAdditionalProperty(modelNamePrefix, CodegenConstants.MODEL_NAME_PREFIX);
checkAndSetAdditionalProperty(modelNameSuffix, CodegenConstants.MODEL_NAME_SUFFIX);
checkAndSetAdditionalProperty(gitUserId, CodegenConstants.GIT_USER_ID);
checkAndSetAdditionalProperty(gitRepoId, CodegenConstants.GIT_REPO_ID);
checkAndSetAdditionalProperty(releaseNote, CodegenConstants.RELEASE_NOTE);
checkAndSetAdditionalProperty(httpUserAgent, CodegenConstants.HTTP_USER_AGENT);

handleDynamicProperties(config);

if (isNotEmpty(library)) {
config.setLibrary(library);
}

config.additionalProperties().putAll(additionalProperties);

ClientOptInput input = new ClientOptInput()
.config(config);

ClientOptInput input = new ClientOptInput();
final List<AuthorizationValue> authorizationValues = AuthParser.parse(auth);
if (authorizationValue != null) {
authorizationValues.add(authorizationValue);
}

ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setFlatten(true);
SwaggerParseResult result = new OpenAPIParser().readLocation(inputSpec, authorizationValues, options);
OpenAPI openAPI = result.getOpenAPI();

input.opts(new ClientOpts())
.openAPI(openAPI);

return input;
}

public ClientOptInput toClientOptInput(String content) {
if (!StringUtils.isBlank(inputSpec)) {
config.setInputSpec(inputSpec);
SwaggerParseResult result = new OpenAPIParser().readContents(inputSpec, authorizationValues, null);
OpenAPI openAPI = result.getOpenAPI();

Validate.notEmpty(lang, "language must be specified");
input.opts(new ClientOpts())
.openAPI(openAPI);

setVerboseFlags();
setSystemProperties();
} else {
String specContent = null;
try {
specContent = loadSpecContent(inputSpecURL, authorizationValues);
} catch (Exception e) {
String msg = "Unable to read URL: " + inputSpecURL;
LOGGER.error(msg, e);
throw new IllegalArgumentException(msg);
}

CodegenConfig config = CodegenConfigLoader.forName(lang);
if (StringUtils.isBlank(specContent)) {
String msg = "Empty content found in URL: " + inputSpecURL;
LOGGER.error(msg);
throw new IllegalArgumentException(msg);
}
config.setInputSpec(specContent);
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setFlatten(true);
SwaggerParseResult result = new OpenAPIParser().readLocation(inputSpecURL, authorizationValues, options);
OpenAPI openAPI = result.getOpenAPI();

input.opts(new ClientOpts())
.openAPI(openAPI);
}

config.setOutputDir(outputDir);
config.setSkipOverwrite(skipOverwrite);
Expand Down Expand Up @@ -502,17 +529,7 @@ public ClientOptInput toClientOptInput(String content) {

config.additionalProperties().putAll(additionalProperties);

ClientOptInput input = new ClientOptInput()
.config(config);

final List<AuthorizationValue> authorizationValues = AuthParser.parse(auth);

SwaggerParseResult result = new OpenAPIParser().readContents(content, authorizationValues, null);
OpenAPI openAPI = result.getOpenAPI();

input.opts(new ClientOpts())
.openAPI(openAPI);

input.config(config);
return input;
}

Expand Down
Loading