diff --git a/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/v3/maven/plugin/CodeGenMojo.java b/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/v3/maven/plugin/CodeGenMojo.java index a46f1c46670..6189d02ecab 100644 --- a/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/v3/maven/plugin/CodeGenMojo.java +++ b/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/v3/maven/plugin/CodeGenMojo.java @@ -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; @@ -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); } diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/CodegenConfig.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/CodegenConfig.java index 01b4e4f4f8c..3754b1da4a3 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/CodegenConfig.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/CodegenConfig.java @@ -83,6 +83,8 @@ public interface CodegenConfig { List supportingFiles(); + List configFiles(); + String getInputSpec(); void setInputSpec(String inputSpec); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/DefaultGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/DefaultGenerator.java index 282a28c79b1..94acb4bcbac 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/DefaultGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/DefaultGenerator.java @@ -725,6 +725,65 @@ private void generateSupportingFiles(List files, Map bundl } + private void generateConfigFiles(List files, Map 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 buildSupportFileBundle(List allOperations, List allModels) { Map bundle = new HashMap<>(); @@ -795,8 +854,9 @@ public List generate() { List allOperations = new ArrayList<>(); generateApis(files, allOperations, allModels); - // supporting files + // supporting and config files Map bundle = buildSupportFileBundle(allOperations, allModels); + generateConfigFiles(files, bundle); generateSupportingFiles(files, bundle); config.processOpenAPI(openAPI); return files; @@ -840,7 +900,7 @@ public Map generateBundle() { // supporting files Map bundle = buildSupportFileBundle(allOperations, allModels); - Json.prettyPrint(bundle); + generateConfigFiles(files, bundle); generateSupportingFiles(files, bundle); config.processOpenAPI(openAPI); return bundle;