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

Expand binary field and a test. #976

Merged
merged 8 commits into from May 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/OData/ExpanderProjector.cs
Expand Up @@ -241,6 +241,8 @@ private object Project(Field field, List<Property> expansion, List<Property> sel
{
if (!(field is ReferenceField refField))
{
if (field is BinaryField binaryField)
return ProjectBinaryField(binaryField);
if (!(field is AllowedChildTypesField allowedChildTypesField))
return null;
return ProjectMultiRefContents(allowedChildTypesField.GetData(), expansion, selection, httpContext);
Expand All @@ -255,6 +257,11 @@ private object Project(Field field, List<Property> expansion, List<Property> sel
? ProjectMultiRefContents(refField.GetData(), expansion, selection, httpContext)
: (object)ProjectSingleRefContent(refField.GetData(), expansion, selection, httpContext);
}
private object ProjectBinaryField(BinaryField field)
{
return RepositoryTools.GetStreamString(
field.Content.ContentHandler.GetBinary(field.Name).GetStream());
Copy link
Member

Choose a reason for hiding this comment

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

I think we should use the same binary provider here as anywhere else when serving binary values.

DocumentBinaryProvider.Instance.GetStream

}
private List<ODataEntity> ProjectMultiRefContents(object references, List<Property> expansion, List<Property> selection, HttpContext httpContext)
{
var contents = new List<ODataEntity>();
Expand Down
28 changes: 28 additions & 0 deletions src/Tests/SenseNet.ODataTests/ODataChildrenTests.cs
Expand Up @@ -13,6 +13,7 @@
using SenseNet.OData;
using SenseNet.ODataTests.Responses;
using SenseNet.Portal;
using SenseNet.Search;
using Task = System.Threading.Tasks.Task;
// ReSharper disable CommentTypo
// ReSharper disable StringLiteralTypo
Expand Down Expand Up @@ -248,5 +249,32 @@ public async Task OD_GET_Children_Property_Filtered()
Assert.IsTrue(entities[0].Name.StartsWith("SF"));
}).ConfigureAwait(false);
}

[TestMethod]
public async Task OD_GET_Children_Binary_Expand()
{
await ODataChildrenTest(async () =>
{
var settingContent = Content.Load("/Root/System/Settings");
settingContent.ChildrenDefinition.EnableAutofilters = FilterStatus.Disabled;
var settingContents = settingContent.Children.ToArray();
var expectedTexts = settingContents
.ToDictionary(
x => x.Name,
x => RepositoryTools.GetStreamString(((File) x.ContentHandler).Binary.GetStream()));

// ACTION
var response = await ODataGetAsync(
$"/OData.svc/Root/System/Settings",
"?metadata=no&$select=Id,Name,Binary&$expand=Binary")
.ConfigureAwait(false);

// ASSERT
var entities = GetEntities(response);
var texts = entities.ToDictionary(x => x.Name, x => x.AllProperties["Binary"].ToString() ?? "null");
foreach (var name in expectedTexts.Keys)
Assert.AreEqual(name + ":" + expectedTexts[name], name + ":" + texts[name]);
}).ConfigureAwait(false);
}
}
}