Skip to content

Add IgnoreApi() extension method for IEndpointConventionBuilder #34068

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

Closed
Tracked by #34514
martincostello opened this issue Jul 3, 2021 · 6 comments · Fixed by #34906
Closed
Tracked by #34514

Add IgnoreApi() extension method for IEndpointConventionBuilder #34068

martincostello opened this issue Jul 3, 2021 · 6 comments · Fixed by #34906
Assignees
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Priority:1 Work that is critical for the release, but we could probably ship without
Milestone

Comments

@martincostello
Copy link
Member

Background and Motivation

Related to #34061, if using minimal hosting with minimal actions and Swashbuckle and it is possible to hide actions from API Explorer using the [ApiExplorerSettings] attribute (which doesn't work right now, see also #34065), it would be a quality-of-life improvement to the API surface to add an extension method for IEndpointConventionBuilder to make it require less verbose code to hide it.

Proposed API

Add a new extension method to the Microsoft.AspNetCore.Mvc.ApiExplorer assembly with a name something along the lines of IgnoreApi():

namespace Microsoft.AspNetCore.Builder
{
+    public static class ApiExplorerEndpointConventionBuilderExtensions
+    {
+       public static TBuilder IgnoreApi<TBuilder>(this TBuilder builder) where TBuilder : IEndpointConventionBuilder;
+    }
}

The implementation would be something like this:

using Microsoft.AspNetCore.Mvc;

namespace Microsoft.AspNetCore.Builder
{
    /// <summary>
    /// API Explorer extension methods for <see cref="IEndpointConventionBuilder"/>.
    /// </summary>
    public static class ApiExplorerEndpointConventionBuilderExtensions
    {
        private static readonly ApiExplorerSettingsAttribute _ignoreApiMetadata = new()
        {
            IgnoreApi = true
        };

        /// <summary>
        /// Ignores the endpoint(s) from the API Explorer.
        /// </summary>
        /// <param name="builder">The endpoint convention builder.</param>
        /// <returns>The original convention builder parameter.</returns>
        public static TBuilder IgnoreApi<TBuilder>(this TBuilder builder) where TBuilder : IEndpointConventionBuilder
        {
            builder.Add(endpointBuilder =>
            {
                endpointBuilder.Metadata.Add(_ignoreApiMetadata);
            });
            return builder;
        }
    }
}

Usage Examples

var builder = WebApplication.CreateBuilder(args);

// Configure application...

var app = builder.Build();

// Redirect to Swagger documentation
app.MapGet("/api", () => Results.Redirect("/swagger-ui/index.html"))
   .IgnoreApi();

// Configure other endpoints...

app.Run();

Alternative Designs

No alternative designs considered, this is just API sugar to remove the need to use verbose way of doing this using the existing APIs, similar to the existing AllowAnonymous(), RequireAuthorization(), RequireCors(), RequireHost() and WithDisplayName() extension methods:

builder.MapGet("/api", () => Results.Redirect("/swagger-ui/index.html"))
       .WithMetadata(new ApiExplorerSettingsAttribute { IgnoreApi = true });

Risks

None that I'm aware of, other than potential conflicts with user-defined application extension methods of the same name.

@martincostello martincostello added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jul 3, 2021
@Pilchie Pilchie added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-routing labels Jul 14, 2021
@davidfowl davidfowl added area-runtime feature-minimal-actions Controller-like actions for endpoint routing and removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-routing labels Jul 15, 2021
@rafikiassumani-msft rafikiassumani-msft added this to the Backlog milestone Jul 19, 2021
@ghost
Copy link

ghost commented Jul 19, 2021

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@DamianEdwards
Copy link
Member

Swashbuckle's default logic for whether to include an API in a given OpenAPI document is based on ApiDescription.GroupName today. To get the desired experience discussed here it should be updated to also look at whatever new metadata we add to indicate that an endpoint should be excluded when observing API explorer data.

@ghost
Copy link

ghost commented Jul 20, 2021

Thanks for contacting us.

We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@martincostello
Copy link
Member Author

Happy to help out with implementing this one once a decided-upon API spec is ready.

@DamianEdwards
Copy link
Member

@martincostello FYI I got a prototype of this [working in my playground repo].(DamianEdwards/MinimalApiPlayground@b5f19ea)

@martincostello
Copy link
Member Author

Thanks @DamianEdwards - I had a play around with some of the suggested API additions in the epic earlier today to try and match the API surface (but not the implementation) for hiding and also the response metadata in the repo where I've been trying stuff out: martincostello/dotnet-minimal-api-integration-testing@266fec2

@rafikiassumani-msft rafikiassumani-msft added the Priority:1 Work that is critical for the release, but we could probably ship without label Jul 22, 2021
@davidfowl davidfowl added old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels and removed area-runtime labels Jul 31, 2021
captainsafia added a commit that referenced this issue Aug 6, 2021
* Support setting content types in ProducesResponseTypeAttribute to close #34542

* Add WithName extension method to resolve #34538

* Support setting endpoints on group names to resolve #34541

* Add OpenAPI extension methods to resolve #33924

* Add tests for new OpenAPI methods

* Add endpoint metadata attributes

* Update PublicAPI files with deltas

* Add support for SuppressApi to close #34068

* Update tests to account for supporting setting content types

* Fix up PublicAPI analyzer warnings

* Clean up source files

* Address feedback from API review

* Fix typo and update type signature

* Apply feedback from second API review

* Update docstrings

* Apply suggestions from code review

Co-authored-by: Martin Costello <martin@martincostello.com>

* Address non-test related feedback

* Handle setting content types for ProducesResponseType attribute

* Address feedback from peer review

* Add test for ProducesResponseType override scenario

Co-authored-by: Martin Costello <martin@martincostello.com>
@ghost ghost locked as resolved and limited conversation to collaborators Sep 7, 2021
@amcasey amcasey added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Priority:1 Work that is critical for the release, but we could probably ship without
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants