Skip to content

Commit 92cb5ec

Browse files
committed
- improves api surface by removing the need to pass the serializer
1 parent 5a98e14 commit 92cb5ec

3 files changed

Lines changed: 23 additions & 7 deletions

File tree

‎src/main/java/com/microsoft/graph/content/BatchRequest.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public BatchRequest(@Nonnull final String requestUrl, @Nonnull final IBaseClient
5757
@Nullable
5858
public BatchResponseContent post(@Nonnull final BatchRequestContent content) throws ClientException {
5959
this.setHttpMethod(HttpMethod.POST);
60-
return this.getClient().getHttpProvider().send(this, BatchResponseContent.class, content);
60+
final BatchResponseContent response = this.getClient().getHttpProvider().send(this, BatchResponseContent.class, content);
61+
setSerializerOnSteps(response);
62+
return response;
6163
}
6264
/**
6365
* Send this request
@@ -69,6 +71,15 @@ public BatchResponseContent post(@Nonnull final BatchRequestContent content) thr
6971
@Nullable
7072
public java.util.concurrent.CompletableFuture<BatchResponseContent> postAsync(@Nonnull final BatchRequestContent content) throws ClientException {
7173
this.setHttpMethod(HttpMethod.POST);
72-
return this.getClient().getHttpProvider().sendAsync(this, BatchResponseContent.class, content);
74+
return this.getClient().getHttpProvider().sendAsync(this, BatchResponseContent.class, content).thenApply(response -> {
75+
setSerializerOnSteps(response);
76+
return response;
77+
});
78+
}
79+
private void setSerializerOnSteps(final BatchResponseContent response) {
80+
if(response.responses != null)
81+
for(final BatchResponseStep<?> step : response.responses) {
82+
step.serializer = this.getClient().getSerializer();
83+
}
7384
}
7485
}

‎src/main/java/com/microsoft/graph/content/BatchResponseStep.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,21 @@ public class BatchResponseStep<T> extends BatchStep<T> {
4141
@Expose
4242
@SerializedName("status")
4343
public int status;
44+
/** Serializer to use for response deserialization */
45+
protected ISerializer serializer;
4446

4547
/**
4648
* Returned the deserialized response body of the current step
4749
* @param <T2> type of the response body
48-
* @param serializer serializer to use for deserialization. graphClient.getSerializer()
4950
* @param resultClass class of the resulting response body
5051
* @return the deserialized response body
5152
* @throws GraphServiceException when a bad request was sent
5253
* @throws GraphFatalServiceException when the service did not complete the operation as expected because of an internal error
5354
*/
5455
@Nullable
55-
public <T2> T2 getDeserializedBody(@Nonnull final ISerializer serializer, @Nonnull final Class<T2> resultClass) throws GraphServiceException, GraphFatalServiceException {
56+
public <T2> T2 getDeserializedBody(@Nonnull final Class<T2> resultClass) throws GraphServiceException, GraphFatalServiceException {
5657
Objects.requireNonNull(resultClass, "parameter resultClass cannot be null");
57-
if(body == null || !(body instanceof JsonElement)) return null;
58+
if(serializer == null || body == null || !(body instanceof JsonElement)) return null;
5859

5960
final GraphErrorResponse error = serializer.deserializeObject((JsonElement)body, GraphErrorResponse.class);
6061
if(error == null || error.error == null) {

‎src/test/java/com/microsoft/graph/content/BatchResponseContentTest.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,15 @@ public void deserializesErrorsProperly() {
102102
String responsebody = "{\"responses\":[{\"id\":\"1\",\"status\":400,\"headers\":{\"Cache-Control\":\"no-cache\",\"x-ms-resource-unit\":\"1\",\"Content-Type\":\"application/json\"},\"body\":{\"error\":{\"code\":\"Request_BadRequest\",\"message\":\"Avalueisrequiredforproperty'displayName'ofresource'User'.\",\"innerError\":{\"date\":\"2021-02-02T19:19:38\",\"request-id\":\"408b8e64-4047-4c97-95b6-46e9f212ab48\",\"client-request-id\":\"102910da-260c-3028-0fb3-7d6903a02622\"}}}}]}";
103103
ISerializer serializer = new DefaultSerializer(mock(ILogger.class));
104104
BatchResponseContent batchresponse = serializer.deserializeObject(responsebody, BatchResponseContent.class);
105+
if(batchresponse != null && batchresponse.responses != null) // this is done by the batch request in the fluent API
106+
for(final BatchResponseStep<?> step : batchresponse.responses) {
107+
step.serializer = serializer;
108+
}
105109
assertThrows(GraphServiceException.class, () -> {
106-
final Object body = batchresponse.getResponseById("1").getDeserializedBody(serializer, BatchRequestContent.class);
110+
final Object body = batchresponse.getResponseById("1").getDeserializedBody(BatchRequestContent.class);
107111
});
108112
try {
109-
final Object body = batchresponse.getResponseById("1").getDeserializedBody(serializer, BatchRequestContent.class);
113+
final Object body = batchresponse.getResponseById("1").getDeserializedBody(BatchRequestContent.class);
110114
} catch(GraphServiceException ex) {
111115
final GraphErrorResponse response = ex.getError();
112116
assertNotNull(response);

0 commit comments

Comments
 (0)