Skip to content

Commit a270bbc

Browse files
committed
Rest-assured http client has been added
ApiClient has been added @deprecated has been added for operation {{{returnType}}} has been fixed build.gradle.mustache, build.sbt.mustache, api_doc_mustache has been added Samples has been added for rest-assured Useless supporting files has been removed for rest-assured Sample has been added for rest-assured
1 parent a3c9775 commit a270bbc

File tree

112 files changed

+11649
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+11649
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ CONFIG OPTIONS
530530
retrofit - HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1 (Retrofit 1.9.0)
531531
retrofit2 - HTTP client: OkHttp 2.5.0. JSON processing: Gson 2.4 (Retrofit 2.0.0-beta2)
532532
google-api-client - HTTP client: google-api-client 1.23.0. JSON processing: Jackson 2.8.9
533+
rest-assured", "HTTP client: rest-assured : 3.0.6. JSON processing: JSON processing: Gson 2.6.1
533534
```
534535

535536
Your config file for Java can look like

bin/java-petstore-all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
./bin/java-petstore-resttemplate-withxml.sh
1818
./bin/java-petstore-resteasy.sh
1919
./bin/java-petstore-google-api-client.sh
20+
./bin/java-petstore-rest-assured.sh
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"library": "rest-assured",
3+
"artifactId": "swagger-petstore-rest-assured"
4+
}

bin/java-petstore-rest-assured.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
ags="$@ generate -t modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l java -c bin/java-petstore-rest-assured.json -o samples/client/petstore/java/rest-assured -DhideGenerationTimestamp=true"
30+
31+
echo "Removing files and folders under samples/client/petstore/java/rest-assured/src/main"
32+
rm -rf samples/client/petstore/java/rest-assured/src/main
33+
find samples/client/petstore/java/rest-assured -maxdepth 1 -type f ! -name "README.md" -exec rm {} +
34+
java $JAVA_OPTS -jar $executable $ags
35+

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
3939

4040
public static final String RETROFIT_1 = "retrofit";
4141
public static final String RETROFIT_2 = "retrofit2";
42+
public static final String REST_ASSURED = "rest-assured";
4243

4344
protected String gradleWrapperPackage = "gradle.wrapper";
4445
protected boolean useRxJava = false;
@@ -83,6 +84,7 @@ public JavaClientCodegen() {
8384
supportedLibraries.put("resteasy", "HTTP client: Resteasy client 3.1.3.Final. JSON processing: Jackson 2.8.9");
8485
supportedLibraries.put("vertx", "HTTP client: VertX client 3.2.4. JSON processing: Jackson 2.8.9");
8586
supportedLibraries.put("google-api-client", "HTTP client: Google API client 1.23.0. JSON processing: Jackson 2.8.9");
87+
supportedLibraries.put("rest-assured", "HTTP client: rest-assured : 3.0.6. JSON processing: JSON processing: Gson 2.6.1");
8688

8789
CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
8890
libraryOption.setEnum(supportedLibraries);
@@ -169,12 +171,12 @@ public void processOpts() {
169171
writeOptional(outputFolder, new SupportingFile("manifest.mustache", projectFolder, "AndroidManifest.xml"));
170172
supportingFiles.add(new SupportingFile("travis.mustache", "", ".travis.yml"));
171173
supportingFiles.add(new SupportingFile("ApiClient.mustache", invokerFolder, "ApiClient.java"));
172-
if(!"resttemplate".equals(getLibrary())) {
174+
if(!("resttemplate".equals(getLibrary()) || REST_ASSURED.equals(getLibrary()))) {
173175
supportingFiles.add(new SupportingFile("StringUtil.mustache", invokerFolder, "StringUtil.java"));
174176
}
175177

176178
// google-api-client doesn't use the Swagger auth, because it uses Google Credential directly (HttpRequestInitializer)
177-
if (!"google-api-client".equals(getLibrary())) {
179+
if (!("google-api-client".equals(getLibrary()) || REST_ASSURED.equals(getLibrary()))) {
178180
supportingFiles.add(new SupportingFile("auth/HttpBasicAuth.mustache", authFolder, "HttpBasicAuth.java"));
179181
supportingFiles.add(new SupportingFile("auth/ApiKeyAuth.mustache", authFolder, "ApiKeyAuth.java"));
180182
supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java"));
@@ -200,7 +202,7 @@ public void processOpts() {
200202
apiDocTemplateFiles.remove("api_doc.mustache");
201203
}
202204

203-
if (!("feign".equals(getLibrary()) || "resttemplate".equals(getLibrary()) || usesAnyRetrofitLibrary() || "google-api-client".equals(getLibrary()))) {
205+
if (!("feign".equals(getLibrary()) || "resttemplate".equals(getLibrary()) || usesAnyRetrofitLibrary() || "google-api-client".equals(getLibrary()) || REST_ASSURED.equals(getLibrary()))) {
204206
supportingFiles.add(new SupportingFile("apiException.mustache", invokerFolder, "ApiException.java"));
205207
supportingFiles.add(new SupportingFile("Configuration.mustache", invokerFolder, "Configuration.java"));
206208
supportingFiles.add(new SupportingFile("Pair.mustache", invokerFolder, "Pair.java"));
@@ -246,6 +248,11 @@ public void processOpts() {
246248
supportingFiles.remove(new SupportingFile("manifest.mustache", projectFolder, "AndroidManifest.xml"));
247249
} else if ("google-api-client".equals(getLibrary())) {
248250
additionalProperties.put("jackson", "true");
251+
252+
} else if (REST_ASSURED.equals(getLibrary())) {
253+
additionalProperties.put("java8", "true");
254+
additionalProperties.put("gson", "true");
255+
apiTemplateFiles.put("api.mustache", ".java");
249256
} else {
250257
LOGGER.error("Unknown library option (-l/--library): " + getLibrary());
251258
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package {{invokerPackage}};
2+
3+
import {{apiPackage}}.*;
4+
5+
{{#imports}}import {{import}};
6+
{{/imports}}
7+
8+
{{^fullJavaUtil}}
9+
import java.util.ArrayList;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
import io.restassured.RestAssured;
14+
import io.restassured.builder.RequestSpecBuilder;
15+
import io.restassured.builder.ResponseSpecBuilder;
16+
import io.restassured.response.Response;
17+
import java.util.function.Consumer;
18+
import java.util.function.Function;
19+
import java.util.function.Supplier;
20+
{{/fullJavaUtil}}
21+
22+
public class ApiClient {
23+
24+
private final Config config;
25+
26+
private ApiClient(Config config) {
27+
this.config = config;
28+
}
29+
30+
public static ApiClient api(Config config) {
31+
return new ApiClient(config);
32+
}
33+
34+
{{#apiInfo}}
35+
{{#apis}}
36+
public {{classname}} {{classVarName}}() {
37+
return {{classname}}.{{classVarName}}(config.baseReqSpec.get());
38+
}
39+
{{/apis}}
40+
{{/apiInfo}}
41+
42+
public static class Config {
43+
private Supplier<RequestSpecBuilder> baseReqSpec;
44+
45+
/**
46+
* Use common specification for all operations
47+
*/
48+
public Config reqSpecSupplier(Supplier<RequestSpecBuilder> supplier) {
49+
this.baseReqSpec = supplier;
50+
return this;
51+
}
52+
53+
public static Config apiConfig() {
54+
return new Config();
55+
}
56+
}
57+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# {{artifactId}}
2+
3+
## Requirements
4+
5+
Building the API client library requires [Maven](https://maven.apache.org/) to be installed.
6+
7+
## Installation & Usage
8+
9+
To install the API client library to your local Maven repository, simply execute:
10+
11+
```shell
12+
mvn install
13+
```
14+
15+
To deploy it to a remote Maven repository instead, configure the settings of the repository and execute:
16+
17+
```shell
18+
mvn deploy
19+
```
20+
21+
Refer to the [official documentation](https://maven.apache.org/plugins/maven-deploy-plugin/usage.html) for more information.
22+
23+
After the client library is installed/deployed, you can use it in your Maven project by adding the following to your *pom.xml*:
24+
25+
```xml
26+
<dependency>
27+
<groupId>{{groupId}}</groupId>
28+
<artifactId>{{artifactId}}</artifactId>
29+
<version>{{artifactVersion}}</version>
30+
<scope>compile</scope>
31+
</dependency>
32+
33+
```
34+
35+
## Recommendation
36+
37+
It's recommended to create an instance of `ApiClient` per thread in a multithreaded environment to avoid any potential issues.
38+
39+
## Author
40+
41+
{{#apiInfo}}{{#apis}}{{^hasMore}}{{infoEmail}}
42+
{{/hasMore}}{{/apis}}{{/apiInfo}}
43+
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package {{package}};
2+
3+
{{#imports}}import {{import}};
4+
{{/imports}}
5+
6+
{{^fullJavaUtil}}
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
import io.restassured.RestAssured;
13+
import io.restassured.builder.RequestSpecBuilder;
14+
import io.restassured.builder.ResponseSpecBuilder;
15+
import io.restassured.response.Response;
16+
import java.util.function.Consumer;
17+
import java.util.function.Function;
18+
import java.util.function.Supplier;
19+
{{/fullJavaUtil}}
20+
21+
import static io.restassured.http.Method.*;
22+
import static io.restassured.mapper.ObjectMapperType.GSON;
23+
24+
public class {{classname}} {
25+
26+
private RequestSpecBuilder reqSpec;
27+
28+
private {{classname}}(RequestSpecBuilder reqSpec) {
29+
this.reqSpec = reqSpec;
30+
}
31+
32+
public static {{classname}} {{classVarName}}(RequestSpecBuilder reqSpec) {
33+
return new {{classname}}(reqSpec);
34+
}
35+
36+
{{#operations}}
37+
{{#operation}}
38+
{{#isDeprecated}}
39+
@Deprecated
40+
{{/isDeprecated}}
41+
public {{operationIdCamelCase}}Oper {{operationId}}() {
42+
return new {{operationIdCamelCase}}Oper(reqSpec);
43+
}
44+
45+
{{/operation}}
46+
{{/operations}}
47+
48+
{{#operations}}
49+
{{#operation}}
50+
/**
51+
* {{summary}}
52+
* {{notes}}
53+
*
54+
{{#allParams}}
55+
* @see #{{#isPathParam}}{{paramName}}Path{{/isPathParam}}{{#isQueryParam}}{{paramName}}Query{{/isQueryParam}}{{#isFormParam}}{{paramName}}Form{{/isFormParam}}{{#isHeaderParam}}{{paramName}}Header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
56+
{{/allParams}}
57+
{{#returnType}}
58+
* return {{{returnType}}}
59+
{{/returnType}}
60+
{{#isDeprecated}}
61+
* @deprecated
62+
{{/isDeprecated}}
63+
{{#externalDocs}}
64+
* {{description}}
65+
* @see <a href="{{url}}">{{summary}} Documentation</a>
66+
{{/externalDocs}}
67+
*/
68+
{{#isDeprecated}}
69+
@Deprecated
70+
{{/isDeprecated}}
71+
public class {{operationIdCamelCase}}Oper {
72+
73+
public static final String REQ_URI = "{{path}}";
74+
75+
private RequestSpecBuilder reqSpec;
76+
77+
private ResponseSpecBuilder respSpec;
78+
79+
public {{operationIdCamelCase}}Oper() {
80+
this.reqSpec = new RequestSpecBuilder();
81+
this.respSpec = new ResponseSpecBuilder();
82+
}
83+
84+
public {{operationIdCamelCase}}Oper(RequestSpecBuilder reqSpec) {
85+
this.reqSpec = reqSpec;
86+
this.respSpec = new ResponseSpecBuilder();
87+
}
88+
89+
/**
90+
* {{httpMethod}} {{path}}
91+
*/
92+
public <T> T execute(Function<Response, T> handler) {
93+
return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request({{httpMethod}}, REQ_URI));
94+
}
95+
96+
{{#returnType}}
97+
{{#returnSimpleType}}
98+
/**
99+
* {{httpMethod}} {{path}}
100+
* @return {{{returnType}}}
101+
*/
102+
public {{{returnType}}} executeAs(Function<Response, Response> handler) {
103+
return execute(handler).as({{{returnType}}}.class, GSON);
104+
}
105+
{{/returnSimpleType}}
106+
107+
{{^returnSimpleType}}
108+
{{#isListContainer}}
109+
/**
110+
* {{httpMethod}} {{path}}
111+
* @return {{{returnType}}}
112+
*/
113+
public {{{returnType}}} executeAs(Function<Response, Response> handler) {
114+
return Arrays.asList(execute(handler).as({{{returnBaseType}}}[].class, GSON));
115+
}
116+
{{/isListContainer}}
117+
{{/returnSimpleType}}
118+
{{/returnType}}
119+
120+
{{#bodyParams}}
121+
/**
122+
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
123+
*/
124+
public {{operationIdCamelCase}}Oper body({{{dataType}}} {{paramName}}) {
125+
reqSpec.setBody({{paramName}}, GSON);
126+
return this;
127+
}
128+
{{/bodyParams}}
129+
130+
{{#headerParams}}
131+
/**
132+
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
133+
*/
134+
public {{operationIdCamelCase}}Oper {{paramName}}Header(String {{paramName}}) {
135+
reqSpec.addHeader("{{baseName}}", {{paramName}});
136+
return this;
137+
}
138+
{{/headerParams}}
139+
140+
{{#pathParams}}
141+
/**
142+
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
143+
*/
144+
public {{operationIdCamelCase}}Oper {{paramName}}Path({{{dataType}}} {{paramName}}) {
145+
reqSpec.addPathParam("{{baseName}}", {{paramName}});
146+
return this;
147+
}
148+
{{/pathParams}}
149+
150+
{{#queryParams}}
151+
/**
152+
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
153+
*/
154+
public {{operationIdCamelCase}}Oper {{paramName}}Query({{{dataType}}} {{paramName}}) {
155+
reqSpec.addQueryParam("{{baseName}}", {{paramName}});
156+
return this;
157+
}
158+
{{/queryParams}}
159+
160+
{{#formParams}}
161+
/**
162+
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
163+
*/
164+
public {{operationIdCamelCase}}Oper {{paramName}}Form({{{dataType}}} {{paramName}}) {
165+
reqSpec.addFormParam("{{baseName}}", {{paramName}});
166+
return this;
167+
}
168+
{{/formParams}}
169+
170+
/**
171+
* Customise request specification
172+
*/
173+
public {{operationIdCamelCase}}Oper reqSpec(Consumer<RequestSpecBuilder> consumer) {
174+
consumer.accept(reqSpec);
175+
return this;
176+
}
177+
178+
/**
179+
* Customise response specification
180+
*/
181+
public {{operationIdCamelCase}}Oper respSpec(Consumer<ResponseSpecBuilder> consumer) {
182+
consumer.accept(respSpec);
183+
return this;
184+
}
185+
}
186+
{{/operation}}
187+
{{/operations}}
188+
}

0 commit comments

Comments
 (0)