From 0984b5195fec3e045ad55bfc2c24c08aadf8e3b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fele?= Date: Sun, 11 Feb 2024 22:53:29 +0100 Subject: [PATCH 1/4] Add support for IEndpointConventionBuilder.Finally in HttpChain --- src/Http/Wolverine.Http/HttpChain.EndpointBuilder.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Http/Wolverine.Http/HttpChain.EndpointBuilder.cs b/src/Http/Wolverine.Http/HttpChain.EndpointBuilder.cs index a6c00a233..0a277db40 100644 --- a/src/Http/Wolverine.Http/HttpChain.EndpointBuilder.cs +++ b/src/Http/Wolverine.Http/HttpChain.EndpointBuilder.cs @@ -16,6 +16,7 @@ namespace Wolverine.Http; public partial class HttpChain : IEndpointConventionBuilder { private readonly List> _builderConfigurations = new(); + private readonly List> _finallyBuilderConfigurations = new(); /// /// Configure ASP.Net Core endpoint metadata @@ -28,6 +29,11 @@ public void Add(Action convention) _builderConfigurations.Add(convention); } + public void Finally(Action finallyConvention) + { + _finallyBuilderConfigurations.Add(finallyConvention); + } + private bool tryApplyAsEndpointMetadataProvider(Type? type, RouteEndpointBuilder builder) { if (type != null && type.CanBeCastTo(typeof(IEndpointMetadataProvider))) @@ -56,6 +62,7 @@ public RouteEndpoint BuildEndpoint() establishResourceTypeMetadata(builder); foreach (var configuration in _builderConfigurations) configuration(builder); + foreach (var finallyConfiguration in _finallyBuilderConfigurations) finallyConfiguration(builder); foreach (var parameter in Method.Method.GetParameters()) { From 83e40acd6ffce65f35be54eab2e1515d5c72fd77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fele?= Date: Sun, 11 Feb 2024 22:53:56 +0100 Subject: [PATCH 2/4] Fix incorrect status-code 200 in PublishingEndpoints --- src/Http/Wolverine.Http/Runtime/PublishingEndpoint.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Http/Wolverine.Http/Runtime/PublishingEndpoint.cs b/src/Http/Wolverine.Http/Runtime/PublishingEndpoint.cs index 20482a28b..8bb783bab 100644 --- a/src/Http/Wolverine.Http/Runtime/PublishingEndpoint.cs +++ b/src/Http/Wolverine.Http/Runtime/PublishingEndpoint.cs @@ -20,8 +20,13 @@ public static void Configure(HttpChain chain) // a request directly to a Wolverine messaging endpoint for later processing chain.Metadata.Add(builder => { - // Adding and modifying data + // Adding metadata builder.Metadata.Add(new WolverineProducesResponseTypeMetadata { StatusCode = 202, Type = null }); + }); + // This is run after all other metadata has been applied, even after the wolverine built-in metadata + // So use this if you want to change or remove some metadata + chain.Metadata.Finally(builder => + { builder.RemoveStatusCodeResponse(200); }); } From a8292648df10892aa9bec57a42c70365d2b8280e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fele?= Date: Sun, 11 Feb 2024 23:22:59 +0100 Subject: [PATCH 3/4] Add test for the PublishEndpoint metadata modifications --- .../publishing_messages_from_http_endpoint.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Http/Wolverine.Http.Tests/publishing_messages_from_http_endpoint.cs b/src/Http/Wolverine.Http.Tests/publishing_messages_from_http_endpoint.cs index 4a86d74f2..46f9d94fe 100644 --- a/src/Http/Wolverine.Http.Tests/publishing_messages_from_http_endpoint.cs +++ b/src/Http/Wolverine.Http.Tests/publishing_messages_from_http_endpoint.cs @@ -1,3 +1,4 @@ +using Microsoft.AspNetCore.Http.Metadata; using Shouldly; using WolverineWebApi; @@ -21,4 +22,16 @@ public async Task publish_directly() tracked.Sent.SingleMessage() .Name.ShouldBe("Glenn Frey"); } + + [Fact] + public void endpoint_should_not_produce_status_code_200() + { + var endpoint = EndpointFor("/publish/message1"); + + // Status-code 202 should be added in the PublishingEndpoint + endpoint.Metadata.FirstOrDefault(x => x is IProducesResponseTypeMetadata m && m.StatusCode == 202).ShouldNotBeNull(); + + // And status-code 200 is removed + endpoint.Metadata.FirstOrDefault(x => x is IProducesResponseTypeMetadata m && m.StatusCode == 200).ShouldBeNull(); + } } \ No newline at end of file From 937a94df61c50a42fb86093d5bf0833206691b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fele?= Date: Sun, 11 Feb 2024 23:27:47 +0100 Subject: [PATCH 4/4] Update docs --- docs/guide/http/metadata.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/guide/http/metadata.md b/docs/guide/http/metadata.md index f4db00a70..34974176c 100644 --- a/docs/guide/http/metadata.md +++ b/docs/guide/http/metadata.md @@ -44,13 +44,18 @@ public static void Configure(HttpChain chain) // a request directly to a Wolverine messaging endpoint for later processing chain.Metadata.Add(builder => { - // Adding and modifying data + // Adding metadata builder.Metadata.Add(new WolverineProducesResponseTypeMetadata { StatusCode = 202, Type = null }); + }); + // This is run after all other metadata has been applied, even after the wolverine built-in metadata + // So use this if you want to change or remove some metadata + chain.Metadata.Finally(builder => + { builder.RemoveStatusCodeResponse(200); }); } ``` -snippet source | anchor +snippet source | anchor ## Swashbuckle and Wolverine