forked from loafle/openapi-generator-original
added slf4j, updated server gen logic
This commit is contained in:
parent
39487d9917
commit
c832889db8
@ -11,12 +11,18 @@ import com.wordnik.swagger.generator.util.ZipUtil;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Generator {
|
||||
static Logger LOGGER = LoggerFactory.getLogger(Generator.class);
|
||||
|
||||
public static String generateClient(String language, GeneratorInput opts) throws ApiException {
|
||||
LOGGER.debug("generate client for " + language);
|
||||
if(opts == null) {
|
||||
throw new BadRequestException(400, "No options were supplied");
|
||||
}
|
||||
@ -67,8 +73,56 @@ public class Generator {
|
||||
return outputFilename;
|
||||
}
|
||||
|
||||
public static String generateServer(String language, GeneratorInput opts) {
|
||||
return "";
|
||||
public static String generateServer(String language, GeneratorInput opts) throws ApiException {
|
||||
LOGGER.debug("generate server for " + language);
|
||||
if(opts == null) {
|
||||
throw new BadRequestException(400, "No options were supplied");
|
||||
}
|
||||
JsonNode node = opts.getSpec();
|
||||
if(node == null) {
|
||||
throw new BadRequestException(400, "No swagger specification was supplied");
|
||||
}
|
||||
Swagger swagger = new SwaggerParser().read(node);
|
||||
if(swagger == null) {
|
||||
throw new BadRequestException(400, "The swagger specification supplied was not valid");
|
||||
}
|
||||
|
||||
ClientOptInput clientOptInput = new ClientOptInput();
|
||||
ClientOpts clientOpts = new ClientOpts();
|
||||
String outputFolder = getTmpFolder().getAbsolutePath() + File.separator + language + "-server";
|
||||
String outputFilename = outputFolder + "-bundle.zip";
|
||||
|
||||
clientOptInput
|
||||
.opts(clientOpts)
|
||||
.swagger(swagger);
|
||||
|
||||
CodegenConfig codegenConfig = Codegen.getConfig(language);
|
||||
if(codegenConfig == null) {
|
||||
throw new BadRequestException(400, "Unsupported target " + language + " supplied");
|
||||
}
|
||||
|
||||
codegenConfig.setOutputDir(outputFolder);
|
||||
|
||||
Json.prettyPrint(clientOpts);
|
||||
|
||||
clientOptInput.setConfig(codegenConfig);
|
||||
|
||||
try{
|
||||
List<File> files = new Codegen().opts(clientOptInput).generate();
|
||||
if(files.size() > 0) {
|
||||
List<File> filesToAdd = new ArrayList<File>();
|
||||
filesToAdd.add(new File(outputFolder));
|
||||
ZipUtil zip = new ZipUtil();
|
||||
zip.compressFiles(filesToAdd, outputFilename);
|
||||
}
|
||||
else {
|
||||
throw new BadRequestException(400, "A target generation was attempted, but no files were created!");
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new BadRequestException(500, "Unable to build target: " + e.getMessage());
|
||||
}
|
||||
return outputFilename;
|
||||
}
|
||||
|
||||
public static InputOption clientOptions(String language) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user