Skip to content
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

Add ProducesDefaultProblem method to RouteHandlerBuilder #65

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
/// 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.
/// 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
Loading