1

I have a Web.Api/OWIN project that I'm trying to generate a Swagger spec for.

I'm trying to avoid integrating too much of NSwag into my project; the project is an embedded API and I want to avoid having the spec file or Swagger UI run in-process. I'm still having to use NSwag attributes to work around things it cannot work out directly from the code or XML comments.

Instead, I'm trying to use the NSwag command line tool to generate a spec file as part of my build process that I can post-process as separate documentation (ultimately integrated into DocFX documentation).

So far I have managed to use nswag webapi2swagger command to generate the JSON I need, however, it lacks a securityDefinition section.

My endpoints have Authorization attributes which I have managed to include using the OperationSecurityScopeProcessor processor via the SwaggerOperationProcessor attribute.

These Authorization attribute details are successfully added to the spec, however, the absence of a securityDefinition property in the resulting spec is considered "invalid" and Swagger tools will not render the Authorization roles in the documentation.

I get the desired result using the SecurityDefinitionAppender document processor when running in-process via UseSwagger, however, there is no way to specify this for the command line tool, and trying to reference it in a nswag.json or .nswag config file (via the documentProcessorTypes property) fails because SecurityDefinitionAppender does not have a parameterless constructor (SwaggerGeneratorCommandBase.cs doesn't seem to provide a mechanism to support constructor parameters for documentProcessorTypes).

For the moment, if I want to automate this I have to run a script that adds the necessary static fragment to the JSON output before the next build step, but this just feels dirty, and I'm sure there are more complicated issues coming up that this solution would not work for.

Is there a way to reference operation and document processors in the NSwag command line tool when they require constructor parameters, or, is there another way to have the securityDefinition added to my spec file without having to run NSwag in-process?

EDIT

Per @RicoSuter suggestion in the comment below, rather than post-processing the JSON, you can start with a template that contains the bits you need and supply to Nswag via the documentTemplate option for webApiToSwagger.

For me, however, the solution ended up being to write my own IDocumentProcessor that doesn't require constructor parameters and does what I need it to do.

Until/unless the command line runner supports more sophisticated invocation of processors, this seems to be the only way.

Aleks
  • 1,629
  • 14
  • 19
  • 2
    There is a DocumentTemplate setting which can be used to define the initial spec file which is the filled with the paths, etc – Rico Suter Feb 14 '19 at 21:05
  • @RicoSuter Thanks; that's a good idea and would probably help in a lot of cases. It's definitely better than post-processing. – Aleks Feb 18 '19 at 07:42
  • Of course a document processor (code) is much more flexible mainly because you have full access to the document and it is processed after the document has been generated... – Rico Suter Feb 18 '19 at 09:42

0 Answers0