【问题标题】:Setting up examples in Swagger在 Swagger 中设置示例
【发布时间】:2018-07-05 22:01:18
【问题描述】:

我正在使用 Swashbuckle.AspNetCore.Swagger (1.0.0) 和 Swashbuckle.AspNetCore.SwaggerGen (1.0.0)。我正在尝试在Default model example in Swashbuckle (Swagger) 之后向我的 API 添加默认示例。我创建了一个新的类文件并添加了,

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)

【问题讨论】:

    标签: asp.net-core swagger swashbuckle


    【解决方案1】:

    您所关注的链接不适用于Swashbuckle.AspNetCore 版本

    在正确的项目中查找正确的示例: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/search?q=IOperationFilter&unscoped_q=IOperationFilter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-18
      • 2021-05-19
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      相关资源
      最近更新 更多