Skip to content

CreateUploadSessionRequestBuilder.PostAsync fails with 400 status code when FileSystemInfo included #3110

@fimiki

Description

@fimiki

Describe the bug

I'm attempting to create a file upload session with pre-set FileSystemInfo using CreateUploadSessionRequestBuilder.

The request builder serializes the CreateUploadSessionPostRequestBody object into the following JSON:

{
  "item": {
    "fileSystemInfo": {
	  "createdDateTime": "2000-01-01T00:00:00-06:00",
	  "lastModifiedDateTime":"2000-01-01T00:00:00-06:00"
     },
     "@microsoft.graph.conflictBehavior":"replace"
  }
}

Which results in a 400 error when attempting to POST

Image

Expected behavior

Serialize the request body so that the "fileSystemInfo" property appears after the "@microsoft.graph.conflictBehavior" directive (which is what the graph API wants?!?) and the POST succeeds:

Image

How to reproduce

DateTime created = new DateTime(2000, 1, 1);

var uploadSessionRequestBody = new Microsoft.Graph.Drives.Item.Items.Item.CreateUploadSession.CreateUploadSessionPostRequestBody
{
    Item = new DriveItemUploadableProperties
    {
        AdditionalData = new Dictionary<string, object>
        {
            { "@microsoft.graph.conflictBehavior", "replace" },
        },
        FileSystemInfo = new Microsoft.Graph.Models.FileSystemInfo
        {
            CreatedDateTime = created,
            LastModifiedDateTime = created
        }
    }
};

var uploadSession = await graphClient.Drives[driveId]
    .Root
    .ItemWithPath(targetPath)
    .CreateUploadSession
    .PostAsync(uploadSessionRequestBody);

SDK Version

5.103.0

Latest version known to work for scenario above?

No response

Known Workarounds

Use "ToPostRequestInformation" to create a request object and manipulate the request body before sending:

        var uploadSessionRequestBody = new Microsoft.Graph.Drives.Item.Items.Item.CreateUploadSession.CreateUploadSessionPostRequestBody
        {
            Item = new DriveItemUploadableProperties
            {
                AdditionalData = new Dictionary<string, object>
                {
                    { "@microsoft.graph.conflictBehavior", "replace" },
                },
                FileSystemInfo = new Microsoft.Graph.Models.FileSystemInfo
                {
                    CreatedDateTime = created,
                    LastModifiedDateTime = created
                }
            }
        };

        var uploadSessionRequest = Graph.Drives[driveId]
            .Root
            .ItemWithPath(targetPath)
            .CreateUploadSession
            .ToPostRequestInformation(uploadSessionRequestBody);
       
        using (var reader = new StreamReader(uploadSessionRequest.Content))
        {
            var jsonString = await reader.ReadToEndAsync();
            JsonNode? rootNode = JsonNode.Parse(jsonString);

            if (rootNode != null && rootNode["item"] is JsonObject itemObj)
            {
                JsonNode? fileSystemInfoNode = itemObj["fileSystemInfo"];
                JsonNode? conflictBehaviorNode = itemObj["@microsoft.graph.conflictBehavior"];

                if (fileSystemInfoNode != null && conflictBehaviorNode != null)
                {
                    itemObj.Remove("fileSystemInfo");
                    itemObj.Remove("@microsoft.graph.conflictBehavior");
                    itemObj.Add("@microsoft.graph.conflictBehavior", conflictBehaviorNode);
                    itemObj.Add("fileSystemInfo", fileSystemInfoNode);
                }

                uploadSessionRequest.Content = new StringContent(rootNode.ToJsonString()).ReadAsStream();
            }
        }

        var uploadSession = await Graph.RequestAdapter.SendAsync(
            uploadSessionRequest, 
            UploadSession.CreateFromDiscriminatorValue, 
            new Dictionary<string, ParsableFactory<IParsable>>{{ "XXX", ODataError.CreateFromDiscriminatorValue }});

Debug output

No response

Configuration

No response

Other information

I'm not sure if this is an issue with the SDK because typically an API shouldn't care about the order in which JSON fields appear. I've seen examples of .net SDK code with upload sessions that include file system info so I'm not sure at which point this broke.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:waiting-for-triageAn issue that is yet to be reviewed or assignedtype:bugA broken experience

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions