Use IAsyncSwaggerProvider in CLI tofile command#3910
Use IAsyncSwaggerProvider in CLI tofile command#3910bt-Knodel wants to merge 1 commit intodomaindrivendev:masterfrom
Conversation
|
The issue #1661 appears to have missed the CLI implementation of Swagger. So async filters registered in the application are not called. Happy to adjust based on feedback, but would love to be able to use async policy filters with swagger cli. |
| var host = namedArgs.TryGetValue("--host", out var arg) ? arg : null; | ||
| var basePath = namedArgs.TryGetValue("--basepath", out var namedArg) ? namedArg : null; | ||
| var swagger = asyncSwaggerProvider != null | ||
| ? asyncSwaggerProvider.GetSwaggerAsync(namedArgs["swaggerdoc"], host, basePath).GetAwaiter().GetResult() |
There was a problem hiding this comment.
Is there a way to refactor this code to avoid needing to do GetAwaiter().GetResult()?
There was a problem hiding this comment.
Happy to do the refactor. As a heads up, this will update quite a few tests since it requires converting Main and CommandRunner to async.
71ba7a1 to
2271ae2
Compare
martincostello
left a comment
There was a problem hiding this comment.
Just a few small style nits.
There was a problem hiding this comment.
As it's new, could you have this use the Minimal APIs startup style please?
There was a problem hiding this comment.
Done! Followed the other websites using the partial class pattern to expose Program to tests.
There was a problem hiding this comment.
+1 - the Startup logic moved to Program.cs please.
| .ToList(); | ||
|
|
||
| if (authorizeAttributes.Count == 0) | ||
| return; |
There was a problem hiding this comment.
Please add braces to the return/continue usage.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2271ae2 to
514c304
Compare
Summary
The CLI's
tofilecommand resolvesISwaggerProviderand calls the synchronousGetSwagger(), which internally usesGenerateOperation(). This sync path only invokesIOperationFilterregistrations —IOperationAsyncFilterregistrations are silently skipped.This means any project using
OperationAsyncFilter<T>()gets correct output from the runtime swagger middleware but missing output fromdotnet swagger tofile, leading to inconsistencies in generated clients (e.g., via Orval/NSwag).Changes
CommandRunnerandProgram.Mainfully async — no sync-over-async (GetAwaiter().GetResult())_tofilecommand prefersIAsyncSwaggerProvider.GetSwaggerAsync()when available, falling back toISwaggerProvider.GetSwagger()for backward compatibilityAuthorizationtest WebSite demonstrating real-world async filter:SecurityRequirementsAsyncOperationFilterresolves authorization policies viaIAuthorizationPolicyProvider.GetPolicyAsync()and adds security requirements to operationsTest plan
Can_Generate_Swagger_Json_With_Async_Operation_Filterstest verifies async filter output via CLIAuthorizationWebSite validates real-world use case: async operation filter resolves authorization policies viaIAuthorizationPolicyProvider.GetPolicyAsync()and documents security requirements on protected endpoints