gpt4 book ai didi

asp.net-core - 如何在 ASP.NET Core 3 和 Swashbuckle 5 中为问题详细信息编写 ISchemaFilter?

转载 作者:行者123 更新时间:2023-12-04 11:41:29 25 4
gpt4 key购买 nike

我正在使用 ASP.NET Core 3.0 和 Swashbuckle 5。我正在尝试编写一个 ISchemaFilterProblemDetails在 ASP.NET Core 3.0 中。 ProblemDetails返回许多不同的状态代码,例如400, 401, 403, 406, 415, 500 等我想提供一个不同的例子 ProblemDetails取决于状态码。我怎样才能做到这一点?这是我为开始编写的一些代码,只需要填写空白:

public class ProblemDetailsSchemaFilter : ISchemaFilter
{
private static readonly OpenApiObject Status400ProblemDetails = new OpenApiObject()
{
["type"] = new OpenApiString("https://tools.ietf.org/html/rfc7231#section-6.5.1"),
["title"] = new OpenApiString("Bad Request"),
["status"] = new OpenApiInteger(StatusCodes.Status400BadRequest),
["traceId"] = new OpenApiString("00-982607166a542147b435be3a847ddd71-fc75498eb9f09d48-00"),
["errors"] = new OpenApiObject()
{
["property1"] = new OpenApiArray()
{
new OpenApiString("The property field is required"),
},
},
};

private static readonly OpenApiObject Status401ProblemDetails = new OpenApiObject()
{
["type"] = new OpenApiString("https://tools.ietf.org/html/rfc7235#section-3.1"),
["title"] = new OpenApiString("Unauthorized"),
["status"] = new OpenApiInteger(StatusCodes.Status401Unauthorized),
["traceId"] = new OpenApiString("00-982607166a542147b435be3a847ddd71-fc75498eb9f09d48-00"),
};

private static readonly OpenApiObject Status403ProblemDetails = new OpenApiObject()
{
["type"] = new OpenApiString("https://tools.ietf.org/html/rfc7231#section-6.5.3"),
["title"] = new OpenApiString("Forbidden"),
["status"] = new OpenApiInteger(StatusCodes.Status403Forbidden),
["traceId"] = new OpenApiString("00-982607166a542147b435be3a847ddd71-fc75498eb9f09d48-00"),
};

private static readonly OpenApiObject Status404ProblemDetails = new OpenApiObject()
{
["type"] = new OpenApiString("https://tools.ietf.org/html/rfc7231#section-6.5.4"),
["title"] = new OpenApiString("Not Found"),
["status"] = new OpenApiInteger(StatusCodes.Status404NotFound),
["traceId"] = new OpenApiString("00-982607166a542147b435be3a847ddd71-fc75498eb9f09d48-00"),
};

private static readonly OpenApiObject Status406ProblemDetails = new OpenApiObject()
{
["type"] = new OpenApiString("https://tools.ietf.org/html/rfc7231#section-6.5.6"),
["title"] = new OpenApiString("Not Acceptable"),
["status"] = new OpenApiInteger(StatusCodes.Status406NotAcceptable),
["traceId"] = new OpenApiString("00-982607166a542147b435be3a847ddd71-fc75498eb9f09d48-00"),
};

private static readonly OpenApiObject Status409ProblemDetails = new OpenApiObject()
{
["type"] = new OpenApiString("https://tools.ietf.org/html/rfc7231#section-6.5.8"),
["title"] = new OpenApiString("Conflict"),
["status"] = new OpenApiInteger(StatusCodes.Status409Conflict),
["traceId"] = new OpenApiString("00-982607166a542147b435be3a847ddd71-fc75498eb9f09d48-00"),
};

private static readonly OpenApiObject Status415ProblemDetails = new OpenApiObject()
{
["type"] = new OpenApiString("https://tools.ietf.org/html/rfc7231#section-6.5.13"),
["title"] = new OpenApiString("Unsupported Media Type"),
["status"] = new OpenApiInteger(StatusCodes.Status415UnsupportedMediaType),
["traceId"] = new OpenApiString("00-982607166a542147b435be3a847ddd71-fc75498eb9f09d48-00"),
};

private static readonly OpenApiObject Status422ProblemDetails = new OpenApiObject()
{
["type"] = new OpenApiString("https://tools.ietf.org/html/rfc4918#section-11.2"),
["title"] = new OpenApiString("Unprocessable Entity"),
["status"] = new OpenApiInteger(StatusCodes.Status422UnprocessableEntity),
["traceId"] = new OpenApiString("00-982607166a542147b435be3a847ddd71-fc75498eb9f09d48-00"),
};

private static readonly OpenApiObject Status500ProblemDetails = new OpenApiObject()
{
["type"] = new OpenApiString("https://tools.ietf.org/html/rfc7231#section-6.6.1"),
["title"] = new OpenApiString("Internal Server Error"),
["status"] = new OpenApiInteger(StatusCodes.Status500InternalServerError),
["traceId"] = new OpenApiString("00-982607166a542147b435be3a847ddd71-fc75498eb9f09d48-00"),
};

public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.ApiModel.Type == typeof(ProblemDetails))
{
// TODO: Set the default and example based on the status code.
// schema.Default = ???;
// schema.Example = ???;
}
}
}

最佳答案

这是一个简单的解决方法,如下所示:

1.型号:

public class ProblemDetails
{
public int ID { get; set; }
public string Description { get; set; }
}

2.ProblemDetailsS​​chemaFilter:
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.ApiModel.Type == typeof(ProblemDetails))
{
// TODO: Set the default and example based on the status code.
schema.Default = new OpenApiObject
{
["ID"] = new OpenApiInteger(2),
["Description"] = new OpenApiString("test")
};
schema.Example = new OpenApiObject
{
["ID"] = new OpenApiInteger(1),
["Description"] = new OpenApiString("test test")
};
}
}

3.Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
c.SchemaFilter<ProblemDetailsSchemaFilter>();
});
}

您可以显示 schema.Example在 Swagger 用户界面上:
enter image description here

并使用 schema.Default 生成 json :
enter image description here

关于asp.net-core - 如何在 ASP.NET Core 3 和 Swashbuckle 5 中为问题详细信息编写 ISchemaFilter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58681260/

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