domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.22k stars 1.31k forks source link

Azure Pipeline generate Swagger for ASP.NET Core Web API #2853

Closed Totot0 closed 2 months ago

Totot0 commented 5 months ago

I have an ASP.NET Core Web API that I build in Azure Devops. I have to extract the swagger during the build.

I therefore issue the following command:

- task: CmdLine@2 displayName: 'Generate Swagger' inputs: script: | dotnet new tool-manifest dotnet tool install --version 6.5.0 Swashbuckle.AspNetCore.Cli cd Api set ASPNETCORE_ENVIRONMENT=Development dotnet swagger tofile --output $(Build.ArtifactStagingDirectory)/swagger.json $(System.DefaultWorkingDirectory)/src/Api/bin/Release/net8.0/Api.dll v1 workingDirectory: '${{ parameters.workingDirectory }}'

The problem is that I have the impression that it launches the application because it looks for my appsettings.json and once filled, it tries to connect to the BDD.

How do I generate my Swagger in CI?

Load appsettings.json file in Main method :

IConfiguration Configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true) .AddEnvironmentVariables() .Build();

martincostello commented 5 months ago

I have the impression that it launches the application

That's exactly how it works.

If you have things that you need your app to not do if run in this manner to generate the file, you should add a mechanism to your application to avoid those things when run a certain way, such as setting an environment variable that specifies a value that indicates skipping those operations.

Totot0 commented 5 months ago

This is the only way to extract my swagger. There is no other solution?This is the only way to extract my swagger. There is no other solution?

martincostello commented 5 months ago

The app has to be run to execute the code to generate your OpenAPI document. If running the application does things you don't want your app to do when run in this context, you'll need to refactor to add the configurability you need to opt-out of whatever those things are.

github-actions[bot] commented 2 months ago

This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made.

github-actions[bot] commented 2 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.