看来我正在寻找的是 IOpertationFilter。将其与自定义属性相结合,我能够创建我需要的内容,以便动态地手动将自定义参数添加到 Swagger 文档中。请参阅下面的所有相关代码,请注意 Schema/PartialSchema 有很多属性,我只设置了我需要的类型,其他情况可能需要更多。
SwaggerParameterAttribute.cs
using System;
using Microsoft.AspNetCore.Mvc.Filters;
using Swashbuckle.AspNetCore.Swagger;
/// <summary>
/// Types of Swagger parameters
/// </summary>
public enum SwaggerParamType {Body, NonBody};
/// <summary>
/// Attribute to facilitate manually adding a parameter to auto-generated Swagger documentation
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class SwaggerParameterAttribute : ActionFilterAttribute {
/// <summary>
/// Swagger parameter to inject
/// </summary>
public IParameter Parameter { get; set; }
/// <summary>
/// Default constructor
/// </summary>
/// <param name="ParamType">Type of Swagger parameter (Body/NonBody)</param>
/// <param name="Name">Name of the parameter</param>
/// <param name="Type">Primitive type associated with the parameter (int, bool, string, etc.)</param>
/// <param name="In">Location of the parameter (path, query, etc.)</param>
/// <param name="Description">Description of the parameter</param>
/// <param name="Required">Whether the parameter is required or not (true/false)</param>
public SwaggerParameterAttribute(SwaggerParamType ParamType, String Name, String Type, String In, String Description = "", Boolean Required = false){
switch (ParamType) {
case SwaggerParamType.Body:
Parameter = new BodyParameter() { Name = Name, In = In, Description = Description, Required = Required, Schema = new Schema() { Type = Type } };
break;
case SwaggerParamType.NonBody:
Parameter = new NonBodyParameter() { Name = Name, In = In, Description = Description, Required = Required };
((PartialSchema)Parameter).Type = Type;
break;
default:
throw new ArgumentOutOfRangeException("Invalid Swagger parameter type specified.");
}
}
SwaggerOperationFilter.cs
using System;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.Controllers;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using Whatever.NameSpace.Your.Attribute.Is.In;
/// <summary>
/// Custom Swagger Operation Filter
/// </summary>
public class SwaggerOperationFilter : IOperationFilter {
public void Apply(Operation operation, OperationFilterContext context) {
//Check for [SwaggerParameter] add defined parameter to the parameter list
foreach (Attribute attribute in ((ControllerActionDescriptor)context.ControllerActionDescriptor).MethodInfo.GetCustomAttributes()) {
if (attribute.GetType() == typeof(SwaggerParameterAttribute)) {
operation.Parameters.Add(((SwaggerParameterAttribute)attribute).Parameter);
}
}
}
}
Startup.cs(只是swagger操作过滤器部分)
using Swashbuckle.AspNetCore.Swagger;
using Whatever.NameSpace.Your.Filter.Is.In;
public void ConfigureServices(IServiceCollection services) {
services.AddSwaggerGen(options => {
options.OperationFilter<SwaggerOperationFilter>();
}
}
SomeController.cs(使用示例)
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using Whatever.NameSpace.Your.Attribute.Is.In;
[HttpGet("this/{that}/the/{other}")]
[SwaggerParameter(ParamType: SwaggerParamType.NonBody, Name: "param1", Type: "string", In: "query", Description: "Some description of param1 here")]
[SwaggerParameter(SwaggerParamType.NonBody, "param2", "string", "query", "Some description of param2 here")]
public override IActionResult GetWhatever(String that, String other) => base.GetWhatever(that, other);