0

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.

Helder Sepulveda
  • 15,500
  • 4
  • 29
  • 56
Andre
  • 662
  • 1
  • 8
  • 19

1 Answers1

1

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

Helder Sepulveda
  • 15,500
  • 4
  • 29
  • 56
  • Why should those properties be private? I can access them in the controller without any problem. the don't have setters but the getters are available – Andre Nov 03 '17 at 14:41
  • Maybe I'm wrong maybe they are not private, but they are not showing in my example... – Helder Sepulveda Nov 03 '17 at 15:19
  • Do you have a full project on GitHub that I can clone? _ Here is mine: https://github.com/heldersepu/SwashbuckleTest – Helder Sepulveda Nov 03 '17 at 15:20
  • https://github.com/muehan/swashbuckledemo/blob/master/swashbuckletest/Infrastructure/CustomIEnumerableConverter.cs is needed that IPagedList is serialized. Should Swashbuckle not use the same serializer? – Andre Nov 06 '17 at 11:21
  • @Andre That is a HUGE missing piece of the puzzle in your question... And no Swashbuckle will not use the same serializer _ I got my own fork of Swashbuckle and I've seen this request to be able to specify a serializer before, let's see what I can do... – Helder Sepulveda Nov 06 '17 at 13:08
  • since you have already used IDocumentFilters, would you say this is a good way to use it: https://github.com/muehan/swashbuckledemo/blob/master/swashbuckletest/Infrastructure/PagedListDocumentFilter.cs – Andre Nov 06 '17 at 13:46
  • I mark your answer as correct, since your first sentence is the answer I needed – Andre Nov 06 '17 at 13:49
  • @Andre Thanks I'm working on a closer example of IDocumentFilter... will send it via GitHub – Helder Sepulveda Nov 06 '17 at 14:10