From 0cf9c70767404cbad044ee16543d235434955d73 Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Fri, 18 Oct 2024 15:33:57 +0200 Subject: [PATCH 1/2] Add ProducesDefaultProblem method to RouteHandlerBuilder #64 Introduce a new extension method `ProducesDefaultProblem` for the `RouteHandlerBuilder` class to simplify defining common error responses. Enhance `README.md` with documentation and examples for the new method. --- README.md | 20 +++++++++++++ .../Endpoints/PeopleEndpoints.cs | 8 +++++- .../RouteHandlerBuilderExtensions.cs | 7 +++-- .../RouteHandlerBuilderExtensions.cs | 28 +++++++++++++++++++ .../RouteHandlerBuilderExtensions.cs | 7 +++-- 5 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 src/MinimalHelpers.OpenApi/RouteHandlerBuilderExtensions.cs diff --git a/README.md b/README.md index 6fa7184..3f04001 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,26 @@ endpoints.MapPost("login", LoginAsync) }); ``` + ***Extension methods for RouteHandlerBuilder*** + + Often we have endpoints with multiple 4xx return values, each of which produces a `ProblemDetails` response: + + ```csharp + endpoints.MapGet("/api/people/{id:guid}", Get) + .ProducesProblem(StatusCodes.Status400BadRequest) + .ProducesProblem(StatusCodes.Status401Unauthorized) + .ProducesProblem(StatusCodes.Status403Forbidden) + .ProducesProblem(StatusCodes.Status404NotFound); + ``` + + To avoid multiple calls to `ProducesProblem`, we can use the `ProducesDefaultProblem` extension method provided by the library: + + ```csharp +endpoints.MapGet("/api/people/{id:guid}", Get) + .ProducesDefaultProblem(StatusCodes.Status400BadRequest, StatusCodes.Status401Unauthorized, + StatusCodes.Status403Forbidden, StatusCodes.Status404NotFound); + ``` + ## MinimalHelpers.Validation [![Nuget](https://img.shields.io/nuget/v/MinimalHelpers.Validation)](https://www.nuget.org/packages/MinimalHelpers.Validation) diff --git a/samples/MinimalSample/Endpoints/PeopleEndpoints.cs b/samples/MinimalSample/Endpoints/PeopleEndpoints.cs index f8420f5..c4e5176 100644 --- a/samples/MinimalSample/Endpoints/PeopleEndpoints.cs +++ b/samples/MinimalSample/Endpoints/PeopleEndpoints.cs @@ -1,4 +1,5 @@ using System.ComponentModel.DataAnnotations; +using MinimalHelpers.OpenApi; using MinimalHelpers.Validation; namespace MinimalSample.Endpoints; @@ -9,7 +10,12 @@ public static void MapEndpoints(IEndpointRouteBuilder endpoints) { endpoints.MapGet("/api/people", GetList); - endpoints.MapGet("/api/people/{id:guid}", Get); + endpoints.MapGet("/api/people/{id:guid}", Get) + //.ProducesProblem(StatusCodes.Status400BadRequest) + //.ProducesProblem(StatusCodes.Status401Unauthorized) + //.ProducesProblem(StatusCodes.Status403Forbidden) + //.ProducesProblem(StatusCodes.Status404NotFound); + .ProducesDefaultProblem(StatusCodes.Status400BadRequest, StatusCodes.Status401Unauthorized, StatusCodes.Status403Forbidden, StatusCodes.Status404NotFound); endpoints.MapPost("/api/people", Insert) // MinimalHelpers.Validation package performs validation with Data Annotations. diff --git a/src/MinimalHelpers.FluentValidation/RouteHandlerBuilderExtensions.cs b/src/MinimalHelpers.FluentValidation/RouteHandlerBuilderExtensions.cs index 8146235..317873c 100644 --- a/src/MinimalHelpers.FluentValidation/RouteHandlerBuilderExtensions.cs +++ b/src/MinimalHelpers.FluentValidation/RouteHandlerBuilderExtensions.cs @@ -10,12 +10,13 @@ namespace MinimalHelpers.FluentValidation; public static class RouteHandlerBuilderExtensions { /// - /// Adds to the a filter that validates the object. - /// + /// Registers a of type onto the route handler to validate the object. + /// /// The type of the object to validate. /// The to add validation filter to. - /// The with the added validation filter. + /// A that can be used to further customize the route handler. /// The validation is performed using FluentValidation. + /// public static RouteHandlerBuilder WithValidation(this RouteHandlerBuilder builder) where T : class { builder.AddEndpointFilter>() diff --git a/src/MinimalHelpers.OpenApi/RouteHandlerBuilderExtensions.cs b/src/MinimalHelpers.OpenApi/RouteHandlerBuilderExtensions.cs new file mode 100644 index 0000000..2bb0c90 --- /dev/null +++ b/src/MinimalHelpers.OpenApi/RouteHandlerBuilderExtensions.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace MinimalHelpers.OpenApi; + +/// +/// Extension methods for . +/// +/// +public static class RouteHandlerBuilderExtensions +{ + /// + /// Adds to the specified list of status codes as responses. + /// + /// The . + /// The list of status codes to be added as responses. + /// The with the new status codes responses. + public static RouteHandlerBuilder ProducesDefaultProblem(this RouteHandlerBuilder builder, params int[] statusCodes) + { + foreach (var statusCode in statusCodes) + { + builder.ProducesProblem(statusCode); + } + + return builder; + } +} diff --git a/src/MinimalHelpers.Validation/RouteHandlerBuilderExtensions.cs b/src/MinimalHelpers.Validation/RouteHandlerBuilderExtensions.cs index 3dc644c..a046514 100644 --- a/src/MinimalHelpers.Validation/RouteHandlerBuilderExtensions.cs +++ b/src/MinimalHelpers.Validation/RouteHandlerBuilderExtensions.cs @@ -10,12 +10,13 @@ namespace MinimalHelpers.Validation; public static class RouteHandlerBuilderExtensions { /// - /// Adds to the a filter that validates the object. - /// + /// Registers a of type onto the route handler to validate the object. + /// /// The type of the object to validate. /// The to add validation filter to. - /// The with the added validation filter. + /// A that can be used to further customize the route handler. /// The validation is performed with Data annotations, using MiniValidation. + /// public static RouteHandlerBuilder WithValidation(this RouteHandlerBuilder builder) where T : class { builder.AddEndpointFilter>() From 6035255ab841b01dc6c6cbca7eeeae7ff6487773 Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Fri, 18 Oct 2024 15:35:22 +0200 Subject: [PATCH 2/2] Update XML docs for RouteHandlerBuilderExtensions --- .../RouteHandlerBuilderExtensions.cs | 2 +- src/MinimalHelpers.Validation/RouteHandlerBuilderExtensions.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MinimalHelpers.FluentValidation/RouteHandlerBuilderExtensions.cs b/src/MinimalHelpers.FluentValidation/RouteHandlerBuilderExtensions.cs index 317873c..7919cea 100644 --- a/src/MinimalHelpers.FluentValidation/RouteHandlerBuilderExtensions.cs +++ b/src/MinimalHelpers.FluentValidation/RouteHandlerBuilderExtensions.cs @@ -11,7 +11,7 @@ public static class RouteHandlerBuilderExtensions { /// /// Registers a of type onto the route handler to validate the object. - /// + /// /// The type of the object to validate. /// The to add validation filter to. /// A that can be used to further customize the route handler. diff --git a/src/MinimalHelpers.Validation/RouteHandlerBuilderExtensions.cs b/src/MinimalHelpers.Validation/RouteHandlerBuilderExtensions.cs index a046514..aef24e4 100644 --- a/src/MinimalHelpers.Validation/RouteHandlerBuilderExtensions.cs +++ b/src/MinimalHelpers.Validation/RouteHandlerBuilderExtensions.cs @@ -11,7 +11,7 @@ public static class RouteHandlerBuilderExtensions { /// /// Registers a of type onto the route handler to validate the object. - /// + /// /// The type of the object to validate. /// The to add validation filter to. /// A that can be used to further customize the route handler.