json-api-dotnet / JsonApiDotNetCore

A framework for building JSON:API compliant REST APIs using ASP.NET and Entity Framework Core.
https://www.jsonapi.net
MIT License
662 stars 160 forks source link

TopLevelLinks All / Paging does not work as expected #1541

Closed patcharees closed 2 months ago

patcharees commented 2 months ago

Set TopLevelLinks.All or TopLevelLinks.Paging but does not get expected links.

DESCRIPTION

On start up

services.AddJsonApi(
    options =>
    {
        options.Namespace = "api/v1";
        options.IncludeTotalResourceCount = true;
        options.DefaultPageSize = new PageSize(100); 
        options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver()
        {
            NamingStrategy = new Newtonsoft.Json.Serialization.CamelCaseNamingStrategy(false, false),
        };
        options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
        options.ValidateModelState = true;

        options.UseRelativeLinks = true;
        options.TopLevelLinks = JsonApiDotNetCore.Resources.Annotations.LinkTypes.All;
        options.RelationshipLinks = JsonApiDotNetCore.Resources.Annotations.LinkTypes.None;
    },
    resources: builder =>
    {
        builder.Add<Resource1, string>("res1");
        builder.Add<Resource2, string>("res2");
        builder.Add<Resource3, string>("res3");
    });

EXPECTED BEHAVIOR

{ "links": { "self": "/api/v1/books", "first": "/api/v1/books?page[size]=100", "last": "prev": "next": }, ... }

ACTUAL BEHAVIOR

{ "links": { "self": "/api/v1/books", "first": "/api/v1/books?page[size]=100" }, ... }

VERSIONS USED

patcharees commented 2 months ago

Another test http://localhost:21000/api/v1/books?page[number]=3

Got below

{
    "links": {
        "self": "/api/v1/books?page[number]=3",
        "first": "/api/v1/books?page[size]=100",
        "prev": "/api/v1/books?page[number]=2&page[size]=100"
    },
    ...
}    

No next, no last

bkoelman commented 2 months ago

This is by design, see https://www.jsonapi.net/usage/options.html#pagination.

bkoelman commented 2 months ago

Otherwise, try with the latest version.