Skip to content

nextLink is always populated if PageSize is set, leading to a never ending loop when fetching data #176

@leoerlandsson

Description

@leoerlandsson

Source/destination types

N/A

Mapping configuration

N/A

Version: 4.0.0

EF Core 7.0.0

Expected behavior

nextLink is set to null when there are no more records to return.

I understand that there will be a need for a count to be included to be able to determine if there is more data to return or not.

Actual behavior

nextLink is always set when PageSize is present, disregarding if there is more data to return or not, leading to a never ending loop when fetching data. The nextLink includes a $skip=, which is correct, but the fetch goes on forever as nextLink is never set to null.

E.g:

"@odata.nextLink": "https://localhost:44334/api/Customer?$count=true&$skip=45500"

The $skip count will go on forever.

Steps to reproduce

// When calling: 
var result = await query.GetQueryAsync(_mapper, options, QuerySettings);

// The code in the following file always adds nextLink, if PageSize is set, disregarding if there is more data to return or not:
// This results in a never ending loop when fetching data from the API
// [https://github.com/AutoMapper/AutoMapper.Extensions.OData/blob/master/AutoMapper.AspNetCore.OData.EFCore/QueryableExtensions.cs](https://github.com/AutoMapper/AutoMapper.Extensions.OData/blob/master/AutoMapper.AspNetCore.OData.EFCore/QueryableExtensions.cs)

private static void ApplyOptions<TModel>(ODataQueryOptions<TModel> options, QuerySettings querySettings)
        {
            options.AddExpandOptionsResult();
            if (querySettings?.ODataSettings?.PageSize.HasValue == true)
                options.AddNextLinkOptionsResult(querySettings.ODataSettings.PageSize.Value);
        }

Workaround

We implemented a workaround to clear the nextLink if there are no more record to return. This obviously is only possible if we have a TotalCount:

var query = await query.GetQueryAsync(_mapper, options, QuerySettings);

// Workaround for nextLink if we have count
                if (options.Request.ODataFeature().TotalCount.HasValue)
                {
                    var skip = options.Skip != null ? options.Skip.Value : 0;

                    if (skip + QuerySettings.ODataSettings.PageSize >= options.Request.ODataFeature().TotalCount) {
                        options.Request.ODataFeature().NextLink = null;
                    }
                }

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions