forked from loafle/openapi-generator-original
Merge pull request #1428 from swagger-api/feature/selective-generation
made selective generation of models
This commit is contained in:
commit
e289b858a9
@ -36,6 +36,7 @@ import config.Config;
|
|||||||
import config.ConfigParser;
|
import config.ConfigParser;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.ServiceLoader;
|
import java.util.ServiceLoader;
|
||||||
|
|
||||||
@ -112,6 +113,9 @@ public class CodeGenMojo extends AbstractMojo {
|
|||||||
@Parameter(defaultValue = "true")
|
@Parameter(defaultValue = "true")
|
||||||
private boolean addCompileSourceRoot = true;
|
private boolean addCompileSourceRoot = true;
|
||||||
|
|
||||||
|
@Parameter
|
||||||
|
protected Map<String, String> environmentVariables = new HashMap<String, String>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The project being built.
|
* The project being built.
|
||||||
*/
|
*/
|
||||||
@ -125,6 +129,12 @@ public class CodeGenMojo extends AbstractMojo {
|
|||||||
CodegenConfig config = CodegenConfigLoader.forName(language);
|
CodegenConfig config = CodegenConfigLoader.forName(language);
|
||||||
config.setOutputDir(output.getAbsolutePath());
|
config.setOutputDir(output.getAbsolutePath());
|
||||||
|
|
||||||
|
if (environmentVariables != null) {
|
||||||
|
for(String key : environmentVariables.keySet()) {
|
||||||
|
System.setProperty(key, environmentVariables.get(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (null != templateDirectory) {
|
if (null != templateDirectory) {
|
||||||
config.additionalProperties().put(TEMPLATE_DIR_PARAM, templateDirectory.getAbsolutePath());
|
config.additionalProperties().put(TEMPLATE_DIR_PARAM, templateDirectory.getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
@ -29,16 +29,7 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.LinkedHashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class DefaultGenerator extends AbstractGenerator implements Generator {
|
public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||||
Logger LOGGER = LoggerFactory.getLogger(DefaultGenerator.class);
|
Logger LOGGER = LoggerFactory.getLogger(DefaultGenerator.class);
|
||||||
@ -60,6 +51,53 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<File> generate() {
|
public List<File> generate() {
|
||||||
|
Boolean generateApis = null;
|
||||||
|
Boolean generateModels = null;
|
||||||
|
Boolean generateSupportingFiles = null;
|
||||||
|
|
||||||
|
Set<String> modelsToGenerate = null;
|
||||||
|
Set<String> apisToGenerate = null;
|
||||||
|
Set<String> supportingFilesToGenerate = null;
|
||||||
|
|
||||||
|
// allows generating only models by specifying a CSV of models to generate, or empty for all
|
||||||
|
if(System.getProperty("models") != null) {
|
||||||
|
String modelNames = System.getProperty("models");
|
||||||
|
generateModels = true;
|
||||||
|
if(!modelNames.isEmpty()) {
|
||||||
|
modelsToGenerate = new HashSet<String>(Arrays.asList(modelNames.split(",")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(System.getProperty("apis") != null) {
|
||||||
|
String apiNames = System.getProperty("apis");
|
||||||
|
generateApis = true;
|
||||||
|
if(!apiNames.isEmpty()) {
|
||||||
|
apisToGenerate = new HashSet<String>(Arrays.asList(apiNames.split(",")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(System.getProperty("supportingFiles") != null) {
|
||||||
|
String supportingFiles = System.getProperty("supportingFiles");
|
||||||
|
generateSupportingFiles = true;
|
||||||
|
if(!supportingFiles.isEmpty()) {
|
||||||
|
supportingFilesToGenerate = new HashSet<String>(Arrays.asList(supportingFiles.split(",")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(generateApis == null && generateModels == null && generateSupportingFiles == null) {
|
||||||
|
// no specifics are set, generate everything
|
||||||
|
generateApis = true; generateModels = true; generateSupportingFiles = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if(generateApis == null) {
|
||||||
|
generateApis = false;
|
||||||
|
}
|
||||||
|
if(generateModels == null) {
|
||||||
|
generateModels = false;
|
||||||
|
}
|
||||||
|
if(generateSupportingFiles == null) {
|
||||||
|
generateSupportingFiles = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (swagger == null || config == null) {
|
if (swagger == null || config == null) {
|
||||||
throw new RuntimeException("missing swagger input or config!");
|
throw new RuntimeException("missing swagger input or config!");
|
||||||
}
|
}
|
||||||
@ -139,10 +177,20 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
if (definitions != null) {
|
if (definitions != null) {
|
||||||
List<String> sortedModelKeys = sortModelsByInheritance(definitions);
|
List<String> sortedModelKeys = sortModelsByInheritance(definitions);
|
||||||
|
|
||||||
|
if(generateModels) {
|
||||||
|
if(modelsToGenerate != null && modelsToGenerate.size() > 0) {
|
||||||
|
List<String> updatedKeys = new ArrayList<String>();
|
||||||
|
for(String m : sortedModelKeys) {
|
||||||
|
if(modelsToGenerate.contains(m)) {
|
||||||
|
updatedKeys.add(m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sortedModelKeys = updatedKeys;
|
||||||
|
}
|
||||||
|
|
||||||
for (String name : sortedModelKeys) {
|
for (String name : sortedModelKeys) {
|
||||||
try {
|
try {
|
||||||
|
//don't generate models that have an import mapping
|
||||||
//dont generate models that have an import mapping
|
|
||||||
if(config.importMapping().containsKey(name)) {
|
if(config.importMapping().containsKey(name)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -180,6 +228,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (System.getProperty("debugModels") != null) {
|
if (System.getProperty("debugModels") != null) {
|
||||||
System.out.println("############ Model info ############");
|
System.out.println("############ Model info ############");
|
||||||
Json.prettyPrint(allModels);
|
Json.prettyPrint(allModels);
|
||||||
@ -187,6 +236,16 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
|
|
||||||
// apis
|
// apis
|
||||||
Map<String, List<CodegenOperation>> paths = processPaths(swagger.getPaths());
|
Map<String, List<CodegenOperation>> paths = processPaths(swagger.getPaths());
|
||||||
|
if(generateApis) {
|
||||||
|
if(apisToGenerate != null && apisToGenerate.size() > 0) {
|
||||||
|
Map<String, List<CodegenOperation>> updatedPaths = new TreeMap<String, List<CodegenOperation>>();
|
||||||
|
for(String m : paths.keySet()) {
|
||||||
|
if(apisToGenerate.contains(m)) {
|
||||||
|
updatedPaths.put(m, paths.get(m));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
paths = updatedPaths;
|
||||||
|
}
|
||||||
for (String tag : paths.keySet()) {
|
for (String tag : paths.keySet()) {
|
||||||
try {
|
try {
|
||||||
List<CodegenOperation> ops = paths.get(tag);
|
List<CodegenOperation> ops = paths.get(tag);
|
||||||
@ -237,6 +296,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
throw new RuntimeException("Could not generate api file for '" + tag + "'", e);
|
throw new RuntimeException("Could not generate api file for '" + tag + "'", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (System.getProperty("debugOperations") != null) {
|
if (System.getProperty("debugOperations") != null) {
|
||||||
System.out.println("############ Operation info ############");
|
System.out.println("############ Operation info ############");
|
||||||
Json.prettyPrint(allOperations);
|
Json.prettyPrint(allOperations);
|
||||||
@ -280,6 +340,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
Json.prettyPrint(bundle);
|
Json.prettyPrint(bundle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(generateSupportingFiles) {
|
||||||
for (SupportingFile support : config.supportingFiles()) {
|
for (SupportingFile support : config.supportingFiles()) {
|
||||||
try {
|
try {
|
||||||
String outputFolder = config.outputFolder();
|
String outputFolder = config.outputFolder();
|
||||||
@ -297,6 +358,16 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
|
|
||||||
String templateFile = getFullTemplateFile(config, support.templateFile);
|
String templateFile = getFullTemplateFile(config, support.templateFile);
|
||||||
|
|
||||||
|
boolean shouldGenerate = true;
|
||||||
|
if(supportingFilesToGenerate != null && supportingFilesToGenerate.size() > 0) {
|
||||||
|
if(supportingFilesToGenerate.contains(support.destinationFilename)) {
|
||||||
|
shouldGenerate = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
shouldGenerate = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(shouldGenerate) {
|
||||||
if (templateFile.endsWith("mustache")) {
|
if (templateFile.endsWith("mustache")) {
|
||||||
String template = readTemplate(templateFile);
|
String template = readTemplate(templateFile);
|
||||||
Template tmpl = Mustache.compiler()
|
Template tmpl = Mustache.compiler()
|
||||||
@ -338,10 +409,12 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
|
|
||||||
files.add(outputFile);
|
files.add(outputFile);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("Could not generate supporting file '" + support + "'", e);
|
throw new RuntimeException("Could not generate supporting file '" + support + "'", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
config.processSwagger(swagger);
|
config.processSwagger(swagger);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user