Skip to content

Commit

Permalink
WN9 ProblemValidation on route groups /a (#33330)
Browse files Browse the repository at this point in the history
* WN9 ProblemValidation on route groups /a

* WN9 ProblemValidation on route groups /a

* fix

* fix

* fix

* fix
  • Loading branch information
Rick-Anderson authored Aug 13, 2024
1 parent e48f231 commit 54ce817
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
#if NEVER
#elif FIRST
// <snippet_1>
using Microsoft.AspNetCore.Http;

var app = WebApplication.Create();

var todos = app.MapGroup("/todos")
.ProducesProblem();
.ProducesProblem(StatusCodes.Status500InternalServerError);

todos.MapGet("/", () => new Todo(1, "Create sample app", false));
todos.MapPost("/", (Todo todo) => Results.Ok(todo));
Expand All @@ -18,14 +16,12 @@ record Todo(int Id, string Title, bool IsCompleted);
// </snippet_1>
#elif SECOND
// <snippet_2>
using Microsoft.AspNetCore.Http;

var app = WebApplication.Create();

app.MapGet("/", () =>
{
var extensions = new List<KeyValuePair<string, object>> { new("test", "value") };
return TypedResults.Problem("This is an error with extensions",
var extensions = new List<KeyValuePair<string, object?>> { new("test", "value") };
return TypedResults.Problem("This is an error with extensions",
extensions: extensions);
});
// </snippet_2>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
## Support calling `ProducesProblem` and `ProducesValidationProblem` on route groups
<!-- Add this include to the OpenAPI section
[!INCLUDE[](~/release-notes/aspnetcore-9/includes/prob_validation.md)]
-->
### Support calling `ProducesProblem` and `ProducesValidationProblem` on route groups

The `ProducesProblem` and `ProducesValidationProblem` extension methods have been updated to support application on route groups, as in the sample below. These methods can be used to indicate that all endpoints in a route group can return `ProblemDetails` or `ValidationProblemDetails` responses for the purposes of OpenAPI metadata.
The [ProducesProblem](/dotnet/api/microsoft.aspnetcore.http.openapiroutehandlerbuilderextensions.producesproblem) and [ProducesValidationProblem](/dotnet/api/microsoft.aspnetcore.http.openapiroutehandlerbuilderextensions.producesvalidationproblem) extension methods have been updated for route groups. These methods can be used to indicate that all endpoints in a route group can return `ProblemDetails` or `ValidationProblemDetails` responses for the purposes of OpenAPI metadata.

:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/ProducesProblem/Program.cs" id="snippet_1" :::

## `Problem` and `ValidationProblem` result types support construction with `IEnumerable<KeyValuePair<string, object?>>` values
### `Problem` and `ValidationProblem` result types support construction with `IEnumerable<KeyValuePair<string, object?>>` values

Prior to this preview, constructing `Problem` and `ValidationProblem` result types in minimal APIs required that the `errors` and `extensions` properties be initialized with an implementation of `IDictionary<string, object?>`. Starting in this release, these construction APIs support overloads that consume `IEnumerable<KeyValuePair<string, object?>>`.
Prior to .NET 9, constructing [Problem](/dotnet/api/microsoft.aspnetcore.http.typedresults.problem) and [ValidationProblem](/dotnet/api/microsoft.aspnetcore.http.typedresults.validationproblem) result types in minimal APIs required that the `errors` and `extensions` properties be initialized with an implementation of `IDictionary<string, object?>`. In this release, these construction APIs support overloads that consume `IEnumerable<KeyValuePair<string, object?>>`.

:::code language="csharp" source="~/fundamentals/openapi/samples/9.x/ProducesProblem/Program.cs" id="snippet_2" :::

Thanks to GitHub user "joegoldman2" for this contribution!
Thanks to GitHub user [joegoldman2](https://github.com/joegoldman2) for this contribution!

0 comments on commit 54ce817

Please sign in to comment.