forked from loafle/openapi-generator-original
[Java] Play! retrofit2 client content-type handling fixes (#6440)
* proper content-type handling (npe fixed when content-type is absent, improved content-length calculation) * samples updated * fixed missing imports, samples updated
This commit is contained in:
parent
c7d145a4ba
commit
f623aa1f5b
@ -16,6 +16,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Creates {@link Call} instances that invoke underlying {@link WSClient}
|
||||
@ -163,18 +164,21 @@ public class Play24CallFactory implements okhttp3.Call.Factory {
|
||||
|
||||
@Override
|
||||
public MediaType contentType() {
|
||||
return MediaType.parse(r.getHeader("Content-Type"));
|
||||
return Optional.ofNullable(r.getHeader("Content-Type"))
|
||||
.map(MediaType::parse)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long contentLength() {
|
||||
return r.getBody().getBytes().length;
|
||||
return r.asByteArray().length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BufferedSource source() {
|
||||
return new Buffer().write(r.getBody().getBytes());
|
||||
return new Buffer().write(r.asByteArray());
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
for (Map.Entry<String, List<String>> entry : r.getAllHeaders().entrySet()) {
|
||||
|
@ -15,6 +15,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
/**
|
||||
@ -160,18 +161,21 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
|
||||
|
||||
@Override
|
||||
public MediaType contentType() {
|
||||
return MediaType.parse(r.getHeader("Content-Type"));
|
||||
return Optional.ofNullable(r.getHeader("Content-Type"))
|
||||
.map(MediaType::parse)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long contentLength() {
|
||||
return r.getBody().getBytes().length;
|
||||
return r.asByteArray().length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BufferedSource source() {
|
||||
return new Buffer().write(r.getBody().getBytes());
|
||||
return new Buffer().write(r.asByteArray());
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
for (Map.Entry<String, List<String>> entry : r.getAllHeaders().entrySet()) {
|
||||
|
@ -16,6 +16,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Creates {@link Call} instances that invoke underlying {@link WSClient}
|
||||
@ -163,18 +164,21 @@ public class Play24CallFactory implements okhttp3.Call.Factory {
|
||||
|
||||
@Override
|
||||
public MediaType contentType() {
|
||||
return MediaType.parse(r.getHeader("Content-Type"));
|
||||
return Optional.ofNullable(r.getHeader("Content-Type"))
|
||||
.map(MediaType::parse)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long contentLength() {
|
||||
return r.getBody().getBytes().length;
|
||||
return r.asByteArray().length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BufferedSource source() {
|
||||
return new Buffer().write(r.getBody().getBytes());
|
||||
return new Buffer().write(r.asByteArray());
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
for (Map.Entry<String, List<String>> entry : r.getAllHeaders().entrySet()) {
|
||||
|
@ -15,6 +15,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
/**
|
||||
@ -160,18 +161,21 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
|
||||
|
||||
@Override
|
||||
public MediaType contentType() {
|
||||
return MediaType.parse(r.getHeader("Content-Type"));
|
||||
return Optional.ofNullable(r.getHeader("Content-Type"))
|
||||
.map(MediaType::parse)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long contentLength() {
|
||||
return r.getBody().getBytes().length;
|
||||
return r.asByteArray().length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BufferedSource source() {
|
||||
return new Buffer().write(r.getBody().getBytes());
|
||||
return new Buffer().write(r.asByteArray());
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
for (Map.Entry<String, List<String>> entry : r.getAllHeaders().entrySet()) {
|
||||
|
@ -6,6 +6,7 @@ import retrofit2.Call;
|
||||
import retrofit2.http.*;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import io.swagger.client.model.Client;
|
||||
|
@ -6,6 +6,7 @@ import retrofit2.Call;
|
||||
import retrofit2.http.*;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import io.swagger.client.model.Client;
|
||||
|
||||
|
@ -6,6 +6,7 @@ import retrofit2.Call;
|
||||
import retrofit2.http.*;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import java.io.File;
|
||||
import io.swagger.client.model.ModelApiResponse;
|
||||
|
@ -6,6 +6,7 @@ import retrofit2.Call;
|
||||
import retrofit2.http.*;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import io.swagger.client.model.Order;
|
||||
|
||||
|
@ -6,6 +6,7 @@ import retrofit2.Call;
|
||||
import retrofit2.http.*;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import io.swagger.client.model.User;
|
||||
|
||||
|
@ -6,6 +6,7 @@ import rx.Observable;
|
||||
import retrofit2.http.*;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import io.swagger.client.model.Client;
|
||||
|
@ -6,6 +6,7 @@ import rx.Observable;
|
||||
import retrofit2.http.*;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import io.swagger.client.model.Client;
|
||||
|
||||
|
@ -6,6 +6,7 @@ import rx.Observable;
|
||||
import retrofit2.http.*;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import java.io.File;
|
||||
import io.swagger.client.model.ModelApiResponse;
|
||||
|
@ -6,6 +6,7 @@ import rx.Observable;
|
||||
import retrofit2.http.*;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import io.swagger.client.model.Order;
|
||||
|
||||
|
@ -6,6 +6,7 @@ import rx.Observable;
|
||||
import retrofit2.http.*;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import io.swagger.client.model.User;
|
||||
|
||||
|
@ -6,6 +6,7 @@ import io.reactivex.Observable;
|
||||
import retrofit2.http.*;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import io.swagger.client.model.Client;
|
||||
|
@ -6,6 +6,7 @@ import io.reactivex.Observable;
|
||||
import retrofit2.http.*;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import io.swagger.client.model.Client;
|
||||
|
||||
|
@ -6,6 +6,7 @@ import io.reactivex.Observable;
|
||||
import retrofit2.http.*;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import java.io.File;
|
||||
import io.swagger.client.model.ModelApiResponse;
|
||||
|
@ -6,6 +6,7 @@ import io.reactivex.Observable;
|
||||
import retrofit2.http.*;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import io.swagger.client.model.Order;
|
||||
|
||||
|
@ -6,6 +6,7 @@ import io.reactivex.Observable;
|
||||
import retrofit2.http.*;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import io.swagger.client.model.User;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user