added configuration for swagger-yaml output file, #3597

This commit is contained in:
Tony Tam 2016-08-16 15:59:47 -07:00
parent 7a49f96a03
commit 7bc9c4041b
2 changed files with 21 additions and 10 deletions

View File

@ -1,27 +1,28 @@
package io.swagger.codegen.languages;
import java.io.File;
import io.swagger.codegen.*;
import io.swagger.models.Swagger;
import io.swagger.util.Yaml;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.SupportingFile;
import io.swagger.models.Swagger;
import io.swagger.util.Yaml;
import java.io.File;
public class SwaggerYamlGenerator extends DefaultCodegen implements CodegenConfig {
public static final String OUTPUT_NAME = "outputFile";
private static final Logger LOGGER = LoggerFactory.getLogger(SwaggerYamlGenerator.class);
protected String outputFile = "swagger.yaml";
public SwaggerYamlGenerator() {
super();
embeddedTemplateDir = templateDir = "swagger";
outputFolder = "generated-code/swagger";
cliOptions.add(new CliOption(OUTPUT_NAME, "output filename"));
supportingFiles.add(new SupportingFile("README.md", "", "README.md"));
}
@ -40,11 +41,20 @@ public class SwaggerYamlGenerator extends DefaultCodegen implements CodegenConfi
return "Creates a static swagger.yaml file.";
}
@Override
public void processOpts() {
super.processOpts();
if(additionalProperties.containsKey(OUTPUT_NAME)) {
this.outputFile = additionalProperties.get(OUTPUT_NAME).toString();
}
}
@Override
public void processSwagger(Swagger swagger) {
try {
String swaggerString = Yaml.mapper().writeValueAsString(swagger);
String outputFile = outputFolder + File.separator + "swagger.yaml";
String outputFile = outputFolder + File.separator + this.outputFile;
FileUtils.writeStringToFile(new File(outputFile), swaggerString);
LOGGER.debug("wrote file to " + outputFile);
} catch (Exception e) {

View File

@ -20,6 +20,7 @@ public class SwaggerYamlOptionsProvider implements OptionsProvider {
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
return builder.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE)
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
.put("outputFile", "swagger.yaml")
.build();
}