Skip to content

Commit

Permalink
Merge pull request #9337 from swagger-api/swos-95
Browse files Browse the repository at this point in the history
fixes imports when spec contains no schema
  • Loading branch information
frantuma authored Apr 3, 2019
2 parents 56c6401 + f5440a9 commit a5b6510
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,11 @@ private void generateApis(List<File> files, List<Object> allOperations, List<Obj
if (!generateApis) {
return;
}
boolean hasModel = true;
if (allModels == null || allModels.isEmpty()) {
hasModel = false;
}

Map<String, List<CodegenOperation>> paths = processPaths(this.openAPI.getPaths());
Set<String> apisToGenerate = null;
String apiNames = System.getProperty("apis");
Expand Down Expand Up @@ -464,6 +469,9 @@ public int compare(CodegenOperation one, CodegenOperation another) {
}
operation.put("sortParamsByRequiredFlag", sortParamsByRequiredFlag);

operation.put("hasModel", hasModel);


allOperations.add(new HashMap<>(operation));
for (int i = 0; i < allOperations.size(); i++) {
Map<String, Object> oo = (Map<String, Object>) allOperations.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.testng.Assert;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -33,6 +34,34 @@ public void testGeneratorServiceJava3() {
Assert.assertFalse(files.isEmpty());
}

@Test
public void testNoModel() throws Exception{

String path = getTmpFolder().getAbsolutePath();
GenerationRequest request = new GenerationRequest();
request
.codegenVersion(GenerationRequest.CodegenVersion.V3)
.type(GenerationRequest.Type.SERVER)
.lang("jaxrs-jersey")
.spec(loadSpecAsNode("3_0_0/noModel.yaml", true, false))
.options(
new Options()
.outputDir(path)
);
List<File> files = new GeneratorService().generationRequest(request).generate();
System.out.println(path);
Assert.assertFalse(files.isEmpty());
for (File f: files) {
String relPath = f.getAbsolutePath().substring(path.length());
if ("/src/main/java/io/swagger/api/impl/FooApiServiceImpl.java".equals(relPath) ||
"/src/gen/java/io/swagger/api/FooApiService.java".equals(relPath) ||
"/src/gen/java/io/swagger/api/FooApi.java".equals(relPath)) {
Assert.assertFalse(FileUtils.readFileToString(f).contains("import io.swagger.model"));
}
}

}

@Test(description = "test generator service with java 2.0")
public void testGeneratorServiceJava2() {

Expand Down
16 changes: 16 additions & 0 deletions modules/swagger-codegen/src/test/resources/3_0_0/noModel.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
openapi: 3.0.0
info:
title: Sample API
version: '1'
servers:
- url: 'http://localhost:9080/1'
paths:
/foo:
get:
responses:
'200':
description: OK
content:
application/json:
schema:
type: object

0 comments on commit a5b6510

Please # to comment.