Skip to content

Commit 4d05856

Browse files
Copilotlramosvea
andcommitted
Fix ItemWithPath to throw on null/empty/whitespace path
Co-authored-by: lramosvea <77297467+lramosvea@users.noreply.github.com>
1 parent 57a54a0 commit 4d05856

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ public static class DriveItemRequestBuilderExtensions
4444
/// </summary>
4545
public static CustomDriveItemItemRequestBuilder ItemWithPath(this Microsoft.Graph.Drives.Item.Root.RootRequestBuilder rootRequestBuilder, string path)
4646
{
47-
if (!string.IsNullOrEmpty(path))
47+
if (path is null)
48+
throw new ArgumentNullException(nameof(path));
49+
if (string.IsNullOrWhiteSpace(path))
50+
throw new ArgumentException("path cannot be empty or whitespace.", nameof(path));
51+
52+
if (!path.StartsWith("/"))
4853
{
49-
if (!path.StartsWith("/"))
50-
{
51-
path = string.Format("/{0}", path);
52-
}
54+
path = string.Format("/{0}", path);
5355
}
5456

5557
var requestInformation = rootRequestBuilder.ToGetRequestInformation();
@@ -67,12 +69,14 @@ public static CustomDriveItemItemRequestBuilder ItemWithPath(this Microsoft.Grap
6769
/// </summary>
6870
public static CustomDriveItemItemRequestBuilder ItemWithPath(this Microsoft.Graph.Drives.Item.Items.Item.DriveItemItemRequestBuilder rootRequestBuilder, string path)
6971
{
70-
if (!string.IsNullOrEmpty(path))
72+
if (path is null)
73+
throw new ArgumentNullException(nameof(path));
74+
if (string.IsNullOrWhiteSpace(path))
75+
throw new ArgumentException("path cannot be empty or whitespace.", nameof(path));
76+
77+
if (!path.StartsWith("/"))
7178
{
72-
if (!path.StartsWith("/"))
73-
{
74-
path = string.Format("/{0}", path);
75-
}
79+
path = string.Format("/{0}", path);
7680
}
7781

7882
var requestInformation = rootRequestBuilder.ToGetRequestInformation();

tests/Microsoft.Graph.DotnetCore.Test/Requests/Extensions/DriveItemRequestBuilderExtensionsTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,37 @@ public void ItemByPath_BuildRequestWithNestedPathSlashAndMoreThanOneParameter()
141141
Assert.NotNull(itemRequestInformation);
142142
Assert.Equal(expectedRequestUri, itemRequestInformation.URI);
143143
}
144+
[Fact]
145+
public void ItemByPath_ThrowsArgumentNullException_WhenPathIsNull_Root()
146+
{
147+
var graphServiceClient = new GraphServiceClient(new MockAuthenticationProvider().Object);
148+
Assert.Throws<ArgumentNullException>(() => graphServiceClient.Drives["driveId"].Root.ItemWithPath(null));
149+
}
150+
151+
[Theory]
152+
[InlineData("")]
153+
[InlineData(" ")]
154+
public void ItemByPath_ThrowsArgumentException_WhenPathIsEmptyOrWhitespace_Root(string path)
155+
{
156+
var graphServiceClient = new GraphServiceClient(new MockAuthenticationProvider().Object);
157+
Assert.Throws<ArgumentException>(() => graphServiceClient.Drives["driveId"].Root.ItemWithPath(path));
158+
}
159+
160+
[Fact]
161+
public void ItemByPath_ThrowsArgumentNullException_WhenPathIsNull_DriveItem()
162+
{
163+
var graphServiceClient = new GraphServiceClient(new MockAuthenticationProvider().Object);
164+
Assert.Throws<ArgumentNullException>(() => graphServiceClient.Drives["driveId"].Items["itemId"].ItemWithPath(null));
165+
}
166+
167+
[Theory]
168+
[InlineData("")]
169+
[InlineData(" ")]
170+
public void ItemByPath_ThrowsArgumentException_WhenPathIsEmptyOrWhitespace_DriveItem(string path)
171+
{
172+
var graphServiceClient = new GraphServiceClient(new MockAuthenticationProvider().Object);
173+
Assert.Throws<ArgumentException>(() => graphServiceClient.Drives["driveId"].Items["itemId"].ItemWithPath(path));
174+
}
175+
144176
}
145177
}

0 commit comments

Comments
 (0)