12

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

moueidat
  • 529
  • 1
  • 7
  • 21
  • 2
    Potential duplicate that specifically answers the OP. https://stackoverflow.com/questions/33283071/swagger-webapi-create-json-on-build – ttugates Mar 10 '21 at 20:53

2 Answers2

10

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

SivolcC
  • 3,258
  • 2
  • 14
  • 32
StepDo
  • 121
  • 1
  • 5
-1

The simple way is using PowerShell task to update json file and save with another name.

Simple workflow:

  1. Read the file through Get-Content script
  2. Using ConvertFrom-Json to converts content to object
  3. Set the property value to update object
  4. Using ConvertTo-Json to convert an object to a JSON-formatted string
  5. Using Set-Content to write content to a file

For detail code, you can refer to this thread: how do I update json file using powershell.

The build number value is stored in the predefined variable: Build.BuildNumber (PowerShell: $env:BUILD_BUILDNUMBER)

starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
  • 2
    I'll take a look at this ;) But the first step is to read the json file, how shall I read it if it's not present in the artifact ? (when building the project, there's a .dll, .deps.json, .pdb, .runtimeconfig.json and .runtimeconfig.dev.json but no sign of a swagger.json inside) – moueidat Feb 07 '18 at 07:34
  • How do you generate the swagger.json in local machine? – starian chen-MSFT Feb 07 '18 at 07:58
  • 1
    I go to to URL (when running) : localhost:5000/swagger/v1/swagger.json – moueidat Feb 07 '18 at 08:26
  • Run the core app through PowerShell task https://stackoverflow.com/questions/38878032/powershell-not-returning-to-command-line-after-running-dotnet-run, then call the API to generate swagger.json. (You can call start-sleep PS script wait some seconds) – starian chen-MSFT Feb 07 '18 at 09:06