Skip to content
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 @@ -35,6 +35,7 @@
import java.util.Map;

import io.swagger.codegen.v3.CodegenArgument;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand Down Expand Up @@ -434,7 +435,11 @@ protected void execute_() throws MojoExecutionException {
}

if (null != generateSupportingFiles && generateSupportingFiles) {
System.setProperty(CodegenConstants.SUPPORTING_FILES, supportingFilesToGenerate);
if (StringUtils.isBlank(supportingFilesToGenerate)) {
System.setProperty(CodegenConstants.SUPPORTING_FILES, "true");
} else {
System.setProperty(CodegenConstants.SUPPORTING_FILES, supportingFilesToGenerate);
}
} else {
System.clearProperty(CodegenConstants.SUPPORTING_FILES);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public interface CodegenConfig {

List<SupportingFile> supportingFiles();

List<SupportingFile> configFiles();

String getInputSpec();

void setInputSpec(String inputSpec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,65 @@ private void generateSupportingFiles(List<File> files, Map<String, Object> bundl

}

private void generateConfigFiles(List<File> files, Map<String, Object> bundle) {
for (SupportingFile support : config.configFiles()) {
try {
String outputFolder = config.outputFolder();
if (StringUtils.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.replace('/', File.separatorChar);
if (!config.shouldOverwrite(outputFilename)) {
LOGGER.info("Skipped overwriting " + outputFilename);
continue;
}
String templateFile;
if( support instanceof GlobalSupportingFile) {
templateFile = config.getCommonTemplateDir() + File.separator + support.templateFile;
} else {
templateFile = getFullTemplateFile(config, support.templateFile);
}

if(ignoreProcessor.allowsFile(new File(outputFilename))) {
if (templateFile.endsWith("mustache")) {
String rendered = templateEngine.getRendered(templateFile, bundle);
writeToFile(outputFilename, rendered);
files.add(new File(outputFilename));
} else {
InputStream in = null;

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) {
LOGGER.info("writing file " + outputFile);
IOUtils.copy(in, out);
out.close();
} else {
LOGGER.warn("can't open " + templateFile + " for input");
}
files.add(outputFile);
}
} else {
LOGGER.info("Skipped generation of " + outputFilename + " due to rule in .swagger-codegen-ignore");
}
} catch (Exception e) {
throw new RuntimeException("Could not generate config file '" + support + "'", e);
}
}
}

private Map<String, Object> buildSupportFileBundle(List<Object> allOperations, List<Object> allModels) {

Map<String, Object> bundle = new HashMap<>();
Expand Down Expand Up @@ -795,8 +854,9 @@ public List<File> generate() {
List<Object> allOperations = new ArrayList<>();
generateApis(files, allOperations, allModels);

// supporting files
// supporting and config files
Map<String, Object> bundle = buildSupportFileBundle(allOperations, allModels);
generateConfigFiles(files, bundle);
generateSupportingFiles(files, bundle);
config.processOpenAPI(openAPI);
return files;
Expand Down Expand Up @@ -840,7 +900,7 @@ public Map<String, Object> generateBundle() {

// supporting files
Map<String, Object> bundle = buildSupportFileBundle(allOperations, allModels);
Json.prettyPrint(bundle);
generateConfigFiles(files, bundle);
generateSupportingFiles(files, bundle);
config.processOpenAPI(openAPI);
return bundle;
Expand Down
Loading