Asp.net core Swagger在ASP.CORE 3中为字典生成错误的URL
当从查询字符串提取的模型将字典作为其属性之一时,Swagger生成错误的URL。如何告诉Swagger更改URL中字典的格式或手动定义输入参数模式,而不自动生成?试图使用虚张声势的皮带扣和NSwag 控制器Asp.net core Swagger在ASP.CORE 3中为字典生成错误的URL,asp.net-core,swagger,asp.net-core-webapi,swashbuckle,swashbuckle.aspnetcore,Asp.net Core,Swagger,Asp.net Core Webapi,Swashbuckle,Swashbuckle.aspnetcore,当从查询字符串提取的模型将字典作为其属性之一时,Swagger生成错误的URL。如何告诉Swagger更改URL中字典的格式或手动定义输入参数模式,而不自动生成?试图使用虚张声势的皮带扣和NSwag 控制器 public class RecordsController : ControllerBase { [HttpGet] [Route("services/records")] public async Task<IActionResult> Records([From
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; }
}
大摇大摆生成的URL-开放式API v2-将不绑定到“条件”
大摇大摆生成的URL-开放API v3-将不绑定到“条件”
自定义URL-按预期工作,并使用{“UserId”,“1”}
问题
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为Dictionary类型的属性呈现URL,如PropertyName[Key]=Value
备选问题
这不是一个解决方案,但如果我以这种方式定义输入参数的默认值,Swagger将创建正确的URL
{
"Conditions[UserId]": "1",
"Conditions[GroupId]": "2"
}
URL现在是正确的,并且已正确绑定到模型
/services/records?Page=0&Count=5&Conditions[UserId]=1&Conditions[GroupId]=2
有没有办法更改Swagger中显示的字典输入类型的默认值?您需要为查询定义设置查询样式
deepObject
NSwag目前支持这一点,您将通过它设置值deepObject
我也很好奇如何在没有NSwag的情况下做到这一点,所以我看了一下
在这里,您可以为它提供您的静态json招摇过市,如果您希望看到创建相同设置的不同方式,它将为您生成一个服务器
字典的示例模型
[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; }
查看我尝试过的中的deepObject样式,然后大摇大摆地呈现URL,如/services/records?Page=0&Count=5&Conditions.UserId=1&Conditions.GroupId=2而不是Conditions[UserId]=1&Conditions[GroupId]=2,在本例中,我只能传递指定的参数,如“UserId”和“GroupId”,没有任何数量的字典条目。当你说你尝试了,你从上面的例子中尝试了哪一部分。这样我就可以帮助你缩小为什么它不适合你
/services/records?Page=0&Count=5&Conditions[UserId]=1&Conditions[GroupId]=2
[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; }
/// <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)
/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