【问题标题】:Display (values of) DataAnnotations - Swashbuckle - Swagger page显示(值)DataAnnotations - Swashbuckle - Swagger 页面
【发布时间】:2020-05-27 16:20:13
【问题描述】:

是否可以在 Swagger 页面上显示参数的 DataAnnotations,例如最小值和最大值?

我已经尝试将 ShowExtensions 和 ShowCommonExtension 设置为 true 或调用 ShowExtensions(),但并没有解决问题。 Range 属性和/或例如Swagger 页面上不显示字符串的 MaxLength(20)。

启动(代码 sn-p)

    app.UseSwaggerUI(
        options =>
        {
            options.ConfigObject = new ConfigObject
            {
                ShowCommonExtensions = true,
                ShowExtensions = true
            };

            options.ConfigObject.AdditionalItems.Add("showCommonExtensions", true);

            options.ShowExtensions();

框架/包

  • .NET Core 3.1
  • Swashbuckle.AspNetCore (5.4.1)
  • Swashbuckle.AspNetCore.SwaggerGen (5.4.1)

控制器(代码 sn-p)

 [HttpGet]
 public IEnumerable<string> GetMethod([Required] [Range(1, 10)] int value)
 {

Swagger 用户界面

【问题讨论】:

  • @HelderSepulveda 感谢您的回复。这个问题似乎与 OpenApi 规范的 V2 有关。提供的示例是使用 V2 而不是 OAS3,我正在使用的版本。
  • 你能扩展一下“OpenApi 规范问题”吗?两者都有最大值和最小值的选项
  • @HelderSepulveda 请在 Swashbuckle.AspNetCore 的问题页面上查看更多详细信息 - #1683
  • 能否展示一下你正在使用的swagger-ui的版本,这里有获取方法的说明:swagger.io/docs/open-source-tools/swagger-ui/usage/…

标签: asp.net-core swagger swagger-ui swashbuckle swashbuckle.aspnetcore


【解决方案1】:

我做了更多的挖掘......

他们确实检查了多个属性:

https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/316ddd0fe6768f470c274a0e93c789b65cf658b9/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs#L12

if (attribute is DefaultValueAttribute defaultValue && defaultValue.Value != null)
{
    schema.Default = OpenApiAnyFactory.CreateFor(schema, defaultValue.Value);
}
else if (attribute is RegularExpressionAttribute regex)
{
    schema.Pattern = regex.Pattern;
}
else if (attribute is RangeAttribute range)
{
    schema.Maximum = decimal.TryParse(range.Maximum.ToString(), out decimal maximum)
        ? maximum
        : schema.Maximum;

    schema.Minimum = decimal.TryParse(range.Minimum.ToString(), out decimal minimum)
        ? minimum
        : schema.Minimum;
}

但我看到的唯一使用范围的 UnitTest 是在模型上:

https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/47684caeeaaeb4d887236e61a2ea35e4dc76b958/test/Swashbuckle.AspNetCore.TestSupport/Fixtures/DataAnnotatedViaMetadataType.cs#L26

    public class MetadataType
    {
        [Required]
        public string StringWithRequired { get; set; }

        [Required]
        public int IntWithRequired { get; set; }

        [Range(1, 12)]
        public int IntWithRange { get; set; }

        [RegularExpression("^[3-6]?\\d{12,15}$")]
        public string StringWithRegularExpression { get; set; }
    }

如果这是您真正关心的问题,请填写错误报告:
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/new

【讨论】:

  • 感谢您为所描述的问题付出的努力和时间。我在 Swashbuckle.AspNetCore 的 GitHub 页面上创建了一个问题。也许你可以看看它,因为它包含额外的信息:#1683
  • @Odrai 我用您在问题中提供的内容创建了一个 swagger json ...在我的 swagger-ui 版本下看起来不错:swagger-net-test.azurewebsites.net/swagger/ui/index?url=https://… 一定是他们(Swashbuckle.AspNetCore)正在使用旧版本
  • 感谢您为我指明了正确的方向。该问题出现在当前可用的 Swashbuckle.AspNetCore (v. 5.4.1) 的 NuGet 包中。我克隆了当前的主源代码(版本 5.4.2)并添加了对测试项目的引用。经过一些测试,我可以说他们在最新(主)版本中解决了这个问题。我们只需要等待一个新的 NuGet 版本。最重要的部分是 Swashbuckle 处理 ConfigObject 的 ShowCommonExtensions 属性的方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-04
  • 2021-05-07
  • 1970-01-01
相关资源
最近更新 更多