Skip to content
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
10 changes: 10 additions & 0 deletions src/Http/Wolverine.Http.Tests/todo_endpoint_specs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@ await Scenario(opts =>
opts.StatusCodeShouldBe(404);
});
}

[Fact]
public async Task get_an_automatic_404_when_related_entity_does_not_exist_with_validation()
{
await Scenario(opts =>
{
opts.Put.Json(new UpdateRequest("Second", true)).ToUrl("/todos3/1222222222");
opts.StatusCodeShouldBe(404);
});
}
}
5 changes: 3 additions & 2 deletions src/Http/Wolverine.Http/Policies/RequiredEntityPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ public void Apply(IReadOnlyList<HttpChain> chains, GenerationRules rules, IServi

foreach (var parameter in requiredParameters)
{
var frame = new SetStatusCodeAndReturnIfEntityIsNullFrame(parameter.ParameterType);
chain.Middleware.Add(frame);
var loadFrame = chain.Middleware.First(m => m.Creates.Any(x => x.VariableType == parameter.ParameterType));
var stopFrame = new SetStatusCodeAndReturnIfEntityIsNullFrame(parameter.ParameterType);
chain.Middleware.Insert(chain.Middleware.IndexOf(loadFrame) + 1, stopFrame);
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions src/Http/WolverineWebApi/Samples/TodoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,35 @@ public static Todo Put(int id, UpdateRequest request, Todo todo, IDocumentSessio
return todo;
}
}

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

public static IResult Validate([Required] Todo? todo)
{
return todo.IsComplete
? Results.ValidationProblem([new("IsComplere", ["Completed Todo cannot be updated"])])
: WolverineContinue.Result();
}

[WolverinePut("/todos3/{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);
}
}
Loading