[英]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);
}
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 "
引号必须使用"
进行转义。
/// <summary>
/// List of errors
/// </summary>
///<example>["entry1","entry2","entry3"]</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.