Custom OrmLiteResultsFilter gets Null Reference Exception on LoadSelect

Hi,

I’m running into an issue with a custom OrmLiteResultsFilter, where I’m getting a NullReferenceException if I run a LoadSelect in the context of the filter, but it works fine when just running a Select.

The intent of my filter is just to exclude items that the user may not have permission to view, though even with just basic population functionality I still get the error.

The filter, with just the basic population functionality, looks like this:

public class SecuredItemResultsFilter : OrmLiteResultsFilter
{
    private readonly List<string> permissions;
    public SecuredItemResultsFilter(List<string> permissions)
    {
        this.permissions = permissions;
        ResultsFn = SecuredItemFilter;
    }

    private IEnumerable SecuredItemFilter(IDbCommand dbCmd, Type refType)
    {
        var typedResults = dbCmd.ExecuteReader().ConvertToList(dbCmd.GetDialectProvider(), refType);

        return typedResults;
    }
}

And I’m getting a NullReferenceException on line 161 of OrmLiteResultsFilter.cs, inside the GetRefList method, after my filter runs:

var list = (IList)typeof(List<>).GetCachedGenericType(refType).CreateInstance();

Is there anything I should be doing differently in my filter to support loading references, or something else that I’m missing?

I’m using version 4.5.14, and the SqlServer2012Dialect.

Thanks.

Can you provide a full code sample we can run to see the NRE? Ideally a sample that’s runnable in Gistlyn otherwise a complete example we can run is fine.

No worries, I’ve created a Gist here:

Let me know if you need anything else.

I tried to get it going in Gistlyn here:
http://gistlyn.com/?gist=2b9140fe40bd43682b07f99c3f71d51a
But had some issues running it with any filter in there (Type ‘ServiceStack.OrmLite.OrmLiteState’ in Assembly ‘ServiceStack.OrmLite, Version=4.5.13.0, Culture=neutral, PublicKeyToken=null’ is not marked as serializable.).

The issue is that LoadSelect queries is expecting RefResultsFn or RefResults to be populated, should work if you set it with:

public class TestResultsFilter : OrmLiteResultsFilter
{
    public TestResultsFilter()
    {
        ResultsFn = TestFilter;
        RefResultsFn = TestFilter;
    }

    private IEnumerable TestFilter(IDbCommand dbCmd, Type refType)
    {
        var typedResults = dbCmd.ExecuteReader().ConvertToList(dbCmd.GetDialectProvider(), refType);
        return typedResults;
    }
}

Thanks for that, it works once I add that in.
I was hoping it was something simple, like that, that I was missing.

1 Like

Yeah discovery can be hard since it has a lot of optional properties, I’ve also guarded against this NRE in this commit.

1 Like