Skip to content

Commit

Permalink
Add ProducesDefaultProblem method to RouteHandlerBuilder #64
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
marcominerva committed Oct 18, 2024
1 parent 32201bb commit 0cf9c70
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion samples/MinimalSample/Endpoints/PeopleEndpoints.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using MinimalHelpers.OpenApi;
using MinimalHelpers.Validation;

namespace MinimalSample.Endpoints;
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ namespace MinimalHelpers.FluentValidation;
public static class RouteHandlerBuilderExtensions
{
/// <summary>
/// Adds to the <see cref="RouteHandlerBuilder"/> a filter that validates the <typeparamref name="T"/> object.
/// </summary>
/// Registers a <seealso cref="ValidatorFilter{T}"/> of type <typeparamref name="T"/> onto the route handler to validate the <typeparamref name="T"/> object.
/// </summary>
/// <typeparam name="T">The type of the object to validate.</typeparam>
/// <param name="builder">The <see cref="RouteHandlerBuilder"/> to add validation filter to.</param>
/// <returns>The <see cref="RouteHandlerBuilder"/> with the added validation filter.</returns>
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the route handler.</returns>
/// <remarks>The validation is performed using <a href="https://fluentvalidation.net">FluentValidation</a>.</remarks>
/// <seealso cref="ValidatorFilter{T}"/>
public static RouteHandlerBuilder WithValidation<T>(this RouteHandlerBuilder builder) where T : class
{
builder.AddEndpointFilter<ValidatorFilter<T>>()
Expand Down
28 changes: 28 additions & 0 deletions src/MinimalHelpers.OpenApi/RouteHandlerBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace MinimalHelpers.OpenApi;

/// <summary>
/// Extension methods for <see cref="RouteHandlerBuilder"/>.
/// </summary>
/// <seealso cref="RouteHandlerBuilder"/>
public static class RouteHandlerBuilderExtensions
{
/// <summary>
/// Adds to <see cref="RouteHandlerBuilder"/> the specified list of status codes as <see cref="ProblemDetails"/> responses.
/// </summary>
/// <param name="builder">The <see cref="RouteHandlerBuilder"/>.</param>
/// <param name="statusCodes">The list of status codes to be added as <see cref="ProblemDetails"/> responses.</param>
/// <returns>The <see cref="RouteHandlerBuilder"/> with the new status codes responses.</returns>
public static RouteHandlerBuilder ProducesDefaultProblem(this RouteHandlerBuilder builder, params int[] statusCodes)
{
foreach (var statusCode in statusCodes)
{
builder.ProducesProblem(statusCode);
}

return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ namespace MinimalHelpers.Validation;
public static class RouteHandlerBuilderExtensions
{
/// <summary>
/// Adds to the <see cref="RouteHandlerBuilder"/> a filter that validates the <typeparamref name="T"/> object.
/// </summary>
/// Registers a <seealso cref="ValidatorFilter{T}"/> of type <typeparamref name="T"/> onto the route handler to validate the <typeparamref name="T"/> object.
/// </summary>
/// <typeparam name="T">The type of the object to validate.</typeparam>
/// <param name="builder">The <see cref="RouteHandlerBuilder"/> to add validation filter to.</param>
/// <returns>The <see cref="RouteHandlerBuilder"/> with the added validation filter.</returns>
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the route handler.</returns>
/// <remarks>The validation is performed with Data annotations, using <a href="https://github.com/DamianEdwards/MiniValidation">MiniValidation</a>.</remarks>
/// <seealso cref="ValidatorFilter{T}"/>
public static RouteHandlerBuilder WithValidation<T>(this RouteHandlerBuilder builder) where T : class
{
builder.AddEndpointFilter<ValidatorFilter<T>>()
Expand Down

0 comments on commit 0cf9c70

Please sign in to comment.