gpt4 book ai didi

c# - Swashbuckle OperationFilter 添加类型对象的参数

转载 作者:行者123 更新时间:2023-12-05 08:39:58 28 4
gpt4 key购买 nike

我有一个 C# 对象

public class MyObject
{
public int property1 { get; set; }
public string property2 { get; set; }
public string property3 { get; set; }
}

我想创建一个具有 MyObject 模式的对象类型的参数。

public class MyFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
operation.Parameters.Add(new OpenApiParameter
{
Name = "MyParameter",
Description = "MyDescription"
In = ParameterLocation.Header
Required = true,
Schema = new OpenApiSchema()
{
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>()
{
// MyObject
}
}
}
}
}

Swashbuckle 是否提供任何可以自动生成具有MyObject 架构的属性的方法。

最佳答案

我找到了答案。

public class MyFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var myObjectSchema = context.SchemaGenerator.GenerateSchema(typeof(MyObject), context.SchemaRepository);
operation.Parameters.Add(new OpenApiParameter
{
Name = "MyParameter",
Description = "MyDescription"
In = ParameterLocation.Header
Required = true,
Schema = myObjectSchema
}
}
}

关于c# - Swashbuckle OperationFilter 添加类型对象的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57793938/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com