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 support for IEndpointConventionBuilder.Finally and fix status-code 200 in PublishingEndpoint #724

Merged
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
9 changes: 7 additions & 2 deletions docs/guide/http/metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
```
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Http/Wolverine.Http/Runtime/PublishingEndpoint.cs#L15-L29' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_programmatic_one_off_openapi_metadata' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Http/Wolverine.Http/Runtime/PublishingEndpoint.cs#L15-L34' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_programmatic_one_off_openapi_metadata' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Swashbuckle and Wolverine
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Http.Metadata;
using Shouldly;
using WolverineWebApi;

Expand All @@ -21,4 +22,16 @@ public async Task publish_directly()
tracked.Sent.SingleMessage<HttpMessage1>()
.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();
}
}
7 changes: 7 additions & 0 deletions src/Http/Wolverine.Http/HttpChain.EndpointBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Wolverine.Http;
public partial class HttpChain : IEndpointConventionBuilder
{
private readonly List<Action<EndpointBuilder>> _builderConfigurations = new();
private readonly List<Action<EndpointBuilder>> _finallyBuilderConfigurations = new();

/// <summary>
/// Configure ASP.Net Core endpoint metadata
Expand All @@ -28,6 +29,11 @@ public void Add(Action<EndpointBuilder> convention)
_builderConfigurations.Add(convention);
}

public void Finally(Action<EndpointBuilder> finallyConvention)
{
_finallyBuilderConfigurations.Add(finallyConvention);
}

private bool tryApplyAsEndpointMetadataProvider(Type? type, RouteEndpointBuilder builder)
{
if (type != null && type.CanBeCastTo(typeof(IEndpointMetadataProvider)))
Expand Down Expand Up @@ -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())
{
Expand Down
7 changes: 6 additions & 1 deletion src/Http/Wolverine.Http/Runtime/PublishingEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down
Loading