Skip to content

Use IAsyncSwaggerProvider in CLI tofile command#3910

Merged
martincostello merged 1 commit intodomaindrivendev:masterfrom
bt-Knodel:cli-async-swagger-provider
Apr 21, 2026
Merged

Use IAsyncSwaggerProvider in CLI tofile command#3910
martincostello merged 1 commit intodomaindrivendev:masterfrom
bt-Knodel:cli-async-swagger-provider

Conversation

@bt-Knodel
Copy link
Copy Markdown
Contributor

@bt-Knodel bt-Knodel commented Apr 17, 2026

Summary

The CLI's tofile command resolves ISwaggerProvider and calls the synchronous GetSwagger(), which internally uses GenerateOperation(). This sync path only invokes IOperationFilter registrations — IOperationAsyncFilter registrations are silently skipped.

This means any project using OperationAsyncFilter<T>() gets correct output from the runtime swagger middleware but missing output from dotnet swagger tofile, leading to inconsistencies in generated clients (e.g., via Orval/NSwag).

Changes

  • Make CommandRunner and Program.Main fully async — no sync-over-async (GetAwaiter().GetResult())
  • The _tofile command prefers IAsyncSwaggerProvider.GetSwaggerAsync() when available, falling back to ISwaggerProvider.GetSwagger() for backward compatibility
  • Add Authorization test WebSite demonstrating real-world async filter: SecurityRequirementsAsyncOperationFilter resolves authorization policies via IAuthorizationPolicyProvider.GetPolicyAsync() and adds security requirements to operations
  • Integration test snapshots and CLI test for the new WebSite

Test plan

  • All existing CLI and integration tests pass
  • New Can_Generate_Swagger_Json_With_Async_Operation_Filters test verifies async filter output via CLI
  • New Authorization WebSite validates real-world use case: async operation filter resolves authorization policies via IAuthorizationPolicyProvider.GetPolicyAsync() and documents security requirements on protected endpoints
  • Backward compatible — falls back to sync provider if async provider is not registered

@bt-Knodel
Copy link
Copy Markdown
Contributor Author

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.

Comment thread src/Swashbuckle.AspNetCore.Cli/Program.cs
Comment thread src/Swashbuckle.AspNetCore.Cli/Program.cs Outdated
@bt-Knodel bt-Knodel force-pushed the cli-async-swagger-provider branch from 71ba7a1 to 2271ae2 Compare April 19, 2026 12:56
Copy link
Copy Markdown
Collaborator

@martincostello martincostello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few small style nits.

Comment thread test/WebSites/Authorization/Program.cs
Comment thread test/WebSites/Authorization/Startup.cs Outdated
@bt-Knodel bt-Knodel force-pushed the cli-async-swagger-provider branch from 2271ae2 to 514c304 Compare April 20, 2026 20:23
@martincostello
Copy link
Copy Markdown
Collaborator

Looks like some tests need fixing.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@bt-Knodel bt-Knodel force-pushed the cli-async-swagger-provider branch from 514c304 to 034e1e3 Compare April 21, 2026 15:31
@bt-Knodel
Copy link
Copy Markdown
Contributor Author

Added the missing Kiota files to fix the test.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.04%. Comparing base (d22870e) to head (034e1e3).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3910   +/-   ##
=======================================
  Coverage   95.04%   95.04%           
=======================================
  Files         111      111           
  Lines        3958     3958           
  Branches      801      801           
=======================================
  Hits         3762     3762           
  Misses        196      196           
Flag Coverage Δ
Linux 95.04% <ø> (ø)
Windows 95.04% <ø> (ø)
macOS 95.04% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@martincostello martincostello merged commit 4a2efa3 into domaindrivendev:master Apr 21, 2026
18 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants