Skip to content

Commit 25cc058

Browse files
committed
implement /model endpoint returning intermediate model
1 parent d6d68bf commit 25cc058

File tree

6 files changed

+155
-0
lines changed

6 files changed

+155
-0
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/AbstractGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,5 @@ public String getCPResourcePath(String name) {
144144
}
145145
return name;
146146
}
147+
147148
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/DefaultGenerator.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,11 +792,40 @@ public List<File> generate() {
792792

793793
// supporting files
794794
Map<String, Object> bundle = buildSupportFileBundle(allOperations, allModels);
795+
Json.prettyPrint(bundle);
795796
generateSupportingFiles(files, bundle);
796797
config.processOpenAPI(openAPI);
797798
return files;
798799
}
799800

801+
@Override
802+
public Map<String, Object> generateBundle() {
803+
804+
if (openAPI == null) {
805+
throw new RuntimeException("missing OpenAPI input!");
806+
}
807+
if (config == null) {
808+
throw new RuntimeException("missing configuration input!");
809+
}
810+
configureGeneratorProperties();
811+
configureSwaggerInfo();
812+
813+
List<File> files = new ArrayList<>();
814+
// models
815+
List<Object> allModels = new ArrayList<>();
816+
generateModels(files, allModels);
817+
// apis
818+
List<Object> allOperations = new ArrayList<>();
819+
generateApis(files, allOperations, allModels);
820+
821+
// supporting files
822+
Map<String, Object> bundle = buildSupportFileBundle(allOperations, allModels);
823+
Json.prettyPrint(bundle);
824+
generateSupportingFiles(files, bundle);
825+
config.processOpenAPI(openAPI);
826+
return bundle;
827+
}
828+
800829
private File processTemplateToFile(Map<String, Object> templateData, String templateName, String outputFilename) throws IOException {
801830
String adjustedOutputFilename = outputFilename.replaceAll("//", "/").replace('/', File.separatorChar);
802831
if(ignoreProcessor.allowsFile(new File(adjustedOutputFilename))) {

modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/Generator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import java.io.File;
44
import java.util.List;
5+
import java.util.Map;
56

67
public interface Generator {
78
Generator opts(ClientOptInput opts);
89

910
List<File> generate();
11+
Map<String, Object> generateBundle();
12+
1013
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/service/GeneratorService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.io.File;
1010
import java.util.List;
11+
import java.util.Map;
1112

1213
public class GeneratorService {
1314
protected final Logger LOGGER = LoggerFactory.getLogger(GeneratorService.class);
@@ -52,4 +53,11 @@ public List<File> generate() {
5253
throw new RuntimeException("missing opts input");
5354
}
5455

56+
public Map<String, Object> generateBundle() {
57+
if (optsV3 != null) {
58+
return new DefaultGenerator().opts(optsV3).generateBundle();
59+
}
60+
throw new RuntimeException("missing opts input");
61+
}
62+
5563
}

modules/swagger-generator/src/main/java/io/swagger/v3/generator/online/GeneratorController.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,58 @@ public ResponseContext generate(RequestContext context, GenerationRequest genera
484484

485485
}
486486

487+
public ResponseContext generateBundle(RequestContext context, GenerationRequest generationRequest) {
488+
489+
String requestLog = requestLog(generationRequest);
490+
LOGGER.debug("generate start - " + requestLog);
491+
File outputRootFolder = getTmpFolder();
492+
String destPath = null;
493+
494+
if(generationRequest != null && generationRequest.getOptions() != null) {
495+
Object destPathObj = generationRequest.getOptions().getAdditionalProperties().get("outputFolder");
496+
if (destPathObj != null && destPathObj instanceof String) {
497+
destPath = (String)destPathObj;
498+
}
499+
}
500+
if(destPath == null) {
501+
destPath = "";
502+
}
503+
504+
// remove double slashes
505+
destPath.replaceAll("//", "/");
506+
507+
if(destPath.indexOf("..") != -1) {
508+
return new ResponseContext()
509+
.status(400)
510+
.contentType(MediaType.TEXT_PLAIN)
511+
.entity("Illegal output folder");
512+
}
513+
514+
// remove leading slash (will typically not hurt)
515+
if(destPath.indexOf("/") == 0 && destPath.length() > 1) {
516+
destPath = destPath.substring(1);
517+
}
518+
519+
// destPath is where the files are written, relative to output folder
520+
LOGGER.info("using destination path " + destPath);
521+
522+
File outputContentFolder = null;
523+
if (!StringUtils.isBlank(destPath.trim())) {
524+
outputContentFolder = new File(outputRootFolder, destPath);
525+
} else {
526+
outputContentFolder = outputRootFolder;
527+
}
528+
generationRequest.getOptions().setOutputDir(outputContentFolder.getAbsolutePath());
529+
File outputFile = new File(getTmpFolder(), generationRequest.getLang() + "-bundle.zip");
530+
531+
LOGGER.info("file zip file: " + outputFile.getAbsolutePath());
532+
533+
ResponseContext responseContext = generateBundle(generationRequest, outputRootFolder, outputContentFolder, outputFile);
534+
LOGGER.debug("generate end - " + requestLog);
535+
return responseContext;
536+
537+
}
538+
487539
protected static File getTmpFolder() {
488540
try {
489541
File outputFolder = Files.createTempDirectory("codegen-").toFile();
@@ -557,6 +609,45 @@ private ResponseContext generate(GenerationRequest generationRequest, File outpu
557609

558610

559611
}
612+
613+
private ResponseContext generateBundle(GenerationRequest generationRequest, File outputRootFolder, File outputContentFolder, File outputFile) {
614+
GeneratorService generatorService = new GeneratorService();
615+
try {
616+
generatorService.generationRequest(generationRequest);
617+
} catch (Exception e) {
618+
String msg = "Error processing generation request: " + e.getMessage();
619+
LOGGER.error(msg, e);
620+
return new ResponseContext()
621+
.status(400)
622+
.contentType(MediaType.TEXT_PLAIN)
623+
.entity(msg);
624+
}
625+
626+
// final List<File> files;
627+
final Map<String, Object> files;
628+
try {
629+
files = generatorService.generateBundle();
630+
} catch (Exception e) {
631+
String msg = String.format("Error generating `%s` code : %s", generationRequest.getLang(), e.getMessage());
632+
LOGGER.error(msg, e);
633+
return new ResponseContext()
634+
.status(500)
635+
.contentType(MediaType.TEXT_PLAIN)
636+
.entity(msg);
637+
}
638+
if (files.size() > 0) {
639+
return new ResponseContext()
640+
.status(200)
641+
.contentType(MediaType.APPLICATION_JSON_TYPE)
642+
.entity(files);
643+
} else {
644+
return new ResponseContext()
645+
.status(500)
646+
.contentType(MediaType.TEXT_PLAIN)
647+
.entity("A target generation was attempted, but no files were created");
648+
}
649+
}
650+
560651
private ResponseContext downloadFile(File outputRootFolder, File outputContentFolder, File outputFile, String lang, GenerationRequest.Type type) {
561652

562653
final ZipUtil zipUtil = new ZipUtil();

modules/swagger-generator/src/main/resources/openapi.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,29 @@ tags:
1515
- name: documentation
1616
- name: config
1717
paths:
18+
"/model":
19+
post:
20+
tags:
21+
- clients
22+
- servers
23+
- documentation
24+
- config
25+
x-swagger-router-controller: io.swagger.v3.generator.online.GeneratorController
26+
operationId: generateBundle
27+
summary: Generates the intermediate model ("bundle") and returns it as a JSON.
28+
body.
29+
requestBody:
30+
content:
31+
application/json:
32+
schema:
33+
"$ref": "#/components/schemas/GenerationRequest"
34+
responses:
35+
'200':
36+
description: successful operation
37+
content:
38+
application/json:
39+
schema:
40+
type: object
1841
"/generate":
1942
get:
2043
tags:

0 commit comments

Comments
 (0)