I trying to Add two custom MediaType to same method.
for adding this task we can use [Produces()] attribute, but if we want to have two GET method in same controller we get this error in 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
in Swashbuckle v5 beta we can use this IOperationFilter and add custom MediaType schema and use this code
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))
});
}
}
but we can not use this code in Swashbuckle v5.6.3
please help me to resolve this problem
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I Replece This Code And Solved My Problem.
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 });
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0