Search code examples
asp.net-core-2.0swashbucklepagedlistjsonserializer

Swashbuckle response is wrong with IPagedList<T>


I have a asp.net core project with uses swashbuckle for JS-client generation. To use pagination we use PagedList from X.PagedList nuget. Controller implementation:

[HttpGet]
[ProducesResponseType(typeof(IPagedList<Model>), 200)]
[ProducesResponseType(typeof(void), 500)]
public async Task<IActionResult> Get([FromQuery]query)
{
    var response = await this.mediator.Send(query);

    return Ok(response.Results); // results is IPagedList<Model>
}

the result in JSON looks like this:

{
    "count": 10,
    "pageCount": 2,
    "totalItemCount": 15,
    "pageNumber": 1,
    "pageSize": 10,
    "hasPreviousPage": false,
    "hasNextPage": true,
    "isFirstPage": true,
    "isLastPage": false,
    "firstItemOnPage": 1,
    "lastItemOnPage": 10,
    "items": [
        {
            "guid": "dafa9d3b-9ee2-4cbc-b7d7-902b5bc9e887",
            "name": "asdf",
            "number": 1006,
        }
    ]
}

but somehow swagger thinks the result looks like Array of Model:

[
  {
    "guid": "string",
    "name": "string",
    "number": 0,
  }
]

I also tried with

[SwaggerResponse(200, Type = typeof(IPagedList<Model>))]

without success.

XPagedList 7.1.0 Swashbuckle.AspNetCore.Swagger 1.0.0

Is there a way I can overwrite the output? it makes client code generation useless if the code is wrong!

EDIT: There is a custom JsonConverter for PagedList

services.AddMvc().AddControllersAsServices().AddJsonOptions(options =>
{
    options.SerializerSettings.NullValueHandling = NullValueHandling.Include;
    options.SerializerSettings.Converters.Add(new CustomIEnumerableConverter(new[] {"X.PagedList"}));
});

But as I understand Swashbuckle should use the same JsonSerializer.


Solution

  • If you want to overwrite the output you can use an IDocumentFilter, I have a few examples here: SwashbuckleTest/blob/master/Swagger_Test/App_Start/SwaggerConfig.cs


    Now I'm not sure why your response shows count, pageCount, totalItemCount those should not be showing on the response, I created a controller to test:

    public class PagedListController : ApiController
    {
        // GET: api/PagedList
        public IPagedList<Company> Get()
        {           
            return PagedCompany;
        }
    
        // GET: api/PagedList/5
        [SwaggerResponse(200, Type = typeof(IPagedList<Company>))]
        public async Task<IHttpActionResult> Get(int id)
        {
            return Ok(PagedCompany);
        }
    
        private IPagedList<Company> PagedCompany
        {
            get
            {
                var data = new List<Company>();
                for (int i = 0; i < 10; i++)
                    data.Add(new Company { Id = i, Name = i.ToString() });
                return new PagedList<Company>(data, 1, 3);
            }
        }
    }
    

    And the response does not have any of those yours have, you can try it here: http://swashbuckletest.azurewebsites.net/swagger/ui/index?filter=PagedList#/PagedList/PagedList_Get