swagger Swashbuckle如何将OneOf声明添加到OpenAPI 3

fumotvh3  于 2023-04-06  发布在  其他
关注(0)|答案(2)|浏览(176)

我有一个请求对象,可以是2个字符串类型“A”或“B”。

  • 注意:这是我真正想要的一个简单的例子。枚举在这里不适用。*
public class SampleRequest
    {
        //Can only be "A" or "B"
        public string Property1 { get; set; }
    }

我正在尝试创建一个模式过滤器,它可以输出为OpenAPI的“OneOf”属性。
https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00#section-5.5.5
https://swagger.io/docs/specification/data-models/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”描述:

oneOf: List [ OrderedMap { "type": "string", "description": "A" }, OrderedMap { "type": "string", "description": "B" } ]

然而,我期望它的价值看起来更像

oneOf: [ "A", "B" ]

这可能吗?阅读我的swagger文档的人不会知道OrderedMap的List是什么。

vohkndzv

vohkndzv1#

在C# .NET Core 5中:为了在swagger.json的编译时自动解析oneOf(多态性),请在Startup.cs中添加以下行:

public void ConfigureServices(IServiceCollection services){
     services.AddSwaggerGen(c => {c.UseOneOfForPolymorphism();})
}

对于其他所有内容,您可以遵循the documentation of OpenAPI 3.0

bqujaahr

bqujaahr2#

我用SchemaFilter解决这个问题:

public class SampleRequest
    {
        //Can only be "A" or "B"
        public string Property1 { get; set; }
    }
    public class SampleRequestSchemaFilter : ISchemaFilter
    {
        public void Apply(
            OpenApiSchema schema,
            SchemaFilterContext context
        )
        {
            if (context.Type == typeof(SampleRequest))
            {
                var sampleProperty = schema.Properties.FirstOrDefault(
                    c => string.Equals(
                        c.Key,
                        nameof(SampleRequest.Property1),
                        StringComparison.InvariantCultureIgnoreCase
                    )
                );
                if (sampleProperty.Value != null)
                {
                    var oneOfList = new List<OpenApiSchema>();
                    var aSchema = context.SchemaGenerator.GenerateSchema(typeof(A),context.SchemaRepository);
                    if (aSchema != null)
                    {
                        oneOfList.Add(aSchema);
                    }
                    var bSchema = context.SchemaGenerator.GenerateSchema(typeof(B),context.SchemaRepository);
                    if (bSchema != null)
                    {
                        oneOfList.Add(bSchema);
                    }
                    if (oneOfList.Any())
                    {
                        var oneOfSchema = new OpenApiSchema
                        {
                            Type = "object",
                            OneOf = oneOfList
                        };
                        schema.Properties.Remove(sampleProperty.Key);
                        schema.Properties.Add(sampleProperty.Key, oneOfSchema);
                    }
                }
            }
        }
    }

我正在使用这个版本的软件包:

<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />

相关问题