Skip to content

Commit 842335c

Browse files
committed
Check for data collection type inside event subscriber functions added
1 parent ad5e821 commit 842335c

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/JQDT/Application/ApplicationBase.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ private void PerformDataProcessorEventHandler(ref IQueryable<T> data, RequestInf
151151
{
152152
var dataAsObj = (object)data;
153153
eventHandler(ref dataAsObj, requestInfoModel);
154+
155+
// Assert that the data type remains proper inside the event subscriber function.
156+
if (!typeof(IQueryable<T>).IsAssignableFrom(dataAsObj.GetType()))
157+
{
158+
throw new ArgumentException($"Inappropriate data collection type inside event subscriber function. The data collection type must be IQueryable<>.");
159+
}
160+
154161
data = (IQueryable<T>)dataAsObj;
155162
}
156163

src/JQDT/DataProcessing/DataProcessBase.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ private void ExecuteOnDataprocessingEvents(ref IQueryable<T> data, RequestInfoMo
8484
{
8585
var dataAsObj = (object)data;
8686
this.OnDataProcessingEvent(ref dataAsObj, requestInfoModel);
87+
88+
// Assert that the data type remains proper inside the event subscriber function.
89+
if (!typeof(IQueryable<T>).IsAssignableFrom(dataAsObj.GetType()))
90+
{
91+
throw new ArgumentException($"Inappropriate data collection type inside event subscriber function. The data collection type must be IQueryable<>.");
92+
}
93+
8794
data = (IQueryable<T>)dataAsObj;
8895
}
8996

src/Tests/UnitTests/Tests.UnitTests/JQDataTableAttributeMvcUnitTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ public void AllEventsInsideMvcJQDTAttributeShouldBeCalledInCorrectOrder()
7777
Assert.IsTrue(expectedEventsCalls.SequenceEqual(calledEvents));
7878
}
7979

80+
[Test]
81+
public void ExpectToThrowOnInappropriateDataTypeInsideEventSubscriberFunction()
82+
{
83+
var serviceLocatorMock = this.GetServiceLocatorMock();
84+
var executeFunctionMock = this.GetExecuteFunctionProviderMock(serviceLocatorMock);
85+
var contextMock = this.GetHttpContextMock();
86+
87+
var testAttr = new JQDataTableThrowExceptionTestAttribute(serviceLocatorMock.Object, executeFunctionMock.Object);
88+
89+
testAttr.OnActionExecuted(contextMock.Object);
90+
var jsonResult = (JsonResult)contextMock.Object.Result;
91+
var jsonResultString = jsonResult.Data.ToString();
92+
93+
Assert.IsTrue(jsonResultString.Contains("error = Inappropriate data collection type inside event subscriber function. The data collection type must be IQueryable<>."));
94+
}
95+
8096
private Mock<IServiceLocator> GetServiceLocatorMock()
8197
{
8298
var serviceLocatorMock = new Mock<IServiceLocator>();
@@ -162,6 +178,19 @@ protected override IQueryable<T> GetData()
162178
}
163179
}
164180

181+
public class JQDataTableThrowExceptionTestAttribute : JQDataTableAttribute
182+
{
183+
internal JQDataTableThrowExceptionTestAttribute(IServiceLocator serviceLocator, IExecuteFunctionProvider<ActionExecutedContext> executeFunctionProvider)
184+
: base(serviceLocator, executeFunctionProvider)
185+
{
186+
}
187+
188+
public override void OnDataProcessed(ref object data, RequestInfoModel requestInfoModel)
189+
{
190+
data = new List<IntModel>();
191+
}
192+
}
193+
165194
public class JQDataTableTestAttribute : JQDataTableAttribute
166195
{
167196
private readonly List<string> calledEvents;

0 commit comments

Comments
 (0)