question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

filter out the APIs being documented by NSwag in ASP.NET Core MVC

See original GitHub issue

I am trying to filter out the APIs being documented by NSwag inside an ASP.NET Core MVC application but it seems to be failing so far. I used the following custom IActionDescriptorCollectionProvider which seems to be picked up by ASP.NET Core MVC but not by NSwag.

    public class CustomActionDescriptorCollectionProvider : IActionDescriptorCollectionProvider
    {
        private readonly ActionDescriptorCollectionProvider _actionDescriptorCollectionProvider;

        public CustomActionDescriptorCollectionProvider(IEnumerable<IActionDescriptorProvider> actionDescriptorProviders,
            IEnumerable<IActionDescriptorChangeProvider> actionDescriptorChangeProviders)
        {
            if (actionDescriptorProviders == null) throw new ArgumentNullException(nameof(actionDescriptorProviders));
            if (actionDescriptorChangeProviders == null) throw new ArgumentNullException(nameof(actionDescriptorChangeProviders));

            _actionDescriptorCollectionProvider = new ActionDescriptorCollectionProvider(actionDescriptorProviders, actionDescriptorChangeProviders);
        }

        public ActionDescriptorCollection ActionDescriptors
        {
            get
            {
                var filteredCollection = _actionDescriptorCollectionProvider.ActionDescriptors.Items.Where(descriptor =>
                {
                    return false;
                }).ToList();

                return new ActionDescriptorCollection(filteredCollection, _actionDescriptorCollectionProvider.ActionDescriptors.Version);
            }
        }
    }

Then, I registered this like below:

            services.AddSingleton<IActionDescriptorCollectionProvider, CustomActionDescriptorCollectionProvider>();

This is just for a sample as you can see it hides all the actions but with this I expected NSwag to document nothing.

What’s the recommended way to filter out APIs to be documented in NSwag based on a condition? I can see the SwaggerIgnoreAttribute but I cannot apply that based on a condition.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

0reactions
RicoSutercommented, Jun 4, 2018

AddSwagger() is only needed for the new UseSwagger*WithApiExplorer() methods

Read more comments on GitHub >

github_iconTop Results From Across the Web

NSwag : How can i suppress controllers ASP.NET Core
It was possible in ASP.NET MVC by "filtering" the controllers. old code: var controllers = new[] { typeof(ExternalApiController) , typeof( ...
Read more >
Show only specific APIs on Swagger — Asp.Net Core
In this article, I explain how to use Document Filters on Swagger in order to control the endpoints that are being shown on...
Read more >
Hide actions from Swagger / OpenAPI documentation in ...
Two approaches for hiding actions from auto-generated API documentation. ... ASP.NET MVC Core has long supported "conventions".
Read more >
Enriched Web API Documentation using Swagger/OpenAPI in ...
In this article, we will learn about Web API documentation , how we can automatically generate it in ASP .NET Core and how...
Read more >
Get started with NSwag and ASP.NET Core
Learn how to use NSwag to generate documentation and help pages for an ASP.NET Core web API.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found