c# - Swagger 在 ASP.CORE 3 中为字典生成不正确的 URL

标签 c# swagger asp.net-core-webapi swashbuckle swashbuckle.aspnetcore

当从查询字符串中提取的模型具有字典作为其属性之一时,Swagger 会生成不正确的 URL。如何告诉 Swagger 更改 URL 中字典的格式或手动定义输入参数模式,而不自动生成?尝试使用 Swashbuckle 和 NSwag。

Controller

public class RecordsController : ControllerBase
{
  [HttpGet]
  [Route("services/records")]
  public async Task<IActionResult> Records([FromQuery] QueryModel queryModel)
  {
    return null;
  }
}

输入模型 - 查询字符串
public class QueryModel 
{
  public int Page { get; set; }
  public int Count { get; set; }
  public Dictionary<Columns, string> Conditions { get; set; }
}

Swagger 用户界面 显示查询模型上“条件”属性的这种格式
{
  "UserId": "string",
  "GroupId": "string",
  "RecordId": "string"
}

Swagger 生成的 URL - Open API v2 - 不受“条件”的约束
/services/records?Page=0&Count=5&Conditions={"UserId":"1"} 

Swagger 生成的 URL - Open API v3 - 不受“条件”的约束
/services/records?Page=0&Count=5&UserId=1 

自定义网址 - 按预期工作并使用 { "UserId", "1" } 初始化“条件”
/services/records?Page=0&Count=5&Conditions[UserId]=1 

问题

如何强制 Swagger 呈现 URL,如 PropertyName[Key]=Value对于字典类型的属性?

替代问题

不是解决方案,但如果我以这种方式为输入参数定义默认值,Swagger 会创建正确的 URL。
{
  "Conditions[UserId]": "1",
  "Conditions[GroupId]": "2"
}

URL 现在正确并正确绑定(bind)到模型
/services/records?Page=0&Count=5&Conditions[UserId]=1&Conditions[GroupId]=2 

有没有办法更改 Swagger 中显示的字典输入类型的默认值?

最佳答案

您需要设置查询样式deepObject对于查询定义
NSwag 目前通过 SwaggerParameterStyle 支持此功能。您将为其设置值deepObject .
我也很好奇如何在没有 NSwag 的情况下做到这一点,所以我看了一下 https://editor.swagger.io/
在这里,您可以为其提供静态 json Swagger ,如果您想查看创建相同设置的不同方式,它将为您生成服务器
字典的样本模型

[DataContract]
public partial class Dictionary : IEquatable<Dictionary>
{ 
    /// <summary>
    /// Gets or Sets Word
    /// </summary>
    [DataMember(Name="word")]
    public string Word { get; set; }

    /// <summary>
    /// Gets or Sets Define
    /// </summary>
    [DataMember(Name="define")]
    public string Define { get; set; }
sample Controller
    /// <summary>
    /// Get word definition
    /// </summary>
    /// <remarks>Get me the word definitions</remarks>
    /// <param name="dictionary">Status values that need to be considered for filter</param>
    /// <response code="200">successful operation</response>
    [HttpGet]
    [Route("/v2/book")]
    [ValidateModelState]
    [SwaggerOperation("BookGet")]
    public virtual IActionResult BookGet([FromQuery][Required()]Dictionary dictionary)
原始 Swagger 示例查询
/book:
  get:
    summary: Get word definition
    description: Get me the word definitions
    parameters:
    - name: dictionary
      in: query
      description: Status values that need to be considered for filter
      required: true
      style: deepObject
      schema:
        type: object
        properties:
          word: 
            type: string
          define:
            type: string
查看 https://swagger.io/specification/ 中的 deepObject 样式

关于c# - Swagger 在 ASP.CORE 3 中为字典生成不正确的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59926069/

相关文章:

javascript - requestBody 未出现在通话中

c# - .NET Core 和 Swagger API 生成

c# - 带有 DTD 解析错误的 .NET Core Web API XML

c# - WebAPI 中的文件上传

C# 算法时间复杂度

c# - 一个 C# 异常可以在不同线程上多次引发吗?

maven - 如何在 swagger-maven-plugin 3.1.0 中设置覆盖模型

javascript - 使用 Dot Net Core 2 WebApi 中的 Json 序列化自动将 C# DateTime 转换为 JavaScript 日期

c# - 在 C# 中创建非阻塞异步门

c# - 在 Windows Phone 中使用 JSON/Web Service 通过 MySQL 数据库调用存储在服务器上的视频等媒体