Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Swagger json at Build time

I'm working on a ASP.NET Core WebAPI project and I'm trying to find a way to generate swagger json at build time ?

As i'm working with 4 environments, i'd like to have 4 swagger.json with a different name of my choice, like:

  • swagger_{appName}dev{buildNumber}.json
  • swagger_{appName}demo{buildNumber}.json
  • swagger_{appName}int{buildNumber}.json
  • swagger_{appName}staging{buildNumber}.json

Is it also possible to edit the fields in the json ? I'd like to edit (depending of the environment) the following fields : host, schemes and basePath.

I'm using Swashbuckle.AspNetCore but it appears it doesn't have an option to do such task or am I wrong ?

Thanks in advance

like image 284
moueidat Avatar asked Feb 06 '18 21:02

moueidat


People also ask

How does swagger json get generated?

If you are using the swagger nuget package in your project, this file is automatically created for you. You can simply access this file by navigation to your API and adding /swagger. json and the end of the url.

Where is the swagger json file?

Launch the app, and navigate to http://localhost:<port>/swagger/v1/swagger.json . The generated document describing the endpoints appears as shown in Swagger specification (swagger. json). The Swagger UI can be found at http://localhost:<port>/swagger .

Is swagger a json file?

The Swagger specification of the REST API consists of a file of JSON data called swagger. json. This schema file includes endpoint URLs, descriptions, request parameters and response structures for the entire API.


1 Answers

You have the option of generating OpenApi json file(s) from the command line without deploying using the Swashbuckle.AspNetCore.Cli nuget package.

The command will look something like this and can be added as a "post build" script

dotnet <nugetpackages>\Swashbuckle.AspNetCore.Cli\bin\$(Configuration)\netcoreapp<ver>\dotnet-swagger.dll tofile --host http://localhost --output swagger.json <bin>\<AssemblyName>.dll v1

more details can be found here https://github.com/domaindrivendev/Swashbuckle.AspNetCore#swashbuckleaspnetcorecli

I'm not sure about generating separate files for each environment offhand

like image 169
StepDo Avatar answered Oct 24 '22 22:10

StepDo