Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fiewski opened this issue Aug 29, 2024 · 1 comment
Open
Labels

Comments

@fiewski
Copy link

fiewski commented Aug 29, 2024

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
Copy link
Collaborator

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:

  • Run with a specific environment name instead of Development/Production
  • Run with a specific environment variable defined that indicates the same (e.g. OPENAPI_GENERATOR=true)
  • Check the name of the entrypoint assembly to see if it's the generator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants