Skip to content

Commit

Permalink
new docs for 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Jul 17, 2023
1 parent 9ba0712 commit 83dc7bc
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
27 changes: 26 additions & 1 deletion docs/guide/http/fluentvalidation.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,29 @@ opts.UseFluentValidationProblemDetailMiddleware();

as shown in context below in an application shown below:

snippet: sample_using_configure_endpoints
<!-- snippet: sample_using_configure_endpoints -->
<a id='snippet-sample_using_configure_endpoints'></a>
```cs
app.MapWolverineEndpoints(opts =>
{
// This is strictly to test the endpoint policy

opts.ConfigureEndpoints(httpChain =>
{
// The HttpChain model is a configuration time
// model of how the HTTP endpoint handles requests
// This adds metadata for OpenAPI
httpChain.WithMetadata(new CustomMetadata());
httpChain.WithTags("wolverine");
});

// more configuration for HTTP...
// Opting into the Fluent Validation middleware from
// Wolverine.Http.FluentValidation
opts.UseFluentValidationProblemDetailMiddleware();
```
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Program.cs#L80-L103' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_configure_endpoints' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
33 changes: 32 additions & 1 deletion docs/guide/http/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,38 @@ continue working against that entity data you just loaded. To help remove boiler
support for this pattern using the standard `[Required]` attribute on the parameters of the inputs to the HTTP handler
methods. Here's an example that tries to apply an update to an existing `Todo` entity:

snippet: sample_update_with_required_entity
<!-- snippet: sample_update_with_required_entity -->
<a id='snippet-sample_update_with_required_entity'></a>
```cs
public record UpdateRequest(string Name, bool IsComplete);

public static class UpdateEndpoint
{
// Find required Todo entity for the route handler below
public static Task<Todo?> LoadAsync(int id, IDocumentSession session)
=> session.LoadAsync<Todo>(id);

[WolverinePut("/todos/{id:int}")]
public static StoreDoc<Todo> Put(
// Route argument
int id,

// The request body
UpdateRequest request,

// Entity loaded by the method above,
// but note the [Required] attribute
[Required] Todo? todo)
{
todo.Name = request.Name;
todo.IsComplete = request.IsComplete;

return MartenOps.Store(todo);
}
}
```
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Samples/TodoController.cs#L123-L152' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_update_with_required_entity' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

You'll notice that the `LoadAsync()` method is looking up the `Todo` entity for the route parameter, where Wolverine would
normally be passing that value to the matching `Todo` parameter of the main `Put` method. In this case though, because of
Expand Down
29 changes: 28 additions & 1 deletion docs/guide/http/problemdetails.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,34 @@ To make that clearer, here's the generated code:

And for more context, here's the matching "happy path" and "sad path" tests for the endpoint above:

sample: sample_testing_problem_details_behavior
<!-- snippet: sample_testing_problem_details_behavior -->
<a id='snippet-sample_testing_problem_details_behavior'></a>
```cs
[Fact]
public async Task continue_happy_path()
{
// Should be good
await Scenario(x =>
{
x.Post.Json(new NumberMessage(3)).ToUrl("/problems");
});
}

[Fact]
public async Task stop_with_problems_if_middleware_trips_off()
{
// This is the "sad path" that should spawn a ProblemDetails
// object
var result = await Scenario(x =>
{
x.Post.Json(new NumberMessage(10)).ToUrl("/problems");
x.StatusCodeShouldBe(400);
x.ContentTypeShouldBe("application/problem+json");
});
}
```
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Http/Wolverine.Http.Tests/problem_details_usage_in_http_middleware.cs#L18-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_testing_problem_details_behavior' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Lastly, if Wolverine sees the existence of a `ProblemDetails` return value in any middleware, Wolverine will fill in OpenAPI
metadata for the "application/problem+json" content type and a status code of 400. This behavior can be easily overridden
Expand Down

0 comments on commit 83dc7bc

Please sign in to comment.