mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-11 03:22:42 +00:00
[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:
@@ -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,20 +164,23 @@ 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()) {
|
||||
for (String value : entry.getValue()) {
|
||||
builder.addHeader(entry.getKey(), value);
|
||||
|
||||
@@ -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,20 +161,23 @@ 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()) {
|
||||
for (String value : entry.getValue()) {
|
||||
builder.addHeader(entry.getKey(), value);
|
||||
|
||||
Reference in New Issue
Block a user