Merge branch 'master' into java-imports

Conflicts:
	modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java
This commit is contained in:
xhh
2016-01-29 11:16:14 +08:00
356 changed files with 11850 additions and 3863 deletions

View File

@@ -452,34 +452,51 @@ public class ApiClient {
}
}
private ClientResponse getAPIResponse(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames) throws ApiException {
/**
* Build full URL by concatenating base path, the given sub path and query parameters.
*
* @param path The sub path
* @param queryParams The query parameters
* @return The full URL
*/
private String buildUrl(String path, List<Pair> queryParams) {
final StringBuilder url = new StringBuilder();
url.append(basePath).append(path);
if (body != null && !formParams.isEmpty()){
if (queryParams != null && !queryParams.isEmpty()) {
// support (constant) query string in `path`, e.g. "/posts?draft=1"
String prefix = path.contains("?") ? "&" : "?";
for (Pair param : queryParams) {
if (param.getValue() != null) {
if (prefix != null) {
url.append(prefix);
prefix = null;
} else {
url.append("&");
}
String value = parameterToString(param.getValue());
url.append(escapeString(param.getName())).append("=").append(escapeString(value));
}
}
}
return url.toString();
}
private ClientResponse getAPIResponse(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames) throws ApiException {
if (body != null && !formParams.isEmpty()) {
throw new ApiException(500, "Cannot have body and form params");
}
updateParamsForAuth(authNames, queryParams, headerParams);
StringBuilder b = new StringBuilder();
b.append("?");
if (queryParams != null){
for (Pair queryParam : queryParams){
if (!queryParam.getName().isEmpty()) {
b.append(escapeString(queryParam.getName()));
b.append("=");
b.append(escapeString(queryParam.getValue()));
b.append("&");
}
}
}
String querystring = b.substring(0, b.length() - 1);
final String url = buildUrl(path, queryParams);
Builder builder;
if (accept == null)
builder = httpClient.resource(basePath + path + querystring).getRequestBuilder();
else
builder = httpClient.resource(basePath + path + querystring).accept(accept);
if (accept == null) {
builder = httpClient.resource(url).getRequestBuilder();
} else {
builder = httpClient.resource(url).accept(accept);
}
for (String key : headerParams.keySet()) {
builder = builder.header(key, headerParams.get(key));

View File

@@ -495,7 +495,9 @@ public class ApiClient {
public <T> T invokeAPI(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames, GenericType<T> returnType) throws ApiException {
updateParamsForAuth(authNames, queryParams, headerParams);
WebTarget target = httpClient.target(this.basePath).path(path);
// Not using `.target(this.basePath).path(path)` below,
// to support (constant) query string in `path`, e.g. "/posts?draft=1"
WebTarget target = httpClient.target(this.basePath + path);
if (queryParams != null) {
for (Pair queryParam : queryParams) {

View File

@@ -657,8 +657,8 @@ public class ApiClient {
}
/**
* Deserialize response body to Java object, according to the Content-Type
* response header.
* Deserialize response body to Java object, according to the return type and
* the Content-Type response header.
*
* @param response HTTP response
* @param returnType The type of the Java object
@@ -667,12 +667,21 @@ public class ApiClient {
* or the Content-Type of the response is not supported.
*/
public <T> T deserialize(Response response, Type returnType) throws ApiException {
if (response == null || returnType == null)
if (response == null || returnType == null) {
return null;
}
// Handle file downloading.
if (returnType.equals(File.class))
if ("byte[]".equals(returnType.toString())) {
// Handle binary response (byte array).
try {
return (T) response.body().bytes();
} catch (IOException e) {
throw new ApiException(e);
}
} else if (returnType.equals(File.class)) {
// Handle file downloading.
return (T) downloadFileFromResponse(response);
}
String respBody;
try {
@@ -684,8 +693,9 @@ public class ApiClient {
throw new ApiException(e);
}
if (respBody == null || "".equals(respBody))
if (respBody == null || "".equals(respBody)) {
return null;
}
String contentType = response.headers().get("Content-Type");
if (contentType == null) {
@@ -707,20 +717,29 @@ public class ApiClient {
}
/**
* Serialize the given Java object into request body string, according to the
* request Content-Type.
* Serialize the given Java object into request body according to the object's
* class and the request Content-Type.
*
* @param obj The Java object
* @param contentType The request Content-Type
* @return The serialized string
* @return The serialized request body
* @throws ApiException If fail to serialize the given object
*/
public String serialize(Object obj, String contentType) throws ApiException {
if (isJsonMime(contentType)) {
if (obj != null)
return json.serialize(obj);
else
return null;
public RequestBody serialize(Object obj, String contentType) throws ApiException {
if (obj instanceof byte[]) {
// Binary (byte array) body parameter support.
return RequestBody.create(MediaType.parse(contentType), (byte[]) obj);
} else if (obj instanceof File) {
// File body parameter support.
return RequestBody.create(MediaType.parse(contentType), (File) obj);
} else if (isJsonMime(contentType)) {
String content;
if (obj != null) {
content = json.serialize(obj);
} else {
content = null;
}
return RequestBody.create(MediaType.parse(contentType), content);
} else {
throw new ApiException("Content type \"" + contentType + "\" is not supported");
}
@@ -909,7 +928,7 @@ public class ApiClient {
reqBody = RequestBody.create(MediaType.parse(contentType), "");
}
} else {
reqBody = RequestBody.create(MediaType.parse(contentType), serialize(body, contentType));
reqBody = serialize(body, contentType);
}
Request request = null;
@@ -932,20 +951,27 @@ public class ApiClient {
* @return The full URL
*/
public String buildUrl(String path, List<Pair> queryParams) {
StringBuilder query = new StringBuilder();
if (queryParams != null) {
final StringBuilder url = new StringBuilder();
url.append(basePath).append(path);
if (queryParams != null && !queryParams.isEmpty()) {
// support (constant) query string in `path`, e.g. "/posts?draft=1"
String prefix = path.contains("?") ? "&" : "?";
for (Pair param : queryParams) {
if (param.getValue() != null) {
if (query.toString().length() == 0)
query.append("?");
else
query.append("&");
if (prefix != null) {
url.append(prefix);
prefix = null;
} else {
url.append("&");
}
String value = parameterToString(param.getValue());
query.append(escapeString(param.getName())).append("=").append(escapeString(value));
url.append(escapeString(param.getName())).append("=").append(escapeString(value));
}
}
}
return basePath + path + query.toString();
return url.toString();
}
/**

View File

@@ -13,6 +13,7 @@ import org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuil
import retrofit.Converter;
import retrofit.Retrofit;
import retrofit.GsonConverterFactory;
{{#useRxJava}}import retrofit.RxJavaCallAdapterFactory;{{/useRxJava}}
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@@ -22,6 +23,7 @@ import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.ResponseBody;
import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth;
import {{invokerPackage}}.auth.OAuth;
@@ -117,6 +119,7 @@ public class ApiClient {
.Builder()
.baseUrl(baseUrl)
.client(okClient)
{{#useRxJava}}.addCallAdapterFactory(RxJavaCallAdapterFactory.create()){{/useRxJava}}
.addConverterFactory(GsonCustomConverterFactory.create(gson));
}

View File

@@ -2,8 +2,10 @@ package {{package}};
import {{invokerPackage}}.CollectionFormats.*;
import retrofit.Call;
{{#useRxJava}}import rx.Observable;{{/useRxJava}}
{{^useRxJava}}import retrofit.Call;{{/useRxJava}}
import retrofit.http.*;
import com.squareup.okhttp.RequestBody;
{{#imports}}import {{import}};
@@ -28,7 +30,7 @@ public interface {{classname}} {
{{#formParams}}{{#-first}}
{{#isMultipart}}@Multipart{{/isMultipart}}{{^isMultipart}}@FormUrlEncoded{{/isMultipart}}{{/-first}}{{/formParams}}
@{{httpMethod}}("{{path}}")
Call<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}> {{operationId}}({{^allParams}});{{/allParams}}
{{#useRxJava}}Observable{{/useRxJava}}{{^useRxJava}}Call{{/useRxJava}}<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}> {{operationId}}({{^allParams}});{{/allParams}}
{{#allParams}}{{>libraries/retrofit2/queryParams}}{{>libraries/retrofit2/pathParams}}{{>libraries/retrofit2/headerParams}}{{>libraries/retrofit2/bodyParams}}{{>libraries/retrofit2/formParams}}{{#hasMore}}, {{/hasMore}}{{^hasMore}}
);{{/hasMore}}{{/allParams}}

View File

@@ -20,7 +20,7 @@ if(hasProperty('target') && target == 'android') {
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
android {
compileSdkVersion 22
buildToolsVersion '22.0.0'
@@ -32,7 +32,7 @@ if(hasProperty('target') && target == 'android') {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
// Rename the aar correctly
libraryVariants.all { variant ->
variant.outputs.each { output ->
@@ -44,7 +44,7 @@ if(hasProperty('target') && target == 'android') {
}
}
}
afterEvaluate {
android.libraryVariants.all { variant ->
def task = project.tasks.create "jar${variant.name.capitalize()}", Jar
@@ -56,12 +56,12 @@ if(hasProperty('target') && target == 'android') {
artifacts.add('archives', task);
}
}
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
artifacts {
archives sourcesJar
}
@@ -70,16 +70,16 @@ if(hasProperty('target') && target == 'android') {
apply plugin: 'java'
apply plugin: 'maven'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
install {
repositories.mavenInstaller {
pom.artifactId = '{{artifactId}}'
}
}
task execute(type:JavaExec) {
main = System.getProperty('mainClass')
classpath = sourceSets.main.runtimeClasspath
@@ -93,14 +93,26 @@ ext {
gson_version = "2.4"
swagger_annotations_version = "1.5.0"
junit_version = "4.12"
{{#useRxJava}}
rx_java_version = "1.0.15"
{{/useRxJava}}
{{^useRxJava}}{{/useRxJava}}
}
dependencies {
compile "com.squareup.okhttp:okhttp:$okhttp_version"
compile "com.squareup.retrofit:retrofit:$retrofit_version"
compile "com.google.code.gson:gson:$gson_version"
compile "com.squareup.retrofit:converter-gson:$retrofit_version"
{{#useRxJava}}
compile "com.squareup.retrofit:adapter-rxjava:$retrofit_version"
compile "io.reactivex:rxjava:$rx_java_version"
{{/useRxJava}}
{{^useRxJava}}{{/useRxJava}}
compile "com.google.code.gson:gson:$gson_version"
compile "io.swagger:swagger-annotations:$swagger_annotations_version"
compile "org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:$oltu_version"
testCompile "junit:junit:$junit_version"
}

View File

@@ -100,8 +100,9 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>
1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
@@ -137,6 +138,18 @@
<artifactId>okhttp</artifactId>
<version>${okhttp-version}</version>
</dependency>
{{#useRxJava}}
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava</artifactId>
<version>${rxjava-version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit</groupId>
<artifactId>adapter-rxjava</artifactId>
<version>${retrofit-version}</version>
</dependency>
{{/useRxJava}}
<!-- test dependencies -->
<dependency>
@@ -149,6 +162,7 @@
<properties>
<swagger-annotations-version>1.5.0</swagger-annotations-version>
<retrofit-version>2.0.0-beta2</retrofit-version>
{{#useRxJava}}<rxjava-version>1.0.15</rxjava-version>{{/useRxJava}}
<okhttp-version>2.5.0</okhttp-version>
<gson-version>2.4</gson-version>
<oltu-version>1.0.0</oltu-version>