简体   繁体   English

如何使用 Swashbuckle 记录有条件要求的财产

[英]How to document conditionally required property with Swashbuckle

We have the following request model:我们有以下请求 model:

public class SequenceRequest<TPayload>: SequenceRequest
{
    public TPayload Payload { get; set; }
}

The Payload property is conditionally required based on the type of the TPayload.根据 TPayload 的类型,有条件地需要Payload属性。

Is there a way how can I set the Required property of the OpenApiParameter based on that, eg using the IOperationFilter?有没有一种方法可以基于此设置 OpenApiParameter 的 Required 属性,例如使用 IOperationFilter?

I can see a lot of information in the OperationFilterContext describing the request, but I am not sure what I need to override.我可以在 OperationFilterContext 中看到很多描述请求的信息,但我不确定我需要覆盖什么。

Could anybody provide an example, please?请问有人可以举个例子吗?

Finally, I got a solution for my case using a schema filter.最后,我使用模式过滤器为我的案例找到了解决方案。

public class PayloadRequiredSchemaFilter : ISchemaFilter
{
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        // from the model definition we know, the type is generic
        // and derived from a non-generic SequenceRequest
        if (context.Type.IsGenericType && context.Type.IsSubclassOf(typeof(SequenceRequest)))
        {
            // from the model we know it has one generic argument
            var argumentType = context.Type.GenericTypeArguments.First();

            // in our case the Payload is not required when it is of the SequencePayload type
            if (argumentType.Equals(typeof(SequencePayload)))
            {
                return;
            }

            // the Payload is required
            // we know the property name
            // however, the list of properties is serialized => the key may change based on the serializer options
            var propertyName = schema.Properties.Single(p => argumentType.Name.Equals(p.Value.Reference?.Id)).Key;
            // mark the property as required
            schema.Required.Add(propertyName);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据对象的使用有条件地保留 object 属性“必需”? - How to keep object property “Required” conditionally, depending on object's use? 如何在 Swashbuckle 中更改 POST 和 PUT 的必填字段? - How to vary the required fields for POST and PUT in Swashbuckle? 如何配置 Swashbuckle 以忽略模型上的属性 - How to configure Swashbuckle to ignore property on model swashbuckle文档csrf令牌。 如何通过? 文件吗? - swashbuckle document csrf token. how to pass it? document it? 有条件地使MVC类属性/类成为必需 - Make MVC class property/class required conditionally 使用数据注释的条件必需属性 - Conditionally required property using data annotations Swashbuckle Swagger UI:如何从xml注释中的参数中删除必需的 - Swashbuckle Swagger UI: How to remove required from parameters in xml commenting 如何使用Swashbuckle记录查询字符串参数? - How can I document query string parameters with Swashbuckle? 使用 [FromQuery] 时如何记录 Swagger/Swashbuckle 参数描述 - How to document Swagger/Swashbuckle parameter descriptions when using [FromQuery] 如何将架构过滤器添加到一个 Swashbuckle api 文档版本 - How to add a Schema Filter to just one Swashbuckle api document version
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM