Search code examples
c#swaggerswagger-uiswashbuckle.net-7.0

Swagger is throwing exception when ProducesResponseType is used


I am using .Net 7 and Swagger for Api and documentation. I am facing a problem when using "ProducesResponseType". It is throwing exception which is below.

Here is what I am using to decorate my controller.

This is the problem
[ProducesResponseType(typeof(PaginationResponse<GetBackEndUsers.Result>), 200)]

This is where it is conflicting with. Both have different classes and properties.
[ProducesResponseType(typeof(Login.Result), 200)]

Classes I am using are below

public class PaginationResponse<T> where T : class
    {
        public int PageNumber { get; set; } = 1;

        public int ItemCount { get; set; } = 10;

        public int TotalRecords { get; set; }

        public IList<T> Data { get; set; }

        public PaginationResponse()
        {
            Data = new List<T>();
        }
    }

public static class GetBackEndUsers
    {
        public class Result
        {
            public string FirstName { get; set; } = string.Empty;
            public string LastName { get; set; } = string.Empty;
            public string? Email { get; set; } = string.Empty;
            public DateTime CreatedAt { get; set; }
        }
    }

The exception I am getting is

InvalidOperationException: Can't use schemaId "$Result" for type "$OctuFit.Application.Features.GetBackEndUsers+Result". The same schemaId is already used for type "$OctuFit.Application.Features.Login+Result"

Solution I have tried

options.CustomSchemaIds(type => type.ToString());

options.CustomSchemaIds( type => type.FullName );

Both removes my schema from request and response.

Any help will be appreciated. Cheers

Edit 1

Here is the example of the controller method

[HttpGet]
        [ProducesResponseType(typeof(PaginationResponse<GetBackEndUsers.Result>), 200)]
        public async Task<IActionResult> GetBackendUsers([FromQuery] PaginationRequest pagination)
        {
            var query = new GetBackEndUsers.Query
            {
                PageNumber = pagination.PageNumber,
                ItemCount = pagination.ItemCount
            };

            var response = await mediator.Send(query);

            return HandleResponse(response);
        }

Solution

  • options.CustomSchemaIds(s => s.FullName?.Replace("+", "."));
    

    Solved my problem.