Skip to content

Commit

Permalink
OpenAPI creation respects [ExcludeFromDescription]. Closes GH-674
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Jan 2, 2024
1 parent 29a6bbc commit 2c7ca03
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Http/Wolverine.Http.Tests/swashbuckle_integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Swashbuckle.AspNetCore.Swagger;

namespace Wolverine.Http.Tests;

Expand All @@ -19,6 +20,19 @@ public async Task wolverine_stuff_is_in_the_document()
var doc = results.ReadAsText();

doc.ShouldContain("/fromservice");

doc.ShouldNotContain("/ignore");
}

[Fact]
public void ignore_endpoint_methods_that_are_marked_with_ExcludeFromDescription()
{
HttpChains.Chains.Any(x => x.RoutePattern.RawText == "/ignore").ShouldBeTrue();

var generator = Host.Services.GetRequiredService<ISwaggerProvider>();
var doc = generator.GetSwagger("v1");

doc.Paths.Any(x => x.Key == "/ignore").ShouldBeFalse();
}


Expand Down
7 changes: 7 additions & 0 deletions src/Http/Wolverine.Http/WolverineApiDescriptionProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using JasperFx.Core.Reflection;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Routing;
Expand All @@ -25,6 +26,12 @@ public void OnProvidersExecuting(ApiDescriptionProviderContext context)
{
if (endpoint is RouteEndpoint routeEndpoint && routeEndpoint.Metadata.GetMetadata<HttpChain>() is {} chain)
{
if (chain.Method.HandlerType.HasAttribute<ExcludeFromDescriptionAttribute>() ||
chain.Method.Method.HasAttribute<ExcludeFromDescriptionAttribute>())
{
continue;
}

foreach (var httpMethod in chain.HttpMethods)
{
context.Results.Add(chain.CreateApiDescription(httpMethod));
Expand Down
12 changes: 12 additions & 0 deletions src/Http/WolverineWebApi/IgnoredEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Wolverine.Http;

namespace WolverineWebApi;

public class IgnoredEndpoint
{
[WolverineGet(("/ignore")), ExcludeFromDescription]
public string Ignore()
{
return "nothing";
}
}

0 comments on commit 2c7ca03

Please sign in to comment.