Swashbuckle MapType<类型>不适用于参数

Swashbuckle MapTypelt;Typegt; doesn#39;t work with parameters(Swashbuckle MapType类型不适用于参数)
本文介绍了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.请参阅 https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1648 为此.

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<类型>不适用于参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)