在 Swagger 中设置示例
Setting up examples in Swagger
我正在使用 Swashbuckle.AspNetCore.Swagger (1.0.0) 和 Swashbuckle.AspNetCore.SwaggerGen (1.0.0)。我正在尝试将默认示例添加到 之后的 API。我创建了一个新的 class 文件并添加了
public class SwaggerDefaultValue : Attribute
{
public string ParameterName { get; set; }
public string Value { get; set; }
public SwaggerDefaultValue(string parameterName, string value)
{
this.ParameterName = parameterName;
this.Value = value;
}
}
public class AddDefaultValues : IOperationFilter
{
public void Apply(Operation operation, DataTypeRegistry dataTypeRegistry, ApiDescription apiDescription)
{
foreach (var param in operation.Parameters)
{
var actionParam = apiDescription.ActionDescriptor.GetParameters().First(p => p.ParameterName == param.Name);
if (actionParam != null)
{
var customAttribute = actionParam.ActionDescriptor.GetCustomAttributes<SwaggerDefaultValue>().FirstOrDefault();
if (customAttribute != null)
{
param.DefaultValue = customAttribute.Value;
}
}
}
}
}
但我收到此错误 - AddDefaultValues does not implement interface member IOperationFilter.Apply(Operation, OperationFilterContext)
您关注的 link 不适用于 Swashbuckle.AspNetCore 版本
在正确的项目中查找正确的示例:
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/search?q=IOperationFilter&unscoped_q=IOperationFilter
我正在使用 Swashbuckle.AspNetCore.Swagger (1.0.0) 和 Swashbuckle.AspNetCore.SwaggerGen (1.0.0)。我正在尝试将默认示例添加到
public class SwaggerDefaultValue : Attribute
{
public string ParameterName { get; set; }
public string Value { get; set; }
public SwaggerDefaultValue(string parameterName, string value)
{
this.ParameterName = parameterName;
this.Value = value;
}
}
public class AddDefaultValues : IOperationFilter
{
public void Apply(Operation operation, DataTypeRegistry dataTypeRegistry, ApiDescription apiDescription)
{
foreach (var param in operation.Parameters)
{
var actionParam = apiDescription.ActionDescriptor.GetParameters().First(p => p.ParameterName == param.Name);
if (actionParam != null)
{
var customAttribute = actionParam.ActionDescriptor.GetCustomAttributes<SwaggerDefaultValue>().FirstOrDefault();
if (customAttribute != null)
{
param.DefaultValue = customAttribute.Value;
}
}
}
}
}
但我收到此错误 - AddDefaultValues does not implement interface member IOperationFilter.Apply(Operation, OperationFilterContext)
您关注的 link 不适用于 Swashbuckle.AspNetCore 版本
在正确的项目中查找正确的示例: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/search?q=IOperationFilter&unscoped_q=IOperationFilter