Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ public static class DriveItemRequestBuilderExtensions
public static CustomDriveItemItemRequestBuilder ItemWithPath(this Microsoft.Graph.Drives.Item.Root.RootRequestBuilder rootRequestBuilder, string path)
{
path = NormalizePathOrThrow(path);
var requestInformation = rootRequestBuilder.ToGetRequestInformation();
// Encode the path in accordance with the one drive spec
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/addressing-driveitems
UriBuilder builder = new UriBuilder(requestInformation.URI.OriginalString);
builder.Path += $":{path}:";
var parameter = builder.Uri.OriginalString.Split(new []{':'},StringSplitOptions.RemoveEmptyEntries).Last();
var parameter = EncodePath(path);
return new CustomDriveItemItemRequestBuilder(rootRequestBuilder.GetPathParameters(),rootRequestBuilder.GetRequestAdapter(), rootRequestBuilder.GetUrlTemplate().Replace("root",$"root:{parameter}:"));
}

Expand All @@ -61,15 +56,26 @@ public static CustomDriveItemItemRequestBuilder ItemWithPath(this Microsoft.Grap
public static CustomDriveItemItemRequestBuilder ItemWithPath(this Microsoft.Graph.Drives.Item.Items.Item.DriveItemItemRequestBuilder rootRequestBuilder, string path)
{
path = NormalizePathOrThrow(path);
var requestInformation = rootRequestBuilder.ToGetRequestInformation();
// Encode the path in accordance with the one drive spec
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/addressing-driveitems
UriBuilder builder = new UriBuilder(requestInformation.URI.OriginalString);
builder.Path += $":{path}:";
var parameter = builder.Uri.OriginalString.Split(new []{':'},StringSplitOptions.RemoveEmptyEntries).Last();
var parameter = EncodePath(path);
return new CustomDriveItemItemRequestBuilder(rootRequestBuilder.GetPathParameters(), rootRequestBuilder.GetRequestAdapter(),rootRequestBuilder.GetUrlTemplate().Replace("{driveItem%2Did}",$"{{driveItem%2Did}}:{parameter}:"));
}

/// <summary>
/// Encodes the path in accordance with the one drive spec
/// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/addressing-driveitems
/// </summary>
private static string EncodePath(string path)
{
// UriBuilder escapes the path when it is assigned, so only the path is needed here, not the full request URL.
var encoded = new UriBuilder { Path = path }.Path;
// Keep the last non-empty ':' separated part, as before.
var end = encoded.Length;
while (end > 0 && encoded[end - 1] == ':')
end--;
var start = end == 0 ? 0 : encoded.LastIndexOf(':', end - 1) + 1;
Comment on lines +73 to +75

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 0e6c3cb: two theories, one per ItemWithPath overload, covering a:b, a:b::, /::, folder:name/file.txt and item/with/path:. Each asserts the request URL and the Children builder URL. The expected values were taken from main, so they pin the previous behaviour.

Note that for a path such as a:b only b is kept. That is how main behaves today and this PR does not change it; I can open a separate issue if it is not intended.

return encoded.Substring(start, end - start);
}

private static string NormalizePathOrThrow(string path)
{
if (path is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,42 @@ public void ItemByPath_BuildRequestWithSpecialPoundCharacter(string pathInput, s
Assert.Equal(expectedRequestUri, itemRequestInformation.URI);

}


// Only the last non-empty part of a path containing colons is used, and trailing colons are dropped.
[Theory]
[InlineData("a:b", "b")]
[InlineData("a:b::", "b")]
[InlineData("/::", "/")]
[InlineData("folder:name/file.txt", "name/file.txt")]
[InlineData("item/with/path:", "/item/with/path")]
public void ItemByPath_BuildRequestWithColonInPath_Root(string pathInput, string expectedEncodedPath)
{
var graphServiceClient = new GraphServiceClient(new MockAuthenticationProvider().Object);
var expectedRequestUri = new Uri(string.Format(Constants.Url.GraphBaseUrlFormatString, "v1.0") + "/drives/driveId/root:" + expectedEncodedPath + ":");
var expectedChildrenRequestUri = new Uri(expectedRequestUri.OriginalString + "/children");
var itemRequestBuilder = graphServiceClient.Drives["driveId"].Root.ItemWithPath(pathInput);

Assert.Equal(expectedRequestUri, itemRequestBuilder.ToGetRequestInformation().URI);
Assert.Equal(expectedChildrenRequestUri, itemRequestBuilder.Children.ToGetRequestInformation().URI);
}

[Theory]
[InlineData("a:b", "b")]
[InlineData("a:b::", "b")]
[InlineData("/::", "/")]
[InlineData("folder:name/file.txt", "name/file.txt")]
[InlineData("item/with/path:", "/item/with/path")]
public void ItemByPath_BuildRequestWithColonInPath_DriveItem(string pathInput, string expectedEncodedPath)
{
var graphServiceClient = new GraphServiceClient(new MockAuthenticationProvider().Object);
var expectedRequestUri = new Uri(string.Format(Constants.Url.GraphBaseUrlFormatString, "v1.0") + "/drives/driveId/items/itemId:" + expectedEncodedPath + ":");
var expectedChildrenRequestUri = new Uri(expectedRequestUri.OriginalString + "/children");
var itemRequestBuilder = graphServiceClient.Drives["driveId"].Items["itemId"].ItemWithPath(pathInput);

Assert.Equal(expectedRequestUri, itemRequestBuilder.ToGetRequestInformation().URI);
Assert.Equal(expectedChildrenRequestUri, itemRequestBuilder.Children.ToGetRequestInformation().URI);
}

[Fact]
public void ItemByPath_BuildRequestWithNestedPathSlash()
{
Expand Down