【问题标题】:How can I get Swashbuckle to produce Swagger-properties with date (without time)?如何让 Swashbuckle 生成带有日期(没有时间)的 Swagger 属性?
【发布时间】:2021-12-07 01:50:12
【问题描述】:

我有一个通过 Swashbuckle 公开的类,看起来像这样:

public class Person
{
     [JsonProperty("dateOfBirth")]
     [JsonConverter(typeof(DateFormatConverter))]
     public System.DateTime? DateOfBirth { get; set; }
}

internal class DateFormatConverter : Newtonsoft.Json.Converters.IsoDateTimeConverter
{
    public DateFormatConverter()
    {
        DateTimeFormat = "yyyy-MM-dd";
    }
}

(该类由 NSwag-studio 生成)

当我使用app.UseSwagger(); 与 Swashbuckle 生成 Swagger 合约时,结果如下所示:

"Person": {
    "dateOfBirth": {
      "format": "date-time",
      "type": "string"
    }
  }
}

我想配置 Swashbuckle 以识别我的 DateFormatConverter 类并相应地处理格式。

我在Startup 类中尝试了options.SchemaFilter<DateFormatSchemaFilter>(),但过滤器没有属性的上下文,所以除非我希望所有 DateTime 对象为date,否则这是这不是一个可行的选择。

【问题讨论】:

  • 你试过 iDocumentFilter 吗?

标签: asp.net date swagger datetime-format swashbuckle


【解决方案1】:

以下是如何使用 iDocumentFilter 将 "date-time" 更改为 "date"

private class Flip2DateDocumentFilter : IDocumentFilter
{
    private List<string> DateTypes(Type AttribType)
    {
        var list = new List<string>();
        foreach (var p in AttribType.GetProperties())
            if (p.CustomAttributes?.Count() > 0)
                list.Add(p.Name);
        return list;
    }

    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry s, IApiExplorer a)
    {
        var t = typeof(Person);
        if (swaggerDoc.definitions[t.Name] != null)
        {
            var dates = DateTypes(t);
            if (dates.Count() > 0)
                foreach (var p in swaggerDoc.definitions[t.Name].properties)
                    if (dates.Contains(p.Key) && p.Value.format == "date-time")
                        p.Value.format = "date";
        }
    }
}

该代码中的关键是 var t = typeof(Person);,它将被爬取以查找具有 CustomAttributes 的日期的类。

当然,这段代码并不是用来复制/粘贴的,只是你可以用IDocumentFilter做的一个小例子


另一种选择是使用 [NodaTime][1] 然后对那些应该只是日期的属性使用`NodaTime.LocalDate`,并在您的招摇配置中使用 MapType

c.MapType&lt;NodaTime.LocalDate&gt;(() =&gt; new Schema { type = "string", format = "date" });


在这个例子中我有这两个选项:
http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=Date#/Date/Date_Post

其背后的代码在 github 上:
https://github.com/heldersepu/Swagger-Net-Test/blob/master/Swagger_Test/App_Start/SwaggerConfig.cs

【讨论】:

  • 超级答案!我不知道IDocumentFilter,它完全符合我的需要。 NodaTime 也可能是一个长期目标。我最终通过迭代 foreach (var typeDef in swaggerDoc.Definitions) { foreach (var property in Type.GetType(namespace + "." + typeDef.Key) 然后检查 JsonPropertyAttribute 的 Swagger 属性名称和 JsonConverterAttribute 的格式来制作更通用的解决方案。
  • 很高兴我能帮上忙,是的,我的例子不是通用的,只是为了给你指明正确的方向......
  • c.MapType&lt;LocalDate&gt;(...) 似乎不适用于控制器方法参数?我的招摇文档仍然显示定义为 LocalDate 的参数的“日期时间”
  • @NickFranceschina 你有示例项目吗?也许您可以在单独的问题上发布一些示例代码
  • @HelderSepulveda - 我进行了进一步研究,结果发现这是一个未解决的问题......基于一些潜在的 MS ApiController 问题(看起来你过去已经回答过一个问这个问题的人?:@ 987654322@)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
相关资源
最近更新 更多