Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: don't access to an empty array. #151

Merged
merged 1 commit into from
Aug 6, 2024
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
41 changes: 41 additions & 0 deletions src/SenseNet.Client.Tests/UnitTests/ContentLoadingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,47 @@ public async Task LoadContent_T_References_Expanded_Multi_List_MultiTypes()
Assert.AreEqual(typeof(MyContentB), referredItems[2].GetType());
}

[TestMethod]
public async Task LoadContent_T_References_Expanded_Empty()
{
// ALIGN
var restCaller = CreateRestCallerFor(@"{ ""d"": { ""Name"": ""Content"", ""Type"": ""Workspace"",
// single reference
""Owner"": { ""Path"": ""/Root/IMS/BuiltIn/Portal/Admin"" },
// null reference
""Manager"": []
}
}");

var repositories = GetRepositoryCollection(services =>
{
services.AddSingleton(restCaller);
services.RegisterGlobalContentType<MyContent>();
services.RegisterGlobalContentType<MyContent2>();
services.RegisterGlobalContentType<MyContent3>();
services.RegisterGlobalContentType<TestContentForReferences>("Workspace");
});
var repository = await repositories.GetRepositoryAsync(FakeServer, CancellationToken.None)
.ConfigureAwait(false);

// ACT
var request = new LoadContentRequest()
{
Path = "/Root/Content",
Expand = new[] { "Owner", "Manager" },
Select = new[] { "Name", "Type", "Owner/Path", "Manager/Path" }
};
var content = await repository.LoadContentAsync<TestContentForReferences>(request, CancellationToken.None);

// ASSERT
var requestedUri = (Uri)restCaller.ReceivedCalls().ToArray()[1].GetArguments().First()!;
Assert.IsNotNull(requestedUri);
Assert.AreEqual("/OData.svc/Root('Content')?metadata=no&$expand=Owner,Manager&$select=Name,Type,Owner/Path,Manager/Path", requestedUri.PathAndQuery);

Assert.IsNotNull(content.Owner);
Assert.IsNull(content.Manager);
}

/* =================================================================== CUSTOM PROPERTIES */

private class CustomType1
Expand Down
2 changes: 1 addition & 1 deletion src/SenseNet.Client/Content_Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ private Content GetReference(object input, Type propertyType)
}
if (input is JArray jArray)
{
return GetReference(jArray[0], propertyType);
return jArray.Count == 0 ? null : GetReference(jArray[0], propertyType);
}
throw new NotSupportedException($"GetReference failed. Object type {input.GetType().FullName} is not supported.");
}
Expand Down