In this post, we will see how to resolve How can I generate at compile-time a separate OpenAPI Swagger.json file for each Controller in my ASP.NET project?
Question:
Our ASP.NET Core web application has an ever-growing list of Controllers. At build-time of the project, I want to programmatically generate an individual separate swagger.json file for each Controller. (We need these files on disk to be picked up by another process.)I’ve tried using NSwag and Swashbuckle. I configured them to generate a Document per Controller but their build-time CLI libraries seem to only support generating a single swagger.json file for a single specified document. I do not want to manually hard-code any specific document or Controller references.
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2578
https://github.com/RicoSuter/NSwag/issues/4292
Best Answer:
Here is a solution I came up with using NSWag. But it could ultimately use any library that can generate files.Basically what happens is the msbuild step “boots” your web application after it is built and the process checks for a special flag (generate-swagger) to indicate that you just want the swagger generated and the process to exit.
Helper extension method
If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com