gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:26:51 25 4
gpt4 key购买 nike

当从查询字符串中提取的模型具有字典作为其属性之一时,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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com