Skip to content

Commit

Permalink
support defining property collection as IEnumerable<> for the new sel…
Browse files Browse the repository at this point in the history
…ect parser
  • Loading branch information
guimabdo committed Aug 25, 2023
1 parent e6df19d commit a9a676f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
6 changes: 4 additions & 2 deletions OData.Client.Abstractions/JsonTemplateHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ private static void Initialize(object instance, int remainingLevels)
{
continue;
}
if (prop.PropertyType.IsArray)
if (prop.PropertyType.IsArray || (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
{
var elementType = prop.PropertyType.GetElementType()!;
var elementType = prop.PropertyType.IsArray ?
prop.PropertyType.GetElementType()! :
prop.PropertyType.GetGenericArguments()[0]!;
if (IsComplexType(elementType))
{
var arrayPropInstance = Array.CreateInstance(elementType, 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Cblx.OData.Client.Tests.SelectAndExpand.NestedIEnumerable;

public class Target
{
public Guid Id { get; set; }

public IEnumerable<TargetChild> Children { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Cblx.OData.Client.Tests.SelectAndExpand.NestedIEnumerable;

public class TargetChild
{
public Guid Id { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Cblx.OData.Client.Tests.SelectAndExpand.NestedIEnumerable;

public class TbSource
{
public Guid Id { get; set; }

public Guid ChildId { get; set; }
public TbSourceChild[] Children { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Cblx.OData.Client.Tests.SelectAndExpand.NestedIEnumerable;

public class TbSourceChild
{
public Guid Id { get; set; }
}
20 changes: 20 additions & 0 deletions OData.Client.UnitTests/SelectAndExpand/NestedIEnumerable/Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Cblx.OData.Client.Tests.SelectAndExpand.NestedIEnumerable;

public class Tests
{
[Fact]
public void Old()
{
string query = "$select=Id&$expand=Children($select=Id)";
var parser = new SelectAndExpandParser<TbSource, Target>();
parser.ToString().Should().Be(query);
}

[Fact]
public void New()
{
string query = "$select=Id&$expand=Children($select=Id)";
var parser = new SelectAndExpandParserV2<TbSource, Target>();
parser.ToSelectAndExpand().Query.Should().Be(query);
}
}

0 comments on commit a9a676f

Please sign in to comment.