Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/com/microsoft/graph/http/CoreHttpProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,15 @@ public <Result, Body> Request getHttpRequest(@Nonnull final IHttpRequest request
}
} else {
logger.logDebug("Sending " + serializable.getClass().getName() + " as request body");
final String serializeObject = serializer.serializeObject(serializable);

String serializeObject = null;

if ("text/plain".equals(contenttype) && serializable instanceof String) {
serializeObject = (String)serializable;
} else {
serializeObject = serializer.serializeObject(serializable);
}

if(serializeObject == null) {
throw new ClientException("Error during serialization of request body, the result was null", null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/** This class provides methods to get the derived class corresponding to the OData type when deserializing payloads. */
public class DerivedClassIdentifier {

private final static String ODATA_TYPE_KEY = "@odata.type";

private final ILogger logger;

/**
* Creates a new instance of the dereived class identifier.
* @param logger The logger to use.
*/
public DerivedClassIdentifier(@Nonnull ILogger logger) {
this.logger = Objects.requireNonNull(logger, "logger parameter cannot be null");;
}
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/com/microsoft/graph/http/CoreHttpProviderTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.microsoft.graph.serializer.DefaultSerializer;
import com.microsoft.graph.serializer.ISerializer;

import okio.Buffer;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
Expand All @@ -39,6 +40,8 @@
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.RequestBody;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -49,6 +52,8 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class CoreHttpProviderTests {
Expand Down Expand Up @@ -288,4 +293,36 @@ public void getHttpRequestSetsRetryOrRedirectOptionsOnNonDefaultValues() throws

assertNotNull(retryOptions);
}

@Test
public void getHttpRequestWithTextPlainBodyDoesNotSerializeAsJson() throws IOException {
final IHttpRequest absRequest = mock(IHttpRequest.class);
when(absRequest.getRequestUrl()).thenReturn(new URL("https://graph.microsoft.com/v1.0/me"));
when(absRequest.getHttpMethod()).thenReturn(HttpMethod.POST);
final ISerializer serializer = mock(ISerializer.class);
final ILogger logger = mock(ILogger.class);

mProvider = new CoreHttpProvider(serializer,
logger,
new OkHttpClient.Builder().build());

// GIVEN: A "text/plain" request body
final HeaderOption option = new HeaderOption("Content-Type", "text/plain");
when(absRequest.getHeaders()).thenReturn(Arrays.asList(option));
final String expectedBody = "Plain String Body";

//WHEN: getHttpRequest is called
final Request request = mProvider.getHttpRequest(absRequest, String.class, expectedBody);

// THEN: The serializer must not be called
verify(serializer, never()).serializeObject(Mockito.any());

// AND: We expect the request body to contain the plain String, not serialized as Json
final Buffer buffer = new Buffer();
final RequestBody requestBody = request.body();
assertNotNull(requestBody);
requestBody.writeTo(buffer);
final String actualRequestBody = buffer.readUtf8();
assertEquals(expectedBody, actualRequestBody);
}
}