joukevandermaas / saule

JSON API library for ASP.Net Web API 2.
https://joukevandermaas.github.io/saule
MIT License
76 stars 37 forks source link

Fixed pagination when endpoint uses custom PagedResult<T> type #227

Closed sergey-litvinov-work closed 4 years ago

sergey-litvinov-work commented 4 years ago

Hey Jouke,

Sorry, but it's another PR for another missing thing :) we tried to use custom PagedResult class. We use it like

    public class XPagedResult<T>: PagedResult<T>
    {
        public int? TotalPages { get; set; }
    }

And then for JSON.API it won't display TotalPages, but for regular JSON it will display that TotalPages field too plus properties in the base class.

The problem is my initial code in PagedResuleQuery.IsPagedResult was checking for specific type but it should also check the base classes too. I've fixed it in this PR and added unit test to cover that scenario too

Thanks!

sergey-litvinov-work commented 4 years ago

I don't think it's possible in the current approach as we are checking for base generic type PagedResult<> and current one would be some specific typed instance like CustomPagedResult<string> that can't be casted to base one. We can do cast to base only if we know about specific GenericType that was used to made inheritor, then it would work

Like here - https://dotnetfiddle.net/dTGKr8

so it's either manually check full hierarchy for typeless generic PagedResult<> or create PagedResult for specific generic type, and then use type.IsAssignableFrom