Skip to content
Merged
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 @@ -44,14 +44,7 @@
/// </summary>
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
Expand All @@ -67,14 +60,7 @@
/// </summary>
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
Expand All @@ -84,29 +70,40 @@
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()

Check warning on line 85 in src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Type.GetFields(BindingFlags)'. The return value of method 'System.Type.BaseType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
.BaseType!
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.FirstOrDefault( field => field.FieldType == typeof(IRequestAdapter));
return (IRequestAdapter)field?.GetValue(obj);
}
private static Dictionary<string, object> GetPathParameters(this object obj) {
var field = obj.GetType()

Check warning on line 92 in src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Type.GetFields(BindingFlags)'. The return value of method 'System.Type.BaseType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
.BaseType!
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.FirstOrDefault( field => field.FieldType == typeof(Dictionary<string, object>));
return (Dictionary<string, object>)field?.GetValue(obj);
}
private static string GetUrlTemplate(this object obj) {
var field = obj.GetType()

Check warning on line 99 in src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Type.GetFields(BindingFlags)'. The return value of method 'System.Type.BaseType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
.BaseType!
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.FirstOrDefault( field => field.FieldType == typeof(string));
return (string)field?.GetValue(obj);
}
internal static T UpdateUrlTemplate<T>(this T obj, string baseTemplate) where T : BaseRequestBuilder{
var field = obj.GetType()

Check warning on line 106 in src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Type.GetFields(BindingFlags)'. The return value of method 'System.Type.BaseType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
.BaseType!
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.FirstOrDefault( field => field.FieldType == typeof(string));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentNullException>(() => 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<ArgumentException>(() => 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<ArgumentNullException>(() => 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<ArgumentException>(() => graphServiceClient.Drives["driveId"].Items["itemId"].ItemWithPath(path));
Assert.Equal("path", ex.ParamName);
}

}
}
Loading