Skip to content

Commit 4c28589

Browse files
committed
Add support for SuppressApi to close #34068
1 parent 145a8d1 commit 4c28589

File tree

6 files changed

+91
-2
lines changed

6 files changed

+91
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using Microsoft.AspNetCore.Http;
6+
7+
namespace Microsoft.AspNetCore.Routing
8+
{
9+
/// <summary>
10+
/// Specifies an endpoint name in <see cref="Endpoint.Metadata"/>.
11+
/// </summary>
12+
/// <remarks>
13+
/// Endpoint names must be unique within an application, and can be used to unambiguously
14+
/// identify a desired endpoint for URI generation using <see cref="LinkGenerator"/>.
15+
/// </remarks>
16+
public interface ISuppressApiMetadata
17+
{
18+
/// <summary>
19+
/// Gets the endpoint name.
20+
/// </summary>
21+
bool SuppressApi { get; }
22+
}
23+
}

src/Http/Routing/src/PublicAPI.Unshipped.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ Microsoft.AspNetCore.Routing.EndpointNameAttribute
3535
Microsoft.AspNetCore.Routing.EndpointNameAttribute.EndpointNameAttribute(string! endpointName) -> void
3636
Microsoft.AspNetCore.Routing.EndpointNameAttribute.EndpointName.get -> string!
3737
static Microsoft.AspNetCore.Builder.RoutingEndpointConventionBuilderExtensions.WithName<TBuilder>(this TBuilder builder, string! endpointName) -> TBuilder
38-
static Microsoft.AspNetCore.Builder.RoutingEndpointConventionBuilderExtensions.WithGroupName<TBuilder>(this TBuilder builder, string! endpointGroupName) -> TBuilder
38+
static Microsoft.AspNetCore.Builder.RoutingEndpointConventionBuilderExtensions.WithGroupName<TBuilder>(this TBuilder builder, string! endpointGroupName) -> TBuilder
39+
Microsoft.AspNetCore.Routing.ISuppressApiMetadata
40+
Microsoft.AspNetCore.Routing.ISuppressApiMetadata.SuppressApi.get -> bool
41+
Microsoft.AspNetCore.Routing.SuppressApiMetadata
42+
Microsoft.AspNetCore.Routing.SuppressApiMetadata.SuppressApiMetadata() -> void
43+
Microsoft.AspNetCore.Routing.SuppressApiMetadata.SuppressApi.get -> bool
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using Microsoft.AspNetCore.Http;
6+
7+
namespace Microsoft.AspNetCore.Routing
8+
{
9+
/// <summary>
10+
/// Specifies an endpoint name in <see cref="Endpoint.Metadata"/>.
11+
/// </summary>
12+
/// <remarks>
13+
/// Endpoint names must be unique within an application, and can be used to unambiguously
14+
/// identify a desired endpoint for URI generation using <see cref="LinkGenerator"/>.
15+
/// </remarks>
16+
public sealed class SuppressApiMetadata : ISuppressApiMetadata
17+
{
18+
/// <summary>
19+
/// Gets the endpoint name.
20+
/// </summary>
21+
public bool SuppressApi => true;
22+
}
23+
}

src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public void OnProvidersExecuting(ApiDescriptionProviderContext context)
5252
{
5353
if (endpoint is RouteEndpoint routeEndpoint &&
5454
routeEndpoint.Metadata.GetMetadata<MethodInfo>() is { } methodInfo &&
55-
routeEndpoint.Metadata.GetMetadata<IHttpMethodMetadata>() is { } httpMethodMetadata)
55+
routeEndpoint.Metadata.GetMetadata<IHttpMethodMetadata>() is { } httpMethodMetadata &&
56+
(routeEndpoint.Metadata.GetMetadata<ISuppressApiMetadata>() == null ||
57+
routeEndpoint.Metadata.GetMetadata<ISuppressApiMetadata>() is { SuppressApi: false} ))
5658
{
5759
// REVIEW: Should we add an ApiDescription for endpoints without IHttpMethodMetadata? Swagger doesn't handle
5860
// a null HttpMethod even though it's nullable on ApiDescription, so we'd need to define "default" HTTP methods.

src/Mvc/Mvc.ApiExplorer/test/EndpointMetadataApiDescriptionProviderTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,28 @@ public void RespectsProducesWithGroupNameExtensionMethod()
409409
Assert.Equal(endpointGroupName, apiDescription.GroupName);
410410
}
411411

412+
[Fact]
413+
public void RespectsSuppressApiMethod()
414+
{
415+
// Arrange
416+
var builder = new TestEndpointRouteBuilder(new ApplicationBuilder(null));
417+
builder.MapGet("/api/todos", () => "").Produces<InferredJsonClass>().SuppressApi();
418+
var context = new ApiDescriptionProviderContext(Array.Empty<ActionDescriptor>());
419+
420+
var endpointDataSource = builder.DataSources.OfType<EndpointDataSource>().Single();
421+
var hostEnvironment = new HostEnvironment
422+
{
423+
ApplicationName = nameof(EndpointMetadataApiDescriptionProviderTest)
424+
};
425+
var provider = new EndpointMetadataApiDescriptionProvider(endpointDataSource, hostEnvironment, new ServiceProviderIsService());
426+
427+
// Act
428+
provider.OnProvidersExecuting(context);
429+
430+
// Assert
431+
Assert.Empty(context.Results);
432+
}
433+
412434
private IList<ApiDescription> GetApiDescriptions(
413435
Delegate action,
414436
string pattern = null,

src/Mvc/Mvc.Core/src/Builder/OpenApiEndpointConventionBuilderExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Microsoft.AspNetCore.Builder;
55
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.AspNetCore.Routing;
67

78
namespace Microsoft.AspNetCore.Http
89
{
@@ -11,6 +12,19 @@ namespace Microsoft.AspNetCore.Http
1112
/// </summary>
1213
public static class OpenApiEndpointConventionBuilderExtensions
1314
{
15+
/// <summary>
16+
/// Adds metadata to support suppressing OpenAPI documentation from
17+
/// being generated for this endpoint.
18+
/// </summary>
19+
/// <param name="builder">The <see cref="IEndpointConventionBuilder"/>.</param>
20+
/// <returns>A <see cref="IEndpointConventionBuilder"/> that can be used to further customize the endpoint.</returns>
21+
public static IEndpointConventionBuilder SuppressApi(this IEndpointConventionBuilder builder)
22+
{
23+
builder.WithMetadata(new SuppressApiMetadata());
24+
25+
return builder;
26+
}
27+
1428
/// <summary>
1529
/// Adds metadata indicating the type of response an endpoint produces.
1630
/// </summary>

0 commit comments

Comments
 (0)