-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to define two response examples for the same class #188
Comments
Hi, did you find a way? I have the same problem, one response class for different status codes |
Okay, i just implemented custom IOperationFilter because my responses the same for each method My example: public class ApiResponsesOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
foreach (var response in operation.Responses)
{
if (int.TryParse(response.Key, out var httpCode))
{
var content = response.Value.Content.FirstOrDefault();
if (content.Value is null) continue;
var example = GetApiError(httpCode);
if (example is null) continue;
content.Value.Example = new OpenApiString(JsonSerializer.Serialize(example));
}
}
}
public static ApiError? GetApiError(int httpCode) => httpCode switch
{
StatusCodes.Status401Unauthorized => new ApiError(httpCode, ErrorDescription.UnAuthorized, ErrorDescription.UnAuthorized),
StatusCodes.Status403Forbidden => new ApiError(httpCode, ErrorDescription.Forbidden, ErrorDescription.Forbidden),
StatusCodes.Status500InternalServerError => new ApiError(httpCode, ErrorDescription.Internal, ErrorDescription.Internal),
_ => null
};
} |
I can't reproduce this. I tried using @scrapstation's example: namespace WebApi.Controllers
{
[SwaggerResponse(200)]
[SwaggerResponse(400, Type = typeof(RemoteServiceErrorInfo))]
[SwaggerResponseExample(400, typeof(ClentErrorExamples))]
[SwaggerResponse(500, Type = typeof(RemoteServiceErrorInfo))]
[SwaggerResponseExample(500, typeof(ServerErrorExamples))]
public class HomeController : ControllerBase
{
[HttpGet("home/index")]
public IActionResult Index()
{
return NoContent();
}
}
public class ServerErrorExamples : IExamplesProvider<RemoteServiceErrorInfo>
{
public RemoteServiceErrorInfo GetExamples()
{
return new RemoteServiceErrorInfo
{
Code = "ServerErrorExamples"
};
}
}
public class RemoteServiceErrorInfo
{
public string Code { get; internal set; }
}
public class ClentErrorExamples : IExamplesProvider<RemoteServiceErrorInfo>
{
public RemoteServiceErrorInfo GetExamples()
{
return new RemoteServiceErrorInfo
{
Code = "ClentErrorExamples"
};
}
}
} If someone can provide example code which I can copy and paste to reproduce, I can investigate further. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The response model of 400 and 500 is the same class:
RemoteServiceErrorInfo
But the example is different .
so, I defind two ExamplesProvider are as fallows
but the swagger shows the same example in 400 and 500
The text was updated successfully, but these errors were encountered: