mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-03 22:20:56 +00:00
made selective generation of models
This commit is contained in:
parent
4723f51c63
commit
723cf9b823
@ -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,22 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<File> generate() {
|
public List<File> generate() {
|
||||||
|
boolean generateApis = true;
|
||||||
|
boolean generateModels = true;
|
||||||
|
boolean generateSupportingFiles = true;
|
||||||
|
|
||||||
|
Set<String> modelsToGenerate = null;
|
||||||
|
|
||||||
|
// allows generating only models by specifying a CSV of models to generate, or empty for all
|
||||||
|
if(System.getProperty("models") != null) {
|
||||||
|
generateApis = false;
|
||||||
|
generateSupportingFiles = false;
|
||||||
|
String modelNames = System.getProperty("models");
|
||||||
|
if(!modelNames.isEmpty()) {
|
||||||
|
modelsToGenerate = new HashSet<String>(Arrays.asList(modelNames.split(",")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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,28 +146,97 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
if (definitions != null) {
|
if (definitions != null) {
|
||||||
List<String> sortedModelKeys = sortModelsByInheritance(definitions);
|
List<String> sortedModelKeys = sortModelsByInheritance(definitions);
|
||||||
|
|
||||||
for (String name : sortedModelKeys) {
|
if(generateModels) {
|
||||||
try {
|
if(modelsToGenerate != null) {
|
||||||
|
List<String> updatedKeys = new ArrayList<String>();
|
||||||
//dont generate models that have an import mapping
|
for(String m : sortedModelKeys) {
|
||||||
if(config.importMapping().containsKey(name)) {
|
if(modelsToGenerate.contains(m)) {
|
||||||
continue;
|
updatedKeys.add(m);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
sortedModelKeys = updatedKeys;
|
||||||
|
}
|
||||||
|
|
||||||
Model model = definitions.get(name);
|
for (String name : sortedModelKeys) {
|
||||||
Map<String, Model> modelMap = new HashMap<String, Model>();
|
try {
|
||||||
modelMap.put(name, model);
|
//don't generate models that have an import mapping
|
||||||
Map<String, Object> models = processModels(config, modelMap, definitions);
|
if(config.importMapping().containsKey(name)) {
|
||||||
models.putAll(config.additionalProperties());
|
|
||||||
|
|
||||||
allModels.add(((List<Object>) models.get("models")).get(0));
|
|
||||||
|
|
||||||
for (String templateName : config.modelTemplateFiles().keySet()) {
|
|
||||||
String suffix = config.modelTemplateFiles().get(templateName);
|
|
||||||
String filename = config.modelFileFolder() + File.separator + config.toModelFilename(name) + suffix;
|
|
||||||
if (!config.shouldOverwrite(filename)) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Model model = definitions.get(name);
|
||||||
|
Map<String, Model> modelMap = new HashMap<String, Model>();
|
||||||
|
modelMap.put(name, model);
|
||||||
|
Map<String, Object> models = processModels(config, modelMap, definitions);
|
||||||
|
models.putAll(config.additionalProperties());
|
||||||
|
|
||||||
|
allModels.add(((List<Object>) models.get("models")).get(0));
|
||||||
|
|
||||||
|
for (String templateName : config.modelTemplateFiles().keySet()) {
|
||||||
|
String suffix = config.modelTemplateFiles().get(templateName);
|
||||||
|
String filename = config.modelFileFolder() + File.separator + config.toModelFilename(name) + suffix;
|
||||||
|
if (!config.shouldOverwrite(filename)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String templateFile = getFullTemplateFile(config, templateName);
|
||||||
|
String template = readTemplate(templateFile);
|
||||||
|
Template tmpl = Mustache.compiler()
|
||||||
|
.withLoader(new Mustache.TemplateLoader() {
|
||||||
|
@Override
|
||||||
|
public Reader getTemplate(String name) {
|
||||||
|
return getTemplateReader(config.templateDir() + File.separator + name + ".mustache");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.defaultValue("")
|
||||||
|
.compile(template);
|
||||||
|
writeToFile(filename, tmpl.execute(models));
|
||||||
|
files.add(new File(filename));
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Could not generate model '" + name + "'", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (System.getProperty("debugModels") != null) {
|
||||||
|
System.out.println("############ Model info ############");
|
||||||
|
Json.prettyPrint(allModels);
|
||||||
|
}
|
||||||
|
|
||||||
|
// apis
|
||||||
|
Map<String, List<CodegenOperation>> paths = processPaths(swagger.getPaths());
|
||||||
|
if(generateApis) {
|
||||||
|
for (String tag : paths.keySet()) {
|
||||||
|
try {
|
||||||
|
List<CodegenOperation> ops = paths.get(tag);
|
||||||
|
Map<String, Object> operation = processOperations(config, tag, ops);
|
||||||
|
|
||||||
|
operation.put("basePath", basePath);
|
||||||
|
operation.put("contextPath", contextPath);
|
||||||
|
operation.put("baseName", tag);
|
||||||
|
operation.put("modelPackage", config.modelPackage());
|
||||||
|
operation.putAll(config.additionalProperties());
|
||||||
|
operation.put("classname", config.toApiName(tag));
|
||||||
|
operation.put("classVarName", config.toApiVarName(tag));
|
||||||
|
operation.put("importPath", config.toApiImport(tag));
|
||||||
|
|
||||||
|
processMimeTypes(swagger.getConsumes(), operation, "consumes");
|
||||||
|
processMimeTypes(swagger.getProduces(), operation, "produces");
|
||||||
|
|
||||||
|
allOperations.add(new HashMap<String, Object>(operation));
|
||||||
|
for (int i = 0; i < allOperations.size(); i++) {
|
||||||
|
Map<String, Object> oo = (Map<String, Object>) allOperations.get(i);
|
||||||
|
if (i < (allOperations.size() - 1)) {
|
||||||
|
oo.put("hasMore", "true");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String templateName : config.apiTemplateFiles().keySet()) {
|
||||||
|
String filename = config.apiFilename(templateName, tag);
|
||||||
|
if (!config.shouldOverwrite(filename) && new File(filename).exists()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
String templateFile = getFullTemplateFile(config, templateName);
|
String templateFile = getFullTemplateFile(config, templateName);
|
||||||
String template = readTemplate(templateFile);
|
String template = readTemplate(templateFile);
|
||||||
Template tmpl = Mustache.compiler()
|
Template tmpl = Mustache.compiler()
|
||||||
@ -172,71 +248,15 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
})
|
})
|
||||||
.defaultValue("")
|
.defaultValue("")
|
||||||
.compile(template);
|
.compile(template);
|
||||||
writeToFile(filename, tmpl.execute(models));
|
|
||||||
|
writeToFile(filename, tmpl.execute(operation));
|
||||||
files.add(new File(filename));
|
files.add(new File(filename));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("Could not generate model '" + name + "'", e);
|
throw new RuntimeException("Could not generate api file for '" + tag + "'", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (System.getProperty("debugModels") != null) {
|
|
||||||
System.out.println("############ Model info ############");
|
|
||||||
Json.prettyPrint(allModels);
|
|
||||||
}
|
|
||||||
|
|
||||||
// apis
|
|
||||||
Map<String, List<CodegenOperation>> paths = processPaths(swagger.getPaths());
|
|
||||||
for (String tag : paths.keySet()) {
|
|
||||||
try {
|
|
||||||
List<CodegenOperation> ops = paths.get(tag);
|
|
||||||
Map<String, Object> operation = processOperations(config, tag, ops);
|
|
||||||
|
|
||||||
operation.put("basePath", basePath);
|
|
||||||
operation.put("contextPath", contextPath);
|
|
||||||
operation.put("baseName", tag);
|
|
||||||
operation.put("modelPackage", config.modelPackage());
|
|
||||||
operation.putAll(config.additionalProperties());
|
|
||||||
operation.put("classname", config.toApiName(tag));
|
|
||||||
operation.put("classVarName", config.toApiVarName(tag));
|
|
||||||
operation.put("importPath", config.toApiImport(tag));
|
|
||||||
|
|
||||||
processMimeTypes(swagger.getConsumes(), operation, "consumes");
|
|
||||||
processMimeTypes(swagger.getProduces(), operation, "produces");
|
|
||||||
|
|
||||||
allOperations.add(new HashMap<String, Object>(operation));
|
|
||||||
for (int i = 0; i < allOperations.size(); i++) {
|
|
||||||
Map<String, Object> oo = (Map<String, Object>) allOperations.get(i);
|
|
||||||
if (i < (allOperations.size() - 1)) {
|
|
||||||
oo.put("hasMore", "true");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (String templateName : config.apiTemplateFiles().keySet()) {
|
|
||||||
String filename = config.apiFilename(templateName, tag);
|
|
||||||
if (!config.shouldOverwrite(filename) && new File(filename).exists()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
String templateFile = getFullTemplateFile(config, templateName);
|
|
||||||
String template = readTemplate(templateFile);
|
|
||||||
Template tmpl = Mustache.compiler()
|
|
||||||
.withLoader(new Mustache.TemplateLoader() {
|
|
||||||
@Override
|
|
||||||
public Reader getTemplate(String name) {
|
|
||||||
return getTemplateReader(config.templateDir() + File.separator + name + ".mustache");
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.defaultValue("")
|
|
||||||
.compile(template);
|
|
||||||
|
|
||||||
writeToFile(filename, tmpl.execute(operation));
|
|
||||||
files.add(new File(filename));
|
|
||||||
}
|
|
||||||
} catch (Exception 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,66 +300,68 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
Json.prettyPrint(bundle);
|
Json.prettyPrint(bundle);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (SupportingFile support : config.supportingFiles()) {
|
if(generateSupportingFiles) {
|
||||||
try {
|
for (SupportingFile support : config.supportingFiles()) {
|
||||||
String outputFolder = config.outputFolder();
|
try {
|
||||||
if (isNotEmpty(support.folder)) {
|
String outputFolder = config.outputFolder();
|
||||||
outputFolder += File.separator + support.folder;
|
if (isNotEmpty(support.folder)) {
|
||||||
}
|
outputFolder += File.separator + support.folder;
|
||||||
File of = new File(outputFolder);
|
|
||||||
if (!of.isDirectory()) {
|
|
||||||
of.mkdirs();
|
|
||||||
}
|
|
||||||
String outputFilename = outputFolder + File.separator + support.destinationFilename;
|
|
||||||
if (!config.shouldOverwrite(outputFilename)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
String templateFile = getFullTemplateFile(config, support.templateFile);
|
|
||||||
|
|
||||||
if (templateFile.endsWith("mustache")) {
|
|
||||||
String template = readTemplate(templateFile);
|
|
||||||
Template tmpl = Mustache.compiler()
|
|
||||||
.withLoader(new Mustache.TemplateLoader() {
|
|
||||||
@Override
|
|
||||||
public Reader getTemplate(String name) {
|
|
||||||
return getTemplateReader(config.templateDir() + File.separator + name + ".mustache");
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.defaultValue("")
|
|
||||||
.compile(template);
|
|
||||||
|
|
||||||
writeToFile(outputFilename, tmpl.execute(bundle));
|
|
||||||
files.add(new File(outputFilename));
|
|
||||||
} else {
|
|
||||||
InputStream in = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
in = new FileInputStream(templateFile);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// continue
|
|
||||||
}
|
}
|
||||||
if (in == null) {
|
File of = new File(outputFolder);
|
||||||
in = this.getClass().getClassLoader().getResourceAsStream(getCPResourcePath(templateFile));
|
if (!of.isDirectory()) {
|
||||||
|
of.mkdirs();
|
||||||
}
|
}
|
||||||
File outputFile = new File(outputFilename);
|
String outputFilename = outputFolder + File.separator + support.destinationFilename;
|
||||||
OutputStream out = new FileOutputStream(outputFile, false);
|
if (!config.shouldOverwrite(outputFilename)) {
|
||||||
if (in != null && out != null) {
|
continue;
|
||||||
System.out.println("writing file " + outputFile);
|
}
|
||||||
IOUtils.copy(in, out);
|
|
||||||
|
String templateFile = getFullTemplateFile(config, support.templateFile);
|
||||||
|
|
||||||
|
if (templateFile.endsWith("mustache")) {
|
||||||
|
String template = readTemplate(templateFile);
|
||||||
|
Template tmpl = Mustache.compiler()
|
||||||
|
.withLoader(new Mustache.TemplateLoader() {
|
||||||
|
@Override
|
||||||
|
public Reader getTemplate(String name) {
|
||||||
|
return getTemplateReader(config.templateDir() + File.separator + name + ".mustache");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.defaultValue("")
|
||||||
|
.compile(template);
|
||||||
|
|
||||||
|
writeToFile(outputFilename, tmpl.execute(bundle));
|
||||||
|
files.add(new File(outputFilename));
|
||||||
} else {
|
} else {
|
||||||
if (in == null) {
|
InputStream in = null;
|
||||||
System.out.println("can't open " + templateFile + " for input");
|
|
||||||
}
|
|
||||||
if (out == null) {
|
|
||||||
System.out.println("can't open " + outputFile + " for output");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
files.add(outputFile);
|
try {
|
||||||
|
in = new FileInputStream(templateFile);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// continue
|
||||||
|
}
|
||||||
|
if (in == null) {
|
||||||
|
in = this.getClass().getClassLoader().getResourceAsStream(getCPResourcePath(templateFile));
|
||||||
|
}
|
||||||
|
File outputFile = new File(outputFilename);
|
||||||
|
OutputStream out = new FileOutputStream(outputFile, false);
|
||||||
|
if (in != null && out != null) {
|
||||||
|
System.out.println("writing file " + outputFile);
|
||||||
|
IOUtils.copy(in, out);
|
||||||
|
} else {
|
||||||
|
if (in == null) {
|
||||||
|
System.out.println("can't open " + templateFile + " for input");
|
||||||
|
}
|
||||||
|
if (out == null) {
|
||||||
|
System.out.println("can't open " + outputFile + " for output");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
files.add(outputFile);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Could not generate supporting file '" + support + "'", e);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException("Could not generate supporting file '" + support + "'", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user