Skip to content

Commit

Permalink
Merge pull request Azure#242 from Azure/FixContentType
Browse files Browse the repository at this point in the history
Fix content type
  • Loading branch information
Dan Schulte authored Sep 29, 2017
2 parents 05bea47 + e52a41d commit af7c901
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 21 deletions.
40 changes: 32 additions & 8 deletions client-runtime/src/main/java/com/microsoft/rest/RestProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,41 @@ private HttpRequest createHttpRequest(SwaggerMethodParser methodParser, Object[]
final Object bodyContentObject = methodParser.body(args);
if (bodyContentObject != null) {
String contentType = methodParser.bodyContentType();
if (ContentType.APPLICATION_JSON.equalsIgnoreCase(contentType)) {
if (contentType == null || contentType.isEmpty()) {
contentType = request.headers().value("Content-Type");
}
if (contentType == null || contentType.isEmpty()) {
if (bodyContentObject instanceof byte[] || bodyContentObject instanceof String) {
contentType = ContentType.APPLICATION_OCTET_STREAM;
}
else {
contentType = ContentType.APPLICATION_JSON;
}
}

request.headers().set("Content-Type", contentType);

boolean isJson = false;
final String[] contentTypeParts = contentType.split(";");
for (String contentTypePart : contentTypeParts) {
if (contentTypePart.trim().equalsIgnoreCase(ContentType.APPLICATION_JSON)) {
isJson = true;
break;
}
}

if (isJson) {
final String bodyContentString = serializer.serialize(bodyContentObject);
request.withBody(bodyContentString, contentType);
}
else if (bodyContentObject instanceof byte[]) {
if (contentType == null) {
contentType = ContentType.APPLICATION_OCTET_STREAM;
}
request.withBody((byte[]) bodyContentObject, contentType);
}
else if (bodyContentObject instanceof String) {
if (contentType == null) {
contentType = ContentType.APPLICATION_OCTET_STREAM;
final String bodyContentString = (String) bodyContentObject;
if (!bodyContentString.isEmpty()) {
request.withBody((String) bodyContentObject, contentType);
}
request.withBody((String) bodyContentObject, contentType);
}
else {
final String bodyContentString = serializer.serialize(bodyContentObject);
Expand Down Expand Up @@ -227,7 +247,11 @@ private Single<?> toProxyReturnValue(HttpResponse response, SwaggerMethodParser
@Override
public Single<Object> call(String responseBodyString) {
try {
return Single.just(serializer.deserialize(responseBodyString, entityType));
Object responseBodyObject = null;
if (responseBodyString != null && !responseBodyString.isEmpty()) {
responseBodyObject = serializer.deserialize(responseBodyString, entityType);
}
return Single.just(responseBodyObject);
} catch (Throwable e) {
return Single.error(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ else if (annotationType.equals(HeaderParam.class)) {
else if (annotationType.equals(BodyParam.class)) {
final BodyParam bodyParamAnnotation = (BodyParam) annotation;
bodyContentMethodParameterIndex = parameterIndex;
bodyContentType = bodyParamAnnotation.contentType();
bodyContentType = bodyParamAnnotation.value();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
/**
* @return the Content-Type that the body should be treated as.
*/
String contentType() default "";
String value() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,6 @@ public Single<HttpResponse> sendRequestInternalAsync(HttpRequest request) {
rxnHeaders.put(header.name(), Collections.<Object>singleton(header.value()));
}

String mimeType = request.mimeContentType();
// FIXME: this stopgap fix only puts the mimeContentType in request headers if it's non-empty.
// In the future, a HttpRequestBody should be considered malformed if it's missing a mimeContentType.
if (mimeType != null && !mimeType.isEmpty()) {
rxnHeaders.put("Content-Type", Collections.<Object>singleton(mimeType));
}

final boolean isSecure = "https".equalsIgnoreCase(uri.getScheme());
final int port;
if (uri.getPort() != -1) {
Expand Down
Loading

0 comments on commit af7c901

Please sign in to comment.