简体   繁体   English

数组类型<example>标签不适用于 Swagger (swashbuckle.aspnetcore)</example>

[英]Array types with <example> tag not working with Swagger (swashbuckle.aspnetcore)

I am using the summary and example tags for the swagger documentation.我正在使用 swagger 文档的摘要和示例标签。 I have a problem with the tag, it's not recognized by swagger when I use array:我的标签有问题,当我使用数组时,swagger 无法识别它:

I use swashbuckle.aspnetcore package Nuget.我使用 swashbuckle.aspnetcore package Nuget。

Example:例子:

[DataContract]
    public class HeaderResponse
    {
        /// <summary>
        /// Statut code
        /// </summary>
        ///<example>400</example>
        [DataMember]
        public int StatusCode { get; set; }
        /// <summary>
        /// Title
        /// </summary>
        /// <example>Erreur sur methode1</example>
        [DataMember]
        public string Title { get; set; }
        /// <summary>
        /// List of errors 
        /// </summary>
        ///<example>["entry1", "entry2", "entry3"]</example>
        [DataMember]
        public List<string> ErrorList { get; } = new List<string>();
}

On swagger documentation, array is not interpreted:在 swagger 文档中,未解释数组:

在此处输入图像描述

I found others solutions by using ISchemaFilter like this:我通过使用 ISchemaFilter 找到了其他解决方案,如下所示:

public class SwaggerExcludeFilter : ISchemaFilter
    {
        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {
            switch (context.Type.Name)
            {
                case "HeaderResponse":
                    foreach (var property in schema.Properties)
                    {
                        if (property.Value.Type == "array")
                        {
                            var array = new OpenApiArray();
                            array.Add(new OpenApiString("item1"));
                            array.Add(new OpenApiString("item2"));
                            array.Add(new OpenApiString("item3"));

                            property.Value.Example = array;
                        }
                    }
                    break;
            }
        }
    }

Is there no other way than to use ISchemaFilter to handle tags of type array?除了使用 ISchemaFilter 来处理数组类型的标签之外,没有其他方法吗?

You can try to do like this:您可以尝试这样做:

public List<string> ErrorList { get; } = new List<string>{"entry1", "entry2", "entry3"};

or:或者:

[DataContract]
public class HeaderResponse
{
    public HeaderResponse()
    {
        ErrorList = new List<string> {"entry1", "entry2", "entry3" };
    }
    /// <summary>
    /// Statut code
    /// </summary>
    ///<example>400</example>
    [DataMember]
    public int StatusCode { get; set; }
    /// <summary>
    /// Title
    /// </summary>
    /// <example>Erreur sur methode1</example>
    [DataMember]
    public string Title { get; set; }
    /// <summary>
    /// List of errors 
    /// </summary>
    [DataMember]
    public List<string> ErrorList { get; set; }
}

Here is a demo:这是一个演示:

[HttpPost("TestPar")]
        public IActionResult TestPar(HeaderResponse h)
        {
            return Json(h);
        }

result:结果: 在此处输入图像描述

I have the same problem and I try this.我有同样的问题,我试试这个。 you can add a condition on the name of the property and add the null return in order not to lose the example on the other properties.您可以在属性名称上添加条件并添加 null 返回,以免丢失其他属性的示例。 But the best solution would be to be able to find a generic solution.但最好的解决方案是能够找到一个通用的解决方案。 How is it possible to get the example tag and the property name with ISchemaFilter ?如何使用 ISchemaFilter 获取示例标记和属性名称? This is my solution :这是我的解决方案:

public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
    schema.Example = GetExampleOrNullFor(schema, context);
}

private IOpenApiAny GetExampleOrNullFor(OpenApiSchema schema, SchemaFilterContext context)
{

switch (context.Type.Name)
{
    case "MyClass":
        foreach (var property in schema.Properties)
        {
            //Array property, which wraps its elements
            if (property.Value.Type == "array")
            {
                if (property.Key == "MyProperty1")
                {
                    var array = new OpenApiArray();
                    array.Add(new OpenApiString("item1"));
                    array.Add(new OpenApiString("item2"));
                    property.Value.Example = array;
                }
                else if (property.Key == "MyProperty2")
                {
                    var array = new OpenApiArray();
                    array.Add(new OpenApiString("item1"));
                    property.Value.Example = array;
                }
            }
        }
        return null;
    default:
        return null;
}

}

The quotes must be escaped using &quot;引号必须使用&quot;进行转义。

/// <summary>
/// List of errors 
/// </summary>
///<example>[&quot;entry1&quot;,&quot;entry2&quot;,&quot;entry3&quot;]</example>
[DataMember]
public List<string> ErrorList { get; } = new List<string>();

Support for examples using XML comments on arrays was added in Swashbuckle.AspNetCore version 6.0.0. Swashbuckle.AspNetCore 版本 6.0.0 中添加了对在数组上使用 XML 注释的示例的支持。

It's mentioned in this GitHub issue and their documentation shows the following example: 这个 GitHub 问题中提到了它,他们的文档显示了以下示例:

/// <summary>
/// The sizes the product is available in
/// </summary>
/// <example>["Small", "Medium", "Large"]</example>
public List<string> Sizes { get; set; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 迁移到 Swashbuckle.AspNetCore 版本 5 时,Swagger UI 中的不记名身份验证 - Bearer authentication in Swagger UI, when migrating to Swashbuckle.AspNetCore version 5 多版本控制 URL 在 Swashbuckle.AspNetCore 5.0.0rc 中不起作用 - Multiple versioning URL not working in Swashbuckle.AspNetCore 5.0.0rc 如何在 ASP.NET Core Swagger (Swashbuckle.AspNetCore) 中定义控制器描述? - How to define controller descriptions in ASP.NET Core Swagger (Swashbuckle.AspNetCore)? 在 Swashbuckle.AspNetCore 中,Swagger JSON 和使用 Results.Ok 时生成的 UI 没有内容类型 - In Swashbuckle.AspNetCore, Swagger JSON and UI generated without content type when Results.Ok is used 如何使用 Swashbuckle.AspNetCore 在 Swagger 模式中将自定义泛型类型公开为字符串 - How to expose a custom generic type as a string in Swagger schema using Swashbuckle.AspNetCore 可选参数在Swashbuckle.AspNetCore中导致空异常 - Optional parameter causes null exception in Swashbuckle.AspNetCore Swashbuckle.AspNetCore-无法为继承基本控制器的控制器生成json - Swashbuckle.AspNetCore - Unable to generate json for controllers that inherit base controllers Swashbuckle.AspNetCore openapi 架构中未正确显示可空属性 - Nullable property is not presented in the Swashbuckle.AspNetCore openapi schema properly Swashbuckle.AspNetCore (5.0.0-rc5) 忽略公共字段 - Swashbuckle.AspNetCore (5.0.0-rc5) ignoring public fields 如何使用Swashbuckle.AspNetCore隐藏响应代码200? - How can I hide response code 200 with Swashbuckle.AspNetCore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM