Mat*_*egg 5 c# asp.net-web-api swashbuckle redoc
我负责维护公司API文档。我们的 API 是用 ASP.NET 编写的。我最近改用 Swashbuckle 5.6.0,它运行得很好。
我遇到的问题是这样的:
我们将数据模型分为 Post 数据和 Get 数据,例如WebAccountGetData.cs和WebAccountPostData.cs。创建(POST)和更新(PUT)时可以使用Post数据。
Post 数据类中的大多数(如果不是全部)字段都可以为 null,当调用 API 方法时,存储过程会返回描述缺少/需要哪些字段的错误消息。API 不处理必填字段。
使用可为 null 的字段意味着 Swashbuckle 不会向文档添加必需标志。但我们想根据使用的 Http 方法(Post/Put)来显示某个字段是否为必填字段。

API 密钥是必需参数,因为它不可为空。
我知道我可以使用[Required]System.ComponentModel.DataAnnotations 命名空间中的属性,但这会将必需标志应用于 POST 和 PUT 方法,这是我们不希望的。
理想情况下,我想使用自定义属性,在其中可以指定 Post 或 Put 方法中是否需要某个字段。
public class ApiRequiredAttribute : Attribute
{
public bool RequiredInPost
{
get;
set;
} = false;
public bool RequiredInPut
{
get;
set;
} = false;
}
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
[ApiRequired(RequiredInPost = true)]
public int? ApprovalStatusId
{
get;
set;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使用自定义IDocumentFilter或IOperationFilter将ISchemaFilter更改(例如切换所需标志)应用于模型字段的架构属性?或者在 Swashbuckle 中不可能引用模型上的属性吗?
我已经设法找到解决方案!
我创建了一个IOperationFilter基于具有我的自定义ApiRequired属性的属性为 POST 和 PUT 方法创建新架构的方法(请参阅原始问题)。
internal class ApplyRequiredAttributeFilter : IOperationFilter
{
public void Apply( Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription )
{
HttpParameterBinding[] parameterBindings = apiDescription.ActionDescriptor.ActionBinding.ParameterBindings;
foreach ( HttpParameterBinding binding in parameterBindings ) {
PropertyInfo[] properties = binding.Descriptor.ParameterType.GetProperties();
// If the type is not an object and has no properties, ignore it.
if ( properties.Length == 0 ) {
continue;
}
Parameter modelParamater = operation.parameters.Last();
if ( modelParamater == null ) {
continue;
}
string schemaPath = modelParamater.schema?.@ref;
string schemaName = schemaPath?.Split( '/' ).Last();
if ( schemaName == null ) {
continue;
}
Schema oldSchema = schemaRegistry.Definitions[ schemaName ];
// Copy the existing schema.
Schema newSchema = new Schema
{
description = oldSchema.description,
properties = new Dictionary<string, Schema>( oldSchema.properties ),
required = oldSchema.required != null ? new List<string>( oldSchema.required ) : new List<string>(),
@type = oldSchema.type,
vendorExtensions = new Dictionary<string, object>( oldSchema.vendorExtensions )
};
// Find model properties with the custom attribute.
foreach ( PropertyInfo property in properties ) {
ApiRequiredAttribute attribute = property.GetCustomAttribute<ApiRequiredAttribute>();
if ( attribute != null ) {
// If the model property is required in POST/PUT and current HTTP method is POST/PUT
// Add the property to the new schema's required flags.
if ( attribute.RequiredInPut && apiDescription.HttpMethod.Method.Equals( "PUT" ) ||
attribute.RequiredInPost && apiDescription.HttpMethod.Method.Equals( "POST" ) ) {
newSchema.required.Add( property.Name );
string newSchemaName = $"{schemaName}:{apiDescription.HttpMethod.Method}";
if ( !schemaRegistry.Definitions.ContainsKey( newSchemaName ) ) {
schemaRegistry.Definitions.Add( newSchemaName, newSchema );
}
// Change the current model schema reference to the new schema with the addition required flags.
modelParamater.schema.@ref = $"{schemaPath}:{apiDescription.HttpMethod.Method}";
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我将过滤器添加到我的 EnableSwagger 调用中。
GlobalConfiguration.Configuration
.EnableSwagger("docs/swagger/", c =>
{
// Other initialization code...
c.OperationFilter<ApplyRequiredAttributeFilter>();
});
Run Code Online (Sandbox Code Playgroud)
属性的使用方式如下:
[ApiRequired( RequiredInPost = true, RequiredInPut = true)]
public bool? Active
{
get;
set;
}
[ApiRequired( RequiredInPost = true )]
public string UserName
{
get;
set;
}
Run Code Online (Sandbox Code Playgroud)
最后,在文档中,所需的标志如下所示。POST 方法参数在左侧,PUT 方法参数在右侧: