diff --git a/src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs b/src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs index 4dec45ad515..874e8a267ec 100644 --- a/src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs +++ b/src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs @@ -44,14 +44,7 @@ public static class DriveItemRequestBuilderExtensions /// public static CustomDriveItemItemRequestBuilder ItemWithPath(this Microsoft.Graph.Drives.Item.Root.RootRequestBuilder rootRequestBuilder, string path) { - if (!string.IsNullOrEmpty(path)) - { - if (!path.StartsWith("/")) - { - path = string.Format("/{0}", 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 @@ -67,14 +60,7 @@ public static CustomDriveItemItemRequestBuilder ItemWithPath(this Microsoft.Grap /// public static CustomDriveItemItemRequestBuilder ItemWithPath(this Microsoft.Graph.Drives.Item.Items.Item.DriveItemItemRequestBuilder rootRequestBuilder, string path) { - if (!string.IsNullOrEmpty(path)) - { - if (!path.StartsWith("/")) - { - path = string.Format("/{0}", 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 @@ -84,6 +70,17 @@ public static CustomDriveItemItemRequestBuilder ItemWithPath(this Microsoft.Grap return new CustomDriveItemItemRequestBuilder(rootRequestBuilder.GetPathParameters(), rootRequestBuilder.GetRequestAdapter(),rootRequestBuilder.GetUrlTemplate().Replace("{driveItem%2Did}",$"{{driveItem%2Did}}:{parameter}:")); } + private static string NormalizePathOrThrow(string path) + { + if (path is null) + throw new ArgumentNullException(nameof(path)); + if (string.IsNullOrWhiteSpace(path)) + throw new ArgumentException("path cannot be empty or whitespace.", nameof(path)); + if (!path.StartsWith("/", StringComparison.Ordinal)) + path = string.Format("/{0}", path); + return path; + } + private static IRequestAdapter GetRequestAdapter(this object obj) { var field = obj.GetType() .BaseType! diff --git a/tests/Microsoft.Graph.DotnetCore.Test/Requests/Extensions/DriveItemRequestBuilderExtensionsTests.cs b/tests/Microsoft.Graph.DotnetCore.Test/Requests/Extensions/DriveItemRequestBuilderExtensionsTests.cs index edc08ec2e5f..2ac9c97e91e 100644 --- a/tests/Microsoft.Graph.DotnetCore.Test/Requests/Extensions/DriveItemRequestBuilderExtensionsTests.cs +++ b/tests/Microsoft.Graph.DotnetCore.Test/Requests/Extensions/DriveItemRequestBuilderExtensionsTests.cs @@ -141,5 +141,41 @@ public void ItemByPath_BuildRequestWithNestedPathSlashAndMoreThanOneParameter() Assert.NotNull(itemRequestInformation); Assert.Equal(expectedRequestUri, itemRequestInformation.URI); } + [Fact] + public void ItemByPath_ThrowsArgumentNullException_WhenPathIsNull_Root() + { + var graphServiceClient = new GraphServiceClient(new MockAuthenticationProvider().Object); + var ex = Assert.Throws(() => graphServiceClient.Drives["driveId"].Root.ItemWithPath(null)); + Assert.Equal("path", ex.ParamName); + } + + [Theory] + [InlineData("")] + [InlineData(" ")] + public void ItemByPath_ThrowsArgumentException_WhenPathIsEmptyOrWhitespace_Root(string path) + { + var graphServiceClient = new GraphServiceClient(new MockAuthenticationProvider().Object); + var ex = Assert.Throws(() => graphServiceClient.Drives["driveId"].Root.ItemWithPath(path)); + Assert.Equal("path", ex.ParamName); + } + + [Fact] + public void ItemByPath_ThrowsArgumentNullException_WhenPathIsNull_DriveItem() + { + var graphServiceClient = new GraphServiceClient(new MockAuthenticationProvider().Object); + var ex = Assert.Throws(() => graphServiceClient.Drives["driveId"].Items["itemId"].ItemWithPath(null)); + Assert.Equal("path", ex.ParamName); + } + + [Theory] + [InlineData("")] + [InlineData(" ")] + public void ItemByPath_ThrowsArgumentException_WhenPathIsEmptyOrWhitespace_DriveItem(string path) + { + var graphServiceClient = new GraphServiceClient(new MockAuthenticationProvider().Object); + var ex = Assert.Throws(() => graphServiceClient.Drives["driveId"].Items["itemId"].ItemWithPath(path)); + Assert.Equal("path", ex.ParamName); + } + } }