tspence / csharp-searchlight

A lightweight, secure framework for a fluent REST API with robust, secure searching features.
Apache License 2.0
11 stars 7 forks source link

Exception in LinqExecutor OrderBy when a model and entity have different data types #102

Closed tspence closed 2 years ago

tspence commented 2 years ago

Version 0.9.19 generates an exception when ordering by a DateTime value

tspence commented 2 years ago

Apparently this only fails on nullable datetime types, not regular datetime types.

tspence commented 2 years ago

Okay, this is odd. I haven't been able to repro it in my test suite. The error being thrown is System.InvalidOperationException: No generic method 'OrderBy' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic. - it seems there is some subtle difference between the test suite that is failing and the Searchlight test suite that is passing.

tspence commented 2 years ago

Found the root cause. Our unit test was sorting on a column, InvoiceDate, which is stored as a string in the API model but is stored as a DateTime? in the database. The root cause of this is the difference between the model data type and the database data type - it was failing to match the correct types.

tspence commented 2 years ago

Solution is to add this annotation to the model:

[SearchlightField(FieldType = typeof(DateTime?))]

This tells Searchlight to correctly query the field by its raw type.

tspence commented 2 years ago

Possible solutions:

tspence commented 2 years ago

Addressed by #106