Swashbuckle v5.5.3 中的 IOperation 过滤器更改
IOperation Filter Change in Swashbuckle v5.5.3
我试图将两个自定义 MediaType 添加到同一方法。
为了添加这个任务,我们可以使用 [Produces()] 属性,但是如果我们想在同一个控制器中有两个 GET 方法,我们会在 swagger
中得到这个错误
SwaggerGeneratorException: Conflicting method/path combination "GET api/authors/{authorId}/books/{bookId}" for actions - Library.API.Controllers.BooksController.GetBook (Library.API),Library.API.Controllers.BooksController.GetBookWithConcatenatedAuthorName (Library.API). Actions require a unique method/path combination for Swagger/OpenAPI 3.0. Use ConflictingActionsResolver as a workaround
在 Swashbuckle v5 beta 中,我们可以使用此 IOperationFilter 并添加自定义 MediaType 架构并使用此代码
public class GetBookOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.OperationId != "GetBook")
{
return;
}
operation.Responses[StatusCodes.Status200OK.ToString()].Content.Add(
"application/vnd.marvin.bookwithconcatenatedauthorname+json",
new OpenApiMediaType()
{
Schema = context.SchemaRegistry.GetOrRegister(typeof(BookWithConcatenatedAuthorName))
});
}
}
但我们不能在 Swashbuckle v5.6.3 中使用此代码
请帮我解决这个问题
我替换了这段代码并解决了我的问题。
if (context.ApiDescription.ActionDescriptor.RouteValues["action"] != "GetBook")
{
return;
}
var schema = context.SchemaGenerator.GenerateSchema(typeof(BookWithConcatenatedAuthorName), context.SchemaRepository);
if (operation.Responses.Any(a=> a.Key==StatusCodes.Status200OK.ToString()))
{
operation.Responses[StatusCodes.Status200OK.ToString()].Content.Add(
"application/vnd.marvin.bookwithconcatenatedauthorname+json",
new OpenApiMediaType() { Schema = schema });
}
我试图将两个自定义 MediaType 添加到同一方法。 为了添加这个任务,我们可以使用 [Produces()] 属性,但是如果我们想在同一个控制器中有两个 GET 方法,我们会在 swagger
中得到这个错误SwaggerGeneratorException: Conflicting method/path combination "GET api/authors/{authorId}/books/{bookId}" for actions - Library.API.Controllers.BooksController.GetBook (Library.API),Library.API.Controllers.BooksController.GetBookWithConcatenatedAuthorName (Library.API). Actions require a unique method/path combination for Swagger/OpenAPI 3.0. Use ConflictingActionsResolver as a workaround
在 Swashbuckle v5 beta 中,我们可以使用此 IOperationFilter 并添加自定义 MediaType 架构并使用此代码
public class GetBookOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.OperationId != "GetBook")
{
return;
}
operation.Responses[StatusCodes.Status200OK.ToString()].Content.Add(
"application/vnd.marvin.bookwithconcatenatedauthorname+json",
new OpenApiMediaType()
{
Schema = context.SchemaRegistry.GetOrRegister(typeof(BookWithConcatenatedAuthorName))
});
}
}
但我们不能在 Swashbuckle v5.6.3 中使用此代码
请帮我解决这个问题
我替换了这段代码并解决了我的问题。
if (context.ApiDescription.ActionDescriptor.RouteValues["action"] != "GetBook")
{
return;
}
var schema = context.SchemaGenerator.GenerateSchema(typeof(BookWithConcatenatedAuthorName), context.SchemaRepository);
if (operation.Responses.Any(a=> a.Key==StatusCodes.Status200OK.ToString()))
{
operation.Responses[StatusCodes.Status200OK.ToString()].Content.Add(
"application/vnd.marvin.bookwithconcatenatedauthorname+json",
new OpenApiMediaType() { Schema = schema });
}