【问题标题】:Swashbuckle MapType<Type> doesn't work with parametersSwashbuckle MapType<Type> 不适用于参数
【发布时间】:2018-08-29 02:26:28
【问题描述】:

我有一个将 ShortGuid 类作为参数的 API 端点,如下所示:

[HttpGet("api/endpoint")]
public async Task<IActionResult> GetTablesAsync(ShortGuid id){}

生成一个大摇大摆的定义:

"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。

c.MapType<ShortGuid>(() => new Schema { Type = "string" });

我需要一个 ISchemaFilter 来执行此操作吗?如果是这样,我该如何编写它(尝试了多次但没有成功)

【问题讨论】:

  • 是的,ISchemaFilter 或 IDocumentFilter 会处理它...告诉我们你做了什么。
  • 同样的问题!有答案吗?

标签: c# swagger swagger-ui swashbuckle swashbuckle.examples


【解决方案1】:

我会公开我的案例和解决方案

在我的应用程序中,我使用强类型 ID,如下所示:

[JsonConverter(typeof(IdConverterFactory))]
public partial record MyId(Guid Guid) : Id(Guid)
{
   public MyId(string guidString) : this(new Guid(guidString)){}
}

我在这样的请求类中使用这种类型类型:

public record MyRequest(MyId Id);

[HttpGet("...")]
public async Task<IActionResult> Get([FromQuery]MyRequest request){ 
  ...
}

在OpenApi定义中生成了一个参数“Id.Guid”(应该是简单的Id)。

如果我尝试使用 ?id=some-guid 发出请求,绑定将不起作用。

就像@AroglDarthu 提到的那样,解决方案是创建一个TypeConverter。

以下是我为我的案例创建的TypeConverter:

public class IdTypeConverter<TId>: TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if(sourceType == typeof(string))
        {
            return true;
        }
        return base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        return Activator.CreateInstance(typeof(TId), new[] { value });
    }
}

我有很多强类型的 id,所以我的 TypeConverter 是通用的(用于制作特定的)。

最后,在Program.cs 中,我跟踪Assembly 中的所有Id 类型,并使用TypeDescriptor.AddAttributes 方法为每个类型注册一个特定的TypeConverter:

 typeof(MyId)
            .Assembly
            .GetTypes()
            .Where(t => t.BaseType == typeof(Id))
            .ToList()
            .ForEach(t => TypeDescriptor.AddAttributes(
                t, 
                new TypeConverterAttribute(typeof(IdTypeConverter<>).MakeGenericType(t)))
            );

或者,您可以使用转换器注释类型,而不是使用TypeDescriptor.AddAttributes:

[JsonConverter(typeof(IdConverterFactory))]
[TypeConverter(typeof(IdTypeConverter<MyId>))]
public partial record MyId(Guid Guid) : Id(Guid)
{
   public MyId(string guidString) : this(new Guid(guidString)){}
}

【讨论】:

    【解决方案2】:

    为此,您必须为您的ShortGuid 添加一个TypeConverter。

    这里有一些关于为什么它不起作用的信息:https://github.com/dotnet/aspnetcore/issues/4825

    另外请注意,如果您使用Nullable&lt;ShortGuid&gt;,您还需要添加c.MapType&lt;ShortGuid?&gt;(...)。请参阅https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1648。

    【讨论】:

      猜你喜欢
      • 2016-04-25
      • 2018-07-04
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      相关资源
      最近更新 更多