msgraph-sdk-java icon indicating copy to clipboard operation
msgraph-sdk-java copied to clipboard

Batch not support Move the Messages.

Open syredeye opened this issue 3 years ago • 1 comments

Sorry, my english.

Basic code

        final BatchRequestContent batchRequestContent = new BatchRequestContent();
        MessageMoveRequest request =  client.me()
                .messages("AAA=")
                .move(MessageMoveParameterSet.newBuilder()
                        .withDestinationId("deleteditems")
                        .build()
                ).buildRequest();

        String requestId = batchRequestContent.addBatchRequestStep(request, HttpMethod.POST);

        final BatchResponseContent batchResponseContent = client.batch().buildRequest().post(batchRequestContent);
        final Message message = batchResponseContent.getResponseById(requestId).getDeserializedBody(Message.class);

        System.out.println(message.subject);

or

String requestId = batchRequestContent.addBatchRequestStep(request, HttpMethod.POST, MessageMoveParameterSet.newBuilder().withDestinationId("deleteditems").build());

That code throw exception : 409 Error message: Write request id : 724517486 does not contain Content-Type header or body.

So I modify added HeaderOption to buildRequest(), but the result was the same.

Spent three hours figuring out the cause. MessageMoveParameterSet is not instanceof IJsonBackedObject (package com.microsoft.graph.serializer)

       if(step.body != null && step.body instanceof IJsonBackedObject &&
            (step.headers == null || !step.headers.containsKey(contentTypeHeaderKey))) {
            if(step.headers == null)
                step.headers = new HashMap<>();
            step.headers.putIfAbsent(contentTypeHeaderKey, "application/json");
        }

Create, class

@Getter @Setter
public class MessageMoveDestinationId implements IJsonBackedObject {
    @SerializedName(value = "destinationId", alternate = {"DestinationId"})
    @Expose
    @Nonnull
    private String destinationId;

    public MessageMoveDestinationId(@Nonnull String destinationId) {
        this.destinationId = destinationId;
    }

    @Override
    public void setRawObject(@Nonnull ISerializer serializer, @Nonnull JsonObject json) {

    }
   
    //I haven't tested whether it's necessary.
    private transient AdditionalDataManager additionalDataManager = new AdditionalDataManager(this);

    @Override
    @Nonnull
    public final AdditionalDataManager additionalDataManager() {
        return additionalDataManager;
    }
}

and run

       final BatchRequestContent batchRequestContent = new BatchRequestContent();
        MessageMoveRequest request =  client.me()
                 .messages("AAA=")
                 .move(null).buildRequest();

        MessageMoveDestinationId destinationId = new MessageMoveDestinationId("deleteditems");

        String requestId = batchRequestContent.addBatchRequestStep(request, HttpMethod.POST, destinationId);

It was worked.

syredeye avatar Apr 01 '22 12:04 syredeye

Hi @syredeye, apologies for the delay, have been spending some time trying to figure out the best way to help, fortunately I have some good news! First of all, I would like to say thank you. You have helped us discover a bug in the way that we handle batch requests for 'actions' in the Java-Sdk. It seems that we are not handling the body of these 'action requests' correctly and therefore they are being lost when added to a batch request. Unfortunately, this is a bit of a tough issue to fix in the SDK and will take some time. In the meantime I offer you this solution that should help your issue and thus make it so that you do not have to create your own custom class. Here you go:

        final BatchRequestContent batchRequestContent = new BatchRequestContent();
        MessageMoveRequest request = graphClient.me()
                .messages("AAA=")
                .move(MessageMoveParameterSet.newBuilder().withDestinationId("deletedItems").build()).buildRequest();
        request.post();

        String requestId = batchRequestContent.addBatchRequestStep(request, HttpMethod.POST, request.body);

ramsessanchez avatar Apr 07 '22 00:04 ramsessanchez