Swashbuckle 如何将 OneOf 声明添加到 OpenAPI 3 [英] Swashbuckle how to add OneOf declaration to OpenAPI 3
问题描述
我有一个请求对象,它可以是 2 种字符串类型A";或B".
I have a request object that can be of 2 string types "A" or "B".
注意:这是我真正想要的一个更简单的例子.枚举在这里对我不起作用.
public class SampleRequest
{
//Can only be "A" or "B"
public string Property1 { get; set; }
}
我正在尝试创建一个可以作为 OpenAPIOneOf"输出的模式过滤器.属性.
I am trying to create a schema filter that can output as the OpenAPI "OneOf" attribute.
https:///datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00#section-5.5.5
https://swagger.io/文档/规范/数据模型/oneof-anyof-allof-not/#oneof
https://github.com/domaindrivendev/Swashbuckle.AspNetCore#schema-filters
public class CustomSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
schema.OneOf = new List<OpenApiSchema>
{
new OpenApiSchema {Type = "string", Description = "A"},
new OpenApiSchema {Type = "string", Description = "B"}
};
}
}
运行 swagger 时,swagger-ui 正确呈现oneOf"说明:
When running the swagger, swagger-ui correctly renders the "oneOf" description:
oneOf: List [ OrderedMap { "type": "string", "description": "A" }, OrderedMap { "type": "string", "description": "B" } ]
然而,我希望它的价值看起来更像
However, I was expecting the value of it to look more like
oneOf: [ "A", "B" ]
这可能吗?阅读我的 swagger 文档的人不会知道 OrderedMap 列表是什么.
Is this possible? The people reading my swagger documentation aren't going to know what a List of OrderedMap is.
推荐答案
在 C# .NET Core 5 中:为了在编译时为您的 swagger 自动解析
,在您的 oneOf
(多态).jsonStartup.cs
中添加以下行:
In C# .NET Core 5: In order to automatically resolve oneOf
(polymorphism) at compile-time for your swagger.json
, add the following line in your Startup.cs
:
public void ConfigureServices(IServiceCollection services){
services.AddSwaggerGen(c => {c.UseOneOfForPolymorphism();})
}
对于其他所有内容,您可以遵循 OpenAPI 3.0 的文档.
For everything else, you can follow the documentation of OpenAPI 3.0.
这篇关于Swashbuckle 如何将 OneOf 声明添加到 OpenAPI 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!