Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 8f9d610

Browse files
committed
Issue 33 : Code adjustments + Unit tests
1 parent d5e60f7 commit 8f9d610

File tree

4 files changed

+102
-4
lines changed

4 files changed

+102
-4
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Recursive loop of complex types
2+
namespace Community.OData.Linq.xTests.Issues33
3+
{
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using System.Linq;
8+
9+
using Xunit;
10+
11+
public class RecursiveComplexType
12+
{
13+
public RecursiveComplexType SelfReference { get; set; }
14+
}
15+
16+
public class ListItem
17+
{
18+
public int Id { get; set; }
19+
20+
public RecursiveComplexType RecursiveComplexType { get; set; }
21+
}
22+
23+
public class Issue33
24+
{
25+
[Fact]
26+
public void Recursive_Loops_Must_Not_Be_Allowed_By_Default()
27+
{
28+
// arrange
29+
30+
var queryable = new List<ListItem>()
31+
{
32+
new ListItem { Id = 1, RecursiveComplexType = new() },
33+
new ListItem { Id = 2, RecursiveComplexType = new() }
34+
}.AsQueryable();
35+
36+
// act
37+
38+
var exception = Assert.Throws<ArgumentException>(() => queryable.OData().Filter("Id eq 1").ToArray());
39+
40+
// assert
41+
42+
Assert.Contains("recursive loop of complex types is not allowed", exception.Message);
43+
}
44+
45+
[Fact]
46+
public void Recursive_Loops_Must_Be_Allowed_If_Opted_Into_By_Inline_Configuration()
47+
{
48+
// arrange
49+
50+
var queryable = new List<ListItem>()
51+
{
52+
new ListItem { Id = 1, RecursiveComplexType = new() },
53+
new ListItem { Id = 2, RecursiveComplexType = new() }
54+
55+
}.AsQueryable();
56+
57+
// act
58+
59+
var result = queryable.OData(x =>
60+
{
61+
x.AllowRecursiveLoopOfComplexTypes = true;
62+
63+
}).Filter("Id eq 1").ToArray();
64+
65+
// assert
66+
67+
Assert.Single(result);
68+
}
69+
70+
[Fact]
71+
public void Recursive_Loops_Must_Be_Allowed_If_Opted_Into_By_Global_Configuration()
72+
{
73+
// arrange
74+
75+
var queryable = new List<ListItem>()
76+
{
77+
new ListItem { Id = 1, RecursiveComplexType = new() },
78+
new ListItem { Id = 2, RecursiveComplexType = new() }
79+
80+
}.AsQueryable();
81+
82+
ODataSettings.SetInitializer(x =>
83+
{
84+
x.AllowRecursiveLoopOfComplexTypes = true;
85+
86+
});
87+
88+
// act
89+
90+
var result = queryable.OData().Filter("Id eq 1").ToArray();
91+
92+
// assert
93+
94+
Assert.Single(result);
95+
}
96+
}
97+
}

Community.Data.OData.Linq/Builder/ODataModelBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ public virtual IEnumerable<OperationConfiguration> Operations
141141
}
142142

143143
/// <summary>
144-
/// Gets/Sets the ability to allow recursion of complex types
144+
/// Gets/Sets the configured settings
145145
/// </summary>
146-
public bool AllowRecursiveLoopOfComplexTypes { get; set; }
146+
public ODataSettings ODataSettings { get; set; }
147147

148148
/// <summary>
149149
/// Gets or sets the navigation property binding options.

Community.Data.OData.Linq/Builder/StructuralTypeConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public virtual ComplexPropertyConfiguration AddComplexProperty(PropertyInfo prop
383383
throw Error.Argument("propertyInfo", SRResources.PropertyDoesNotBelongToType, propertyInfo.Name, this.ClrType.FullName);
384384
}
385385

386-
if (propertyInfo.PropertyType == this.ClrType && ModelBuilder?.AllowRecursiveLoopOfComplexTypes != true)
386+
if (propertyInfo.PropertyType == this.ClrType && ModelBuilder?.ODataSettings?.AllowRecursiveLoopOfComplexTypes != true)
387387
{
388388
throw Error.Argument("propertyInfo", SRResources.RecursiveComplexTypesNotAllowed, this.ClrType.FullName, propertyInfo.Name);
389389
}

Community.Data.OData.Linq/OdataLinqExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ public static ODataQuery<T> OData<T>(this IQueryable<T> query, Action<ODataSetti
6262
edmModel = Models.GetOrAdd(typeof(T), t =>
6363
{
6464
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
65+
builder.ODataSettings = settings;
6566
builder.AddEntityType(t);
66-
builder.AddEntitySet(t.Name, new EntityTypeConfiguration(new ODataModelBuilder { AllowRecursiveLoopOfComplexTypes = settings.AllowRecursiveLoopOfComplexTypes }, t));
67+
builder.AddEntitySet(t.Name, new EntityTypeConfiguration(builder, t));
6768
return builder.GetEdmModel();
6869
});
6970
}

0 commit comments

Comments
 (0)