英文:
Swagger is throwing exception when ProducesResponseType is used
问题
我正在使用.Net 7和Swagger进行Api和文档编写。当使用"ProducesResponseType"时,我遇到了一个问题。它抛出了以下异常。
以下是我用来装饰我的控制器的内容。
这是问题
[ProducesResponseType(typeof(PaginationResponse<GetBackEndUsers.Result>), 200)]
这是与之冲突的地方。两者具有不同的类和属性。
[ProducesResponseType(typeof(Login.Result), 200)]
我使用的类如下
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; }
}
}
我收到的异常是
InvalidOperationException: 无法为类型"$OctuFit.Application.Features.GetBackEndUsers+Result"使用模式标识符"$Result"。相同的模式标识符已用于类型"$OctuFit.Application.Features.Login+Result"
我尝试过的解决方案
options.CustomSchemaIds(type => type.ToString());
options.CustomSchemaIds(type => type.FullName);
这两种方法都会从我的请求和响应中移除模式。
将不在此处提供代码的详细信息,如有需要,请提供更多上下文。
英文:
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);
}
答案1
得分: 2
options.CustomSchemaIds(s => s.FullName?.Replace("+", "."));
Solved my problem.
英文:
options.CustomSchemaIds(s => s.FullName?.Replace("+", "."));
Solved my problem.
答案2
得分: 0
你应该将 [FromQuery] 属性更新为 [FromBody]。
英文:
You should update [FromQuery] attribute to [FromBody].
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论