Is it possible to have NSwag ignore a controller?
Asked Answered
B

3

22

I used NSwag to generate a client for a single controller; I needed it as its own separate client. I would like for it to be ignored when the Swagger specification is generated in the future.

I tried adding this attribute at the top of the controller but it is still being noted in the specification: [ApiExplorerSettings(IgnoreApi = true)]

My controller is implementing the standard Microsoft.AspNetCore.Mvc.Controller class.

Baum answered 18/4, 2017 at 19:43 Comment(1)
Attributes are not the only way to do it, here's an alternative: https://mcmap.net/q/587153/-nswag-how-can-i-suppress-controllers-asp-net-coreMordy
D
44

I think in the latest version,

[ApiExplorerSettings(IgnoreApi = true)]

is supported.

Otherwise you can add the SwaggerIgnoreAttribute OR OpenApiIgnoreAttribute attribute

[SwaggerIgnore]
[OpenApiIgnore]

Or manually select the controllers in NSwagStudio or in the middleware...

Duncandunce answered 18/4, 2017 at 21:0 Comment(5)
I have the latest version but that annotation still didn't work for me. Using [NSwag.Annotations.SwaggerIgnore] ended up being the best solution.Baum
Strange: github.com/NSwag/NSwag/blob/master/src/…Duncandunce
That test only demonstrates that attribute works when placed over a method. I was interested in knocking out the entire controller with one attribute. [SwaggerIgnore] does just that, thankfully.Baum
I see, next version will also support this scenario: github.com/NSwag/NSwag/commit/…Duncandunce
[Microsoft.AspNetCore.Mvc.ApiExplorerSettings(IgnoreApi = true)] is needed if you also have using System.Web.Http.Description; in your ControllerRedintegrate
B
17

Use [OpenApiIgnore]

(since [SwaggerIgnore] has been deprecated)

Barn answered 19/6, 2019 at 11:37 Comment(0)
Z
0

@Arieh 's answer worked for me. I'd like to share more.

  1. Install package NSwag.Annotations for DTO class, for my case which is in separated project, then use [OpenApiIgnore] decorate the property which should be ignored.
  2. Install package NSwag.AspNetCore for the web project.
Zip answered 3/9, 2020 at 4:55 Comment(1)
is this work for any one ?Sweatt

© 2022 - 2024 — McMap. All rights reserved.