Swagger在使用ProducesResponseType时抛出异常。

huangapple go评论72阅读模式
英文:

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&lt;GetBackEndUsers.Result&gt;), 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&lt;T&gt; where T : class
    {
        public int PageNumber { get; set; } = 1;

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

        public int TotalRecords { get; set; }

        public IList&lt;T&gt; Data { get; set; }

        public PaginationResponse()
        {
            Data = new List&lt;T&gt;();
        }
    }

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&lt;GetBackEndUsers.Result&gt;), 200)]
        public async Task&lt;IActionResult&gt; 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 =&gt; s.FullName?.Replace(&quot;+&quot;, &quot;.&quot;));

Solved my problem.

答案2

得分: 0

你应该将 [FromQuery] 属性更新为 [FromBody]。

英文:

You should update [FromQuery] attribute to [FromBody].

huangapple
  • 本文由 发表于 2023年1月6日 13:59:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定