domaindrivendev / Swashbuckle.AspNetCore

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

[Question]: MSBuild Target Build Swagger CLI with ConfigurationProvider #3058

Open fiewski opened 2 weeks ago

fiewski commented 2 weeks ago

What are you wanting to achieve?

Hello guys,

I'm trying to generate the documentation through the Swagger CLI, but my application has a call to the AWS Secret Manager. Therefore, when I'm building the application, the Swagger ToFile returns an error when querying the AWS service. Is there a way to generate the document without necessarily running the application?

What code or approach do you have so far?

Project csproj

<Target Name="ExportSwagger" AfterTargets="AfterBuild">
        <Exec Command="dotnet tool restore" />
        <Exec Command="dotnet swagger tofile --output .\swagger.json $(TargetPath) v1" EnvironmentVariables="DOTNET_ROLL_FORWARD=LatestMajor" />
</Target>

Output error:

  Done executing task "Exec".
  Task "Exec"
    Environment Variables passed to tool:
      DOTNET_ROLL_FORWARD=LatestMajor
    dotnet swagger tofile --output .\swagger.json ..\...\API.dll v1
    Unhandled exception. System.AggregateException: One or more errors occurred. (The security token included in the request is expired)
     ---> Amazon.SecretsManager.AmazonSecretsManagerException: The security token included in the request is expired
     ---> Amazon.Runtime.Internal.HttpErrorResponseException: Exception of type 'Amazon.Runtime.Internal.HttpErrorResponseException' was thrown.
       at Amazon.Runtime.HttpWebRequestMessage.ProcessHttpResponseMessage(HttpResponseMessage responseMessage)
       at Amazon.Runtime.HttpWebRequestMessage.GetResponseAsync(CancellationToken cancellationToken)
       at Amazon.Runtime.Internal.HttpHandler`1.InvokeAsync[T](IExecutionContext executionContext)
       at Amazon.Runtime.Internal.Unmarshaller.InvokeAsync[T](IExecutionContext executionContext)
       at Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)

My Program.cs, I'm placing the call to the aws secret manager as an IConfigurationSource

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Host.AddSecretsManager("CampaignManagerSecrets"); <= Here 

builder.Services
    .AddInternalService(builder.Configuration);

Additional context

No response

martincostello commented 2 weeks ago

Is there a way to generate the document without necessarily running the application?

There is not - this is fundamentally how OpenAPI generation works. The application is run so that all your filters, versioning, etc. is applied to the document. Only reflection isn't sufficient.

You'll need to make your application "generator aware" so that it doesn't use/need the bits that won't work outside of your real deployed application when the OpenAPI document is generated.

Possible solutions:

Then given which way you want to check for "is generator", conditionally not include SSM as a configuration source.