Remove deprecated classes (#177)

* Remove:
 - org.openapitools.codegen.Codegen
 - org.openapitools.codegen.MetaGenerator

* Renaming:
 - DynamicSwaggerConfig > DynamicConfig
 - SwaggerResource > CodegenResource
This commit is contained in:
Jérémie Bresson
2018-04-21 17:26:54 +02:00
committed by GitHub
parent f79f23865b
commit fe00a63ed0
7 changed files with 21 additions and 369 deletions

View File

@@ -27,7 +27,7 @@ public class Bootstrap extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
DynamicSwaggerConfig bc = new DynamicSwaggerConfig();
DynamicConfig bc = new DynamicConfig();
bc.setBasePath("/api");
bc.setTitle("OpenAPI Generator");
bc.setDescription("This is an online OpenAPI generator. You can find out more "

View File

@@ -1,7 +1,7 @@
package org.openapitools.codegen.online;
import org.openapitools.codegen.Codegen;
import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.CodegenConfigLoader;
import org.openapitools.codegen.CodegenType;
import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.models.Operation;
@@ -14,12 +14,12 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DynamicSwaggerConfig extends BeanConfig {
public class DynamicConfig extends BeanConfig {
static List<String> clients = new ArrayList<String>();
static List<String> servers = new ArrayList<String>();
static {
List<CodegenConfig> extensions = Codegen.getExtensions();
List<CodegenConfig> extensions = CodegenConfigLoader.getAll();
for (CodegenConfig config : extensions) {
if (config.getTag().equals(CodegenType.CLIENT)
|| config.getTag().equals(CodegenType.DOCUMENTATION)) {

View File

@@ -135,7 +135,7 @@ public class Generator {
clientOptInput.setConfig(codegenConfig);
try {
List<File> files = new Codegen().opts(clientOptInput).generate();
List<File> files = new DefaultGenerator().opts(clientOptInput).generate();
if (files.size() > 0) {
List<File> filesToAdd = new ArrayList<File>();
LOGGER.debug("adding to " + outputFolder);

View File

@@ -4,8 +4,8 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.Codegen;
import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.CodegenConfigLoader;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.online.exception.BadRequestException;
import org.openapitools.codegen.online.model.Generated;
@@ -25,14 +25,13 @@ import java.util.*;
@Path("/gen")
@Api(value = "/gen", description = "Resource for generating swagger components")
@SuppressWarnings("static-method")
public class SwaggerResource {
public class CodegenResource {
static List<String> clients = new ArrayList<String>();
static List<String> servers = new ArrayList<String>();
private static Map<String, Generated> fileMap = new HashMap<String, Generated>();
static {
List<CodegenConfig> extensions = Codegen.getExtensions();
List<CodegenConfig> extensions = CodegenConfigLoader.getAll();
for (CodegenConfig config : extensions) {
if (config.getTag().equals(CodegenType.CLIENT)
|| config.getTag().equals(CodegenType.DOCUMENTATION)) {

View File

@@ -1,166 +0,0 @@
package org.openapitools.codegen;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import config.Config;
import config.ConfigParser;
/**
* @deprecated use instead {@link org.openapitools.codegen.DefaultGenerator}
* or cli interface from https://github.com/swagger-api/swagger-codegen/pull/547
*/
@Deprecated
public class Codegen extends DefaultGenerator {
private static final Logger LOGGER = LoggerFactory.getLogger(Codegen.class);
static Map<String, CodegenConfig> configs = new HashMap<String, CodegenConfig>();
static String configString;
static String debugInfoOptions = "\nThe following additional debug options are available for all codegen targets:" +
"\n -DdebugSwagger prints the swagger specification as interpreted by the codegen" +
"\n -DdebugModels prints models passed to the template engine" +
"\n -DdebugOperations prints operations passed to the template engine" +
"\n -DdebugSupportingFiles prints additional data passed to the template engine";
@SuppressWarnings("deprecation")
public static void main(String[] args) {
Options options = new Options();
options.addOption("h", "help", false, "shows this message");
options.addOption("l", "lang", true, "client language to generate.\nAvailable languages include:\n\t[" + configString + "]");
options.addOption("o", "output", true, "where to write the generated files");
options.addOption("i", "input-spec", true, "location of the swagger spec, as URL or file");
options.addOption("t", "template-dir", true, "folder containing the template files");
options.addOption("d", "debug-info", false, "prints additional info for debugging");
options.addOption("a", "auth", true, "adds authorization headers when fetching the swagger definitions remotely. Pass in a URL-encoded string of name:header with a comma separating multiple values");
options.addOption("c", "config", true, "location of the configuration file");
ClientOptInput clientOptInput = new ClientOptInput();
ClientOpts clientOpts = new ClientOpts();
OpenAPI openAPI = null;
CommandLine cmd = null;
try {
CommandLineParser parser = new BasicParser();
CodegenConfig config = null;
cmd = parser.parse(options, args);
if (cmd.hasOption("d")) {
usage(options);
System.out.println(debugInfoOptions);
return;
}
if (cmd.hasOption("a")) {
clientOptInput.setAuth(cmd.getOptionValue("a"));
}
if (cmd.hasOption("l")) {
clientOptInput.setConfig(getConfig(cmd.getOptionValue("l")));
} else {
usage(options);
return;
}
if (cmd.hasOption("o")) {
clientOptInput.getConfig().setOutputDir(cmd.getOptionValue("o"));
}
if (cmd.hasOption("h")) {
if (cmd.hasOption("l")) {
config = getConfig(String.valueOf(cmd.getOptionValue("l")));
if (config != null) {
options.addOption("h", "help", true, config.getHelp());
usage(options);
return;
}
}
usage(options);
return;
}
if (cmd.hasOption("i")) {
openAPI = new OpenAPIParser().readLocation(cmd.getOptionValue("i"), clientOptInput.getAuthorizationValues(), null).getOpenAPI();
}
if (cmd.hasOption("c")) {
String configFile = cmd.getOptionValue("c");
Config genConfig = ConfigParser.read(configFile);
config = clientOptInput.getConfig();
if (null != genConfig && null != config) {
for (CliOption langCliOption : config.cliOptions()) {
if (genConfig.hasOption(langCliOption.getOpt())) {
config.additionalProperties().put(langCliOption.getOpt(), genConfig.getOption(langCliOption.getOpt()));
}
}
}
}
if (cmd.hasOption("t")) {
clientOpts.getProperties().put(CodegenConstants.TEMPLATE_DIR, String.valueOf(cmd.getOptionValue("t")));
}
} catch (Exception e) {
usage(options);
return;
}
try {
clientOptInput
.opts(clientOpts)
.openAPI(openAPI);
new Codegen().opts(clientOptInput).generate();
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
public static List<CodegenConfig> getExtensions() {
ServiceLoader<CodegenConfig> loader = ServiceLoader.load(CodegenConfig.class);
List<CodegenConfig> output = new ArrayList<CodegenConfig>();
for (CodegenConfig aLoader : loader) {
output.add(aLoader);
}
return output;
}
static void usage(Options options) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("Codegen", options);
}
public static CodegenConfig getConfig(String name) {
if (configs.containsKey(name)) {
return configs.get(name);
} else {
// see if it's a class
try {
LOGGER.debug("loading class " + name);
Class<?> customClass = Class.forName(name);
LOGGER.debug("loaded");
return (CodegenConfig) customClass.newInstance();
} catch (Exception e) {
throw new RuntimeException("can't load class " + name);
}
}
}
static {
List<CodegenConfig> extensions = getExtensions();
StringBuilder sb = new StringBuilder();
for (CodegenConfig config : extensions) {
if (sb.toString().length() != 0) {
sb.append(", ");
}
sb.append(config.getName());
configs.put(config.getName(), config);
configString = sb.toString();
}
}
}

View File

@@ -1,9 +1,11 @@
package org.openapitools.codegen;
import java.util.ServiceLoader;
import static java.util.ServiceLoader.load;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
public class CodegenConfigLoader {
/**
* Tries to load config class with SPI first, then with class name directly from classpath
@@ -31,4 +33,13 @@ public class CodegenConfigLoader {
throw new RuntimeException("Can't load config class with name ".concat(name) + " Available:\n" + availableConfigs.toString(), e);
}
}
public static List<CodegenConfig> getAll() {
ServiceLoader<CodegenConfig> loader = ServiceLoader.load(CodegenConfig.class);
List<CodegenConfig> output = new ArrayList<CodegenConfig>();
for (CodegenConfig aLoader : loader) {
output.add(aLoader);
}
return output;
}
}

View File

@@ -1,192 +0,0 @@
package org.openapitools.codegen;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
/**
* @deprecated use instead {@link org.openapitools.codegen.DefaultGenerator}
* or cli interface from https://github.com/swagger-api/swagger-codegen/pull/547
*/
@Deprecated
public class MetaGenerator extends AbstractGenerator {
private static final Logger LOGGER = LoggerFactory.getLogger(MetaGenerator.class);
static Map<String, CodegenConfig> configs = new HashMap<String, CodegenConfig>();
static String configString;
public static void main(String[] args) {
new MetaGenerator().generate(args);
}
public static List<CodegenConfig> getExtensions() {
ServiceLoader<CodegenConfig> loader = ServiceLoader.load(CodegenConfig.class);
List<CodegenConfig> output = new ArrayList<CodegenConfig>();
for (CodegenConfig config : loader) {
output.add(config);
}
return output;
}
static void usage(Options options) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("MetaGenerator. Generator for creating a new template set " +
"and configuration for Codegen. The output will be based on the language you " +
"specify, and includes default templates to include.", options);
}
public static CodegenConfig getConfig(String name) {
if (configs.containsKey(name)) {
return configs.get(name);
}
return null;
}
protected void generate(String[] args) {
String outputFolder = null;
String name = null;
String targetPackage = "org.openapitools.codegen";
final String templateDir = "codegen";
Options options = new Options();
options.addOption("h", "help", false, "shows this message");
options.addOption("l", "lang", false, "client language to generate.\nAvailable languages include:\n\t[" + configString + "]");
options.addOption("o", "output", true, "where to write the generated files");
options.addOption("n", "name", true, "the human-readable name of the generator");
options.addOption("p", "package", true, "the package to put the main class into (defaults to org.openapitools.codegen");
CommandLine cmd = null;
try {
CommandLineParser parser = new BasicParser();
cmd = parser.parse(options, args);
if (cmd.hasOption("h")) {
usage(options);
return;
}
if (cmd.hasOption("n")) {
name = cmd.getOptionValue("n");
} else {
System.out.println("name is required"); //FIXME replace by LOGGER
usage(options);
return;
}
if (cmd.hasOption("l")) {
}
if (cmd.hasOption("p")) {
targetPackage = cmd.getOptionValue("p");
}
if (cmd.hasOption("o")) {
outputFolder = cmd.getOptionValue("o");
} else {
System.out.println("output folder is required"); // FIXME replace by LOGGER
usage(options);
return;
}
} catch (Exception e) {
usage(options);
return;
}
LOGGER.info("writing to folder " + outputFolder);
File outputFolderLocation = new File(outputFolder);
if (!outputFolderLocation.exists()) {
outputFolderLocation.mkdirs();
}
File sourceFolder = new File(outputFolder + File.separator + "src/main/java/" + targetPackage.replace('.', File.separatorChar));
if (!sourceFolder.exists()) {
sourceFolder.mkdirs();
}
File resourcesFolder = new File(outputFolder + File.separator + "src/main/resources/META-INF/services");
if (!resourcesFolder.exists()) {
resourcesFolder.mkdirs();
}
String mainClass = Character.toUpperCase(name.charAt(0)) + name.substring(1) + "Generator";
List<SupportingFile> supportingFiles = new ArrayList<SupportingFile>();
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
supportingFiles.add(new SupportingFile("generatorClass.mustache",
"src/main/java/" + File.separator + targetPackage.replace('.', File.separatorChar),
mainClass + ".java"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("api.template", "src/main/resources" + File.separator + name, "api.mustache"));
supportingFiles.add(new SupportingFile("model.template", "src/main/resources" + File.separator + name, "model.mustache"));
supportingFiles.add(new SupportingFile("services.mustache", "src/main/resources/META-INF/services", "org.openapitools.codegen.CodegenConfig"));
List<File> files = new ArrayList<File>();
Map<String, Object> data = new HashMap<String, Object>();
data.put("generatorPackage", targetPackage);
data.put("generatorClass", mainClass);
data.put("name", name);
data.put("fullyQualifiedGeneratorClass", targetPackage + "." + mainClass);
for (SupportingFile support : supportingFiles) {
try {
String destinationFolder = outputFolder;
if (support.folder != null && !"".equals(support.folder)) {
destinationFolder += File.separator + support.folder;
}
File of = new File(destinationFolder);
if (!of.isDirectory()) {
of.mkdirs();
}
String outputFilename = destinationFolder + File.separator + support.destinationFilename;
if (support.templateFile.endsWith("mustache")) {
String template = readTemplate(templateDir + File.separator + support.templateFile);
Template tmpl = Mustache.compiler()
.withLoader(new Mustache.TemplateLoader() {
@Override
public Reader getTemplate(String name) {
return getTemplateReader(templateDir + File.separator + name + ".mustache");
}
})
.defaultValue("")
.compile(template);
writeToFile(outputFilename, tmpl.execute(data));
files.add(new File(outputFilename));
} else {
String template = readTemplate(templateDir + File.separator + support.templateFile);
FileUtils.writeStringToFile(new File(outputFilename), template);
LOGGER.info("copying file to " + outputFilename);
files.add(new File(outputFilename));
}
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
}
}
static {
List<CodegenConfig> extensions = getExtensions();
StringBuilder sb = new StringBuilder();
for (CodegenConfig config : extensions) {
if (sb.toString().length() != 0) {
sb.append(", ");
}
sb.append(config.getName());
configs.put(config.getName(), config);
configString = sb.toString();
}
}
}