-
Notifications
You must be signed in to change notification settings - Fork 7.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expecting an empty observable results in JsonMappingException #1613
Comments
The workaround we found until now: use a wrapper around a // Service wrapper.
public static class Service {
private ServiceEndPoint endPoint;
public Service(Retrofit retrofit) {
endPoint = retrofit.create(ServiceEndPoint.class);
}
Observable<Person> getPerson() {
return Observable.create(new Observable.OnSubscribe<Person>() {
@Override
public void call(Subscriber<? super Person> subscriber) {
if (!subscriber.isUnsubscribed()) {
try {
Response<ResponseBody> response =
endPoint.getPerson().execute();
if(!response.isSuccess()) {
throw new HttpException(response);
}
String json = response.body().string();
if(json != null && json.length() > 0) {
Person person = new ObjectMapper()
.readValue(json, Person.class);
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(person);
}
}
if (!subscriber.isUnsubscribed()) {
subscriber.onCompleted();
}
} catch (Exception ex) {
if (!subscriber.isUnsubscribed()) {
subscriber.onError(ex);
}
}
}
}
});
}
// Service definition.
private interface ServiceEndPoint {
@GET("person")
@Headers("Accept-Encoding: application/json")
Call<ResponseBody> getPerson();
}
} A true solution would be to fix |
A true solution would be catch error and turn it into whatever you want via If you expect empty body then your json entity should represent that. Even if it's "valid" case for On Thu, 18 Feb 2016, 06:35 Rafael Tedin Alvarez [email protected]
@artem_zin |
Jackson throws this, not Retrofit.
This is an accurate statement for Jackson, yes. To support empty payloads you can wrap the There's no action for Retrofit to take here, though. |
Well, we were not considering it an error. With our workaround this is what we basically do: we catch the error before it occurs, i.e. we prevent it. But you're right, we should consider to represent a no-value as
Allow me to remove the Your help was much appreciated! Your comment about |
Good suggestions. Many thanks! |
By the way, here's an example of a null-to-empty delegating converter that doesn't require wrapping: #1554 (comment) |
Thanks, seems a much cleaner solution than our workaround. |
Hi.
We need to interpret an empty body response as an empty observable (completes, no errors, no items emitted). The response code from the server is HTTP 200. The current Retrofit implementation throws a
JsonMappingException
since it tries to deserialize the empty body. We suppose the implementation assumes that there must always be an item in the response body. However, no item is also a valid use case for an observable. The following test illustrates the issue.Note that we want to use
Observable<Person> getPeople()
for the service signature. Is there some workaround for this? Many thanks.The text was updated successfully, but these errors were encountered: