Skip to content

Commit 2e0b730

Browse files
authored
Merge pull request #2 from swagger-api/master
updatingSelf
2 parents 2864125 + 56fe33b commit 2e0b730

File tree

27 files changed

+501
-71
lines changed

27 files changed

+501
-71
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ docker run --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli generate \
229229
-o /local/out/go
230230
```
231231

232+
(On Windows replace `${PWD}` with `%CD%`)
233+
232234
The generated code will be located under `./out/go` in the current directory.
233235

234236
## Getting Started

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class CodegenOperation {
1818
isResponseBinary = false, isResponseFile = false, hasReference = false,
1919
isRestfulIndex, isRestfulShow, isRestfulCreate, isRestfulUpdate, isRestfulDestroy,
2020
isRestful, isDeprecated;
21-
public String path, operationId, returnType, httpMethod, returnBaseType,
21+
public String path, testPath, operationId, returnType, httpMethod, returnBaseType,
2222
returnContainer, summary, unescapedNotes, notes, baseName, defaultResponse, discriminator;
2323
public List<Map<String, String>> consumes, produces, prioritizedContentTypes;
2424
public CodegenParameter bodyParam;

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenParameter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class CodegenParameter {
1313
collectionFormat, description, unescapedDescription, baseType, defaultValue, enumName;
1414

1515
public String example; // example value (x-example)
16+
public String testExample;
1617
public String jsonSchema;
1718
public boolean isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isByteArray, isBinary, isBoolean, isDate, isDateTime, isUuid;
1819
public boolean isListContainer, isMapContainer;
@@ -116,6 +117,7 @@ public CodegenParameter copy() {
116117
output.jsonSchema = this.jsonSchema;
117118
output.defaultValue = this.defaultValue;
118119
output.example = this.example;
120+
output.testExample = this.testExample;
119121
output.isEnum = this.isEnum;
120122
if (this._enum != null) {
121123
output._enum = new ArrayList<String>(this._enum);

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,8 @@ public int compare(CodegenParameter one, CodegenParameter another) {
23322332
op.isRestfulDestroy = op.isRestfulDestroy();
23332333
op.isRestful = op.isRestful();
23342334

2335+
configureDataForTestTemplate(op);
2336+
23352337
return op;
23362338
}
23372339

@@ -3846,4 +3848,54 @@ protected void addSwitch(String key, String description, Boolean defaultValue) {
38463848
option.defaultValue(defaultValue.toString());
38473849
cliOptions.add(option);
38483850
}
3851+
3852+
protected void configureDataForTestTemplate(CodegenOperation codegenOperation) {
3853+
final String httpMethod = codegenOperation.httpMethod;
3854+
String path = codegenOperation.path;
3855+
if ("GET".equalsIgnoreCase(httpMethod)) {
3856+
codegenOperation.vendorExtensions.put("x-is-get-method", Boolean.TRUE);
3857+
}
3858+
if ("POST".equalsIgnoreCase(httpMethod)) {
3859+
codegenOperation.vendorExtensions.put("x-is-post-method", Boolean.TRUE);
3860+
}
3861+
if ("PUT".equalsIgnoreCase(httpMethod)) {
3862+
codegenOperation.vendorExtensions.put("x-is-put-method", Boolean.TRUE);
3863+
}
3864+
if ("DELETE".equalsIgnoreCase(httpMethod)) {
3865+
codegenOperation.vendorExtensions.put("x-is-delete-method", Boolean.TRUE);
3866+
}
3867+
if ("HEAD".equalsIgnoreCase(httpMethod)) {
3868+
codegenOperation.vendorExtensions.put("x-is-head-method", Boolean.TRUE);
3869+
}
3870+
if ("TRACE".equalsIgnoreCase(httpMethod)) {
3871+
codegenOperation.vendorExtensions.put("x-is-trace-method", Boolean.TRUE);
3872+
}
3873+
if ("PATCH".equalsIgnoreCase(httpMethod)) {
3874+
codegenOperation.vendorExtensions.put("x-is-head-method", Boolean.TRUE);
3875+
}
3876+
if ("OPTIONS".equalsIgnoreCase(httpMethod)) {
3877+
codegenOperation.vendorExtensions.put("x-is-options-method", Boolean.TRUE);
3878+
}
3879+
if (path.contains("{")) {
3880+
while(path.contains("{")) {
3881+
final String pathParam = path.substring(path.indexOf("{"), path.indexOf("}") + 1);
3882+
final String paramName = pathParam.replace("{", StringUtils.EMPTY).replace("}", StringUtils.EMPTY);
3883+
3884+
CodegenParameter codegenParameter = null;
3885+
3886+
for (CodegenParameter parameter : codegenOperation.pathParams) {
3887+
if (parameter.baseName.equalsIgnoreCase(paramName)) {
3888+
codegenParameter = parameter;
3889+
break;
3890+
}
3891+
}
3892+
3893+
if (codegenParameter == null || codegenParameter.testExample == null) {
3894+
return;
3895+
}
3896+
path = path.replace(pathParam, codegenParameter.testExample);
3897+
}
3898+
}
3899+
codegenOperation.testPath = path;
3900+
}
38493901
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ public void setParameterExampleValue(CodegenParameter p) {
779779
if (example == null) {
780780
example = p.paramName + "_example";
781781
}
782+
p.testExample = example;
782783
example = "\"" + escapeText(example) + "\"";
783784
} else if ("Integer".equals(type) || "Short".equals(type)) {
784785
if (example == null) {
@@ -788,15 +789,18 @@ public void setParameterExampleValue(CodegenParameter p) {
788789
if (example == null) {
789790
example = "56";
790791
}
792+
p.testExample = example;
791793
example = example + "L";
792794
} else if ("Float".equals(type)) {
793795
if (example == null) {
794796
example = "3.4";
795797
}
798+
p.testExample = example;
796799
example = example + "F";
797800
} else if ("Double".equals(type)) {
798801
example = "3.4";
799802
example = example + "D";
803+
p.testExample = example;
800804
} else if ("Boolean".equals(type)) {
801805
if (example == null) {
802806
example = "true";
@@ -813,6 +817,10 @@ public void setParameterExampleValue(CodegenParameter p) {
813817
example = "new " + type + "()";
814818
}
815819

820+
if (p.testExample == null) {
821+
p.testExample = example;
822+
}
823+
816824
if (example == null) {
817825
example = "null";
818826
} else if (Boolean.TRUE.equals(p.isListContainer)) {

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public class SpringCodegen extends AbstractJavaCodegen
5353
public SpringCodegen() {
5454
super();
5555
outputFolder = "generated-code/javaSpring";
56-
apiTestTemplateFiles.clear(); // TODO: add test template
5756
embeddedTemplateDir = templateDir = "JavaSpring";
5857
apiPackage = "io.swagger.api";
5958
modelPackage = "io.swagger.model";
@@ -254,6 +253,7 @@ public void processOpts() {
254253
additionalProperties.put(SINGLE_CONTENT_TYPES, "true");
255254
this.setSingleContentTypes(true);
256255
}
256+
apiTestTemplateFiles.clear();
257257
} else {
258258
apiTemplateFiles.put("apiController.mustache", "Controller.java");
259259
supportingFiles.add(new SupportingFile("apiException.mustache",
@@ -557,6 +557,14 @@ public String toApiName(String name) {
557557
return camelize(name) + "Api";
558558
}
559559

560+
@Override
561+
public String toApiTestFilename(String name) {
562+
if(library.equals(SPRING_MVC_LIBRARY)) {
563+
return toApiName(name) + "ControllerIT";
564+
}
565+
return toApiName(name) + "ControllerIntegrationTest";
566+
}
567+
560568
@Override
561569
public void setParameterExampleValue(CodegenParameter p) {
562570
String type = p.baseType;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package {{package}};
2+
{{#imports}}import {{import}};
3+
{{/imports}}
4+
import java.util.*;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
11+
import org.springframework.http.HttpStatus;
12+
import org.springframework.http.ResponseEntity;
13+
14+
import static org.junit.Assert.assertEquals;
15+
16+
@RunWith(SpringRunner.class)
17+
@SpringBootTest
18+
public class {{classname}}ControllerIntegrationTest {
19+
@Autowired
20+
private {{classname}} api;
21+
{{#operations}}
22+
{{#operation}}
23+
@Test
24+
public void {{operationId}}Test() throws Exception {
25+
{{#allParams}}
26+
{{^isFile}}
27+
{{{dataType}}} {{paramName}} = {{{example}}};
28+
{{/isFile}}
29+
{{#isFile}}
30+
org.springframework.web.multipart.MultipartFile {{paramName}} = null;
31+
{{/isFile}}
32+
{{/allParams}}
33+
ResponseEntity<{{>returnTypes}}> responseEntity = api.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
34+
assertEquals(HttpStatus.NOT_IMPLEMENTED, responseEntity.getStatusCode());
35+
}
36+
{{/operation}}
37+
{{/operations}}
38+
}

modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-boot/pom.mustache

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@
9191
<groupId>javax.validation</groupId>
9292
<artifactId>validation-api</artifactId>
9393
</dependency>
94-
{{/useBeanValidation}}
94+
{{/useBeanValidation}}
95+
<dependency>
96+
<groupId>org.springframework.boot</groupId>
97+
<artifactId>spring-boot-starter-test</artifactId>
98+
<scope>test</scope>
99+
</dependency>
95100
</dependencies>
96101
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package {{package}};
2+
{{#imports}}import {{import}};
3+
{{/imports}}
4+
import java.util.*;
5+
import org.apache.http.HttpResponse;
6+
import org.apache.http.client.HttpClient;
7+
import org.apache.http.client.methods.HttpGet;
8+
import org.apache.http.impl.client.HttpClientBuilder;
9+
import org.testng.annotations.Test;
10+
import static org.junit.Assert.assertEquals;
11+
/**
12+
* Test class to verify that GET endpoints on generated project are reached.
13+
*/
14+
public class {{classname}}ControllerIT {
15+
16+
{{#operations}}
17+
{{#operation}}
18+
{{#vendorExtensions.x-is-get-method}}
19+
@Test
20+
public void {{operationId}}Test() throws Exception {
21+
final String requestURL = "http://localhost:8002{{^contextPath}}/{{/contextPath}}{{#contextPath}}{{contextPath}}{{/contextPath}}{{testPath}}{{#hasQueryParams}}?{{/hasQueryParams}}{{#queryParams}}{{paramName}}={{testExample}}{{#hasMore}}&{{/hasMore}}{{/queryParams}}";
22+
final HttpClient client = HttpClientBuilder.create().build();
23+
final HttpResponse response = client.execute(new HttpGet(requestURL));
24+
assertEquals(response.getStatusLine().getStatusCode(), 501);
25+
}
26+
{{/vendorExtensions.x-is-get-method}}
27+
{{/operation}}
28+
{{/operations}}
29+
}

modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-mvc/pom.mustache

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,33 @@
160160
<version>1.1.0.Final</version>
161161
<scope>provided</scope>
162162
</dependency>
163-
{{/useBeanValidation}}
163+
{{/useBeanValidation}}
164+
<dependency>
165+
<groupId>org.testng</groupId>
166+
<artifactId>testng</artifactId>
167+
<version>6.8.8</version>
168+
<scope>test</scope>
169+
<exclusions>
170+
<exclusion>
171+
<artifactId>junit</artifactId>
172+
<groupId>junit</groupId>
173+
</exclusion>
174+
<exclusion>
175+
<artifactId>snakeyaml</artifactId>
176+
<groupId>org.yaml</groupId>
177+
</exclusion>
178+
<exclusion>
179+
<artifactId>bsh</artifactId>
180+
<groupId>org.beanshell</groupId>
181+
</exclusion>
182+
</exclusions>
183+
</dependency>
184+
<dependency>
185+
<groupId>org.apache.httpcomponents</groupId>
186+
<artifactId>httpclient</artifactId>
187+
<version>4.5.2</version>
188+
<scope>test</scope>
189+
</dependency>
164190
</dependencies>
165191
<properties>
166192
<java.version>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</java.version>

0 commit comments

Comments
 (0)