Swashbuckle MapTypelt;Typegt; doesn#39;t work with parameters(Swashbuckle MapType类型不适用于参数)
问题描述
我有一个将 ShortGuid 类作为参数的 API 端点,如下所示:
I've got an API endpoint that takes a ShortGuid class as a parameter, like such:
[HttpGet("api/endpoint")]
public async Task<IActionResult> GetTablesAsync(ShortGuid id){}
生成一个大摇大摆的定义:
Generates a swagger definition of:
"parameters":[
{
"name":"guid",
"in":"query",
"required":false,
"type":"string",
"format":"uuid"
},
{
"name":"value",
"in":"query",
"required":false,
"type":"string"
}
],
我需要将该参数视为字符串,而不是 ShortGuid 对象.我已经有一个可以正常工作的类型的 JsonConverter,但是 Swashbuckle 不理解它,所以我的架构不正确(而且我的 swagger-js 客户端不起作用).我认为 MapType<> 会起作用,但这似乎只影响响应对象,因为架构仍将其视为 ShortGuid.
I need to treat that parameter as a string, not a ShortGuid object. I already have a JsonConverter for the type that works fine, but Swashbuckle doesn't understand it so my schema is incorrect (and this my swagger-js client doesnt work). I thought MapType<> would work however that seems to only affect response objects as the schema still treats it as a ShortGuid.
c.MapType<ShortGuid>(() => new Schema { Type = "string" });
我需要一个 ISchemaFilter 来执行此操作吗?如果是这样,我该如何编写它(尝试了多次但没有成功)
Will I require an ISchemaFilter to do this? And if so, how do I go about writing it (tried multiple attempts but no success)
推荐答案
为此,您必须为您的 ShortGuid
添加一个 TypeConverter
.
For this to work on the query string, you have to add a TypeConverter
for your ShortGuid
.
这里有一些关于为什么它不起作用的信息:https://github.com/dotnet/aspnetcore/issues/4825
Here is some information on why it does not work otherwise: https://github.com/dotnet/aspnetcore/issues/4825
另外请注意,如果您使用 Nullable
,您还需要添加 c.MapType
Also note that if you use a Nullable<ShortGuid>
, you will also need to add c.MapType<ShortGuid?>(...)
. See https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1648 for that.
这篇关于Swashbuckle MapType<类型>不适用于参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Swashbuckle MapType<类型>不适用于参数


- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01