-
Notifications
You must be signed in to change notification settings - Fork 107
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
Features/api description #92
Features/api description #92
Conversation
I`m not quite sure why the build fails, but I think it may not be something that I can fix in a source code. |
@ardalis Gentle ping |
Sorry for the delay. I like this! Can you fix the conflict (I'm not sure what to do with the 2nd one in the file)? Also, I feel like there would need to be additional docs on how to do this. You've already provided great explanations in the issue description - can you put that into the README as part of this PR, too? Thanks! |
…ix unit tests after code merge; Expand functional tests to validate failure responses
Let me know when ready to merge |
Hi @ardalis, I`m glad you've liked the idea! I've fixed merge conflicts, but there's couple of things I wanted to mention:
private static ActionResult BadRequest(ControllerBase controller, IResult result)
{
foreach (var error in result.ValidationErrors)
{
controller.ModelState.AddModelError(error.Identifier, error.ErrorMessage);
}
return controller.BadRequest(controller.ModelState); //Ultimately returns Dictionary<string, string[]>
} to private static ValidationProblemDetails BadRequest(ControllerBase controller, IResult result)
{
foreach (var error in result.ValidationErrors)
{
controller.ModelState.AddModelError(error.Identifier, error.ErrorMessage);
}
return new ValidationProblemDetails(controller.ModelState); //Returns ProblemDetails
} Because there was a discrepancy before, where BadRequest returned by asp net core (for example at the model binding/model validation stage) would have a
|
Hi @ardalis Everything is ready for merge on my side, you can proceed if you are ok with the things I mentioned |
Noticed some activity here and fixed merge conflicts |
Thanks, @Artem-Romanenia ! We'll get this in the next release. |
@Artem-Romanenia Thanks for the contribution, I have just merged this into the .NET 7 branch and have verified the changes are working as expected. We should be able to get a release of this out next week. |
By default, Asp Net Core and Api Explorer know nothing about
[TranslateResultToActionResult]
and what it is doing. This PR is an attempt to address this issue and its consequences.Here, a whole
Result<>
object is described as response type, instead of just it`s value. 'Failure' responses are not described. This can be addressed by adding bunch of [ProducesResponseType] attributes to each endpoint.I tried to come up with a soultion that would allow to fix this with just couple of lines of code (or just one line) in Startup file:
With just this code Swagger description of the same operation turns into this:
By default, it will add all known result statuses to a description of every endpoint marked with
[TranslateResultToActionResult]
.If, for example, there is an app that doesn't have authentication or authorization, we can configure it like this:
This will not include description of 401 Unauthorized or 403 Forbidden in any endpoint.
Another example is when you have an endpoint that can only return 200, 404, and 400, while another returns only 200 and 400. Here you can use
[ExpectedFailures]
attribute:You can also configure it to list different Status codes for different Http Methods:
Here it would use 200 OK for GET, but 201 for POST and 204 for DELETE
Another configurability feature is what is returned in case of failure:
Also, this whole thing can be used together with [ProducesResponseType] attributes, as it will not overwrite/duplicate them if they are added as actual attributes.
If you feel this needs any changes or lacks something useful, please let me know!