forked from loafle/openapi-generator-original
Merge pull request #1032 from xhh/retrofit-string-resp
Retrofit template: return body string directly when failed to parse response body as JSON
This commit is contained in:
commit
cc358cf9ce
@ -2,8 +2,20 @@ package {{invokerPackage}};
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonParseException;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
import retrofit.RestAdapter;
|
import retrofit.RestAdapter;
|
||||||
|
import retrofit.converter.ConversionException;
|
||||||
|
import retrofit.converter.Converter;
|
||||||
import retrofit.converter.GsonConverter;
|
import retrofit.converter.GsonConverter;
|
||||||
|
import retrofit.mime.TypedByteArray;
|
||||||
|
import retrofit.mime.TypedInput;
|
||||||
|
import retrofit.mime.TypedOutput;
|
||||||
|
|
||||||
public class ServiceGenerator {
|
public class ServiceGenerator {
|
||||||
// No need to instantiate this class.
|
// No need to instantiate this class.
|
||||||
@ -15,9 +27,63 @@ public class ServiceGenerator {
|
|||||||
.create();
|
.create();
|
||||||
RestAdapter adapter = new RestAdapter.Builder()
|
RestAdapter adapter = new RestAdapter.Builder()
|
||||||
.setEndpoint("{{basePath}}")
|
.setEndpoint("{{basePath}}")
|
||||||
.setConverter(new GsonConverter(gson))
|
.setConverter(new GsonConverterWrapper(gson))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
return adapter.create(serviceClass);
|
return adapter.create(serviceClass);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This wrapper is to take care of this case:
|
||||||
|
* when the deserialization fails due to JsonParseException and the
|
||||||
|
* expected type is String, then just return the body string.
|
||||||
|
*/
|
||||||
|
class GsonConverterWrapper implements Converter {
|
||||||
|
private GsonConverter converter;
|
||||||
|
|
||||||
|
public GsonConverterWrapper(Gson gson) {
|
||||||
|
converter = new GsonConverter(gson);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public Object fromBody(TypedInput body, Type type) throws ConversionException {
|
||||||
|
byte[] bodyBytes = readInBytes(body);
|
||||||
|
TypedByteArray newBody = new TypedByteArray(body.mimeType(), bodyBytes);
|
||||||
|
try {
|
||||||
|
return converter.fromBody(newBody, type);
|
||||||
|
} catch (ConversionException e) {
|
||||||
|
if (e.getCause() instanceof JsonParseException && type.equals(String.class)) {
|
||||||
|
return new String(bodyBytes);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public TypedOutput toBody(Object object) {
|
||||||
|
return converter.toBody(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] readInBytes(TypedInput body) throws ConversionException {
|
||||||
|
InputStream in = null;
|
||||||
|
try {
|
||||||
|
in = body.in();
|
||||||
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
|
byte[] buffer = new byte[0xFFFF];
|
||||||
|
for (int len; (len = in.read(buffer)) != -1;)
|
||||||
|
os.write(buffer, 0, len);
|
||||||
|
os.flush();
|
||||||
|
return os.toByteArray();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ConversionException(e);
|
||||||
|
} finally {
|
||||||
|
if (in != null) {
|
||||||
|
try {
|
||||||
|
in.close();
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -2,8 +2,20 @@ package io.swagger.client;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonParseException;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
import retrofit.RestAdapter;
|
import retrofit.RestAdapter;
|
||||||
|
import retrofit.converter.ConversionException;
|
||||||
|
import retrofit.converter.Converter;
|
||||||
import retrofit.converter.GsonConverter;
|
import retrofit.converter.GsonConverter;
|
||||||
|
import retrofit.mime.TypedByteArray;
|
||||||
|
import retrofit.mime.TypedInput;
|
||||||
|
import retrofit.mime.TypedOutput;
|
||||||
|
|
||||||
public class ServiceGenerator {
|
public class ServiceGenerator {
|
||||||
// No need to instantiate this class.
|
// No need to instantiate this class.
|
||||||
@ -15,9 +27,63 @@ public class ServiceGenerator {
|
|||||||
.create();
|
.create();
|
||||||
RestAdapter adapter = new RestAdapter.Builder()
|
RestAdapter adapter = new RestAdapter.Builder()
|
||||||
.setEndpoint("http://petstore.swagger.io/v2")
|
.setEndpoint("http://petstore.swagger.io/v2")
|
||||||
.setConverter(new GsonConverter(gson))
|
.setConverter(new GsonConverterWrapper(gson))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
return adapter.create(serviceClass);
|
return adapter.create(serviceClass);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This wrapper is to take care of this case:
|
||||||
|
* when the deserialization fails due to JsonParseException and the
|
||||||
|
* expected type is String, then just return the body string.
|
||||||
|
*/
|
||||||
|
class GsonConverterWrapper implements Converter {
|
||||||
|
private GsonConverter converter;
|
||||||
|
|
||||||
|
public GsonConverterWrapper(Gson gson) {
|
||||||
|
converter = new GsonConverter(gson);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public Object fromBody(TypedInput body, Type type) throws ConversionException {
|
||||||
|
byte[] bodyBytes = readInBytes(body);
|
||||||
|
TypedByteArray newBody = new TypedByteArray(body.mimeType(), bodyBytes);
|
||||||
|
try {
|
||||||
|
return converter.fromBody(newBody, type);
|
||||||
|
} catch (ConversionException e) {
|
||||||
|
if (e.getCause() instanceof JsonParseException && type.equals(String.class)) {
|
||||||
|
return new String(bodyBytes);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public TypedOutput toBody(Object object) {
|
||||||
|
return converter.toBody(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] readInBytes(TypedInput body) throws ConversionException {
|
||||||
|
InputStream in = null;
|
||||||
|
try {
|
||||||
|
in = body.in();
|
||||||
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
|
byte[] buffer = new byte[0xFFFF];
|
||||||
|
for (int len; (len = in.read(buffer)) != -1;)
|
||||||
|
os.write(buffer, 0, len);
|
||||||
|
os.flush();
|
||||||
|
return os.toByteArray();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ConversionException(e);
|
||||||
|
} finally {
|
||||||
|
if (in != null) {
|
||||||
|
try {
|
||||||
|
in.close();
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -89,14 +89,14 @@ public interface PetApi {
|
|||||||
/**
|
/**
|
||||||
* Deletes a pet
|
* Deletes a pet
|
||||||
*
|
*
|
||||||
* @param apiKey
|
|
||||||
* @param petId Pet id to delete
|
* @param petId Pet id to delete
|
||||||
|
* @param apiKey
|
||||||
* @return Void
|
* @return Void
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@DELETE("/pet/{petId}")
|
@DELETE("/pet/{petId}")
|
||||||
Void deletePet(
|
Void deletePet(
|
||||||
@Header("api_key") String apiKey,@Path("petId") Long petId
|
@Path("petId") Long petId,@Header("api_key") String apiKey
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -34,13 +34,13 @@ public class Pet {
|
|||||||
**/
|
**/
|
||||||
@ApiModelProperty(required = true, value = "")
|
@ApiModelProperty(required = true, value = "")
|
||||||
@SerializedName("photoUrls")
|
@SerializedName("photoUrls")
|
||||||
private List<String> photoUrls = new ArrayList<String>() ;
|
private List<String> photoUrls = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
**/
|
**/
|
||||||
@ApiModelProperty(value = "")
|
@ApiModelProperty(value = "")
|
||||||
@SerializedName("tags")
|
@SerializedName("tags")
|
||||||
private List<Tag> tags = new ArrayList<Tag>() ;
|
private List<Tag> tags = null;
|
||||||
public enum StatusEnum {
|
public enum StatusEnum {
|
||||||
available, pending, sold,
|
available, pending, sold,
|
||||||
};
|
};
|
||||||
|
@ -120,7 +120,7 @@ public class PetApiTest {
|
|||||||
api.addPet(pet);
|
api.addPet(pet);
|
||||||
|
|
||||||
Pet fetched = api.getPetById(pet.getId());
|
Pet fetched = api.getPetById(pet.getId());
|
||||||
api.deletePet(null, fetched.getId());
|
api.deletePet(fetched.getId(), null);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fetched = api.getPetById(fetched.getId());
|
fetched = api.getPetById(fetched.getId());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user