fix(csharp): require generichost token providers only on APIs whose operations use security
Fixes #25575
Problem
With the csharp generichost library, declaring an apiKey scheme in components.securitySchemes made every generated Api class take a TokenProvider<ApiKeyToken> constructor parameter, even when no operation of that class references the scheme. The provider was stored but never read, and resolving the Api from DI without registering a token failed:
InvalidOperationException: Unable to resolve service for type 'Org.OpenAPITools.TokenProvider`1[Org.OpenAPITools.Client.ApiKeyToken]'
while attempting to activate 'Org.OpenAPITools.Api.DefaultApi'.
Root cause
DefaultGenerator.addAuthenticationSwitches derives hasApiKeyMethods (and the sibling switches) from the document-wide components.securitySchemes map and stamps them onto every API bundle. The generichost api.mustache keyed the class-level ApiKeyProvider property, the constructor parameter, and the assignment off those document-wide flags, so a merely declared scheme was enough to wire the dependency into every Api class.
What this PR does
For generichost, CSharpClientCodegen.postProcessOperationsWithModels now computes class-level switches (classHasApiKeyMethods, classHasHttpBearerMethods, classHasHttpBasicMethods, classHasHttpSignatureMethods, classHasOAuthMethods) from the security schemes actually referenced by the class's own operations (CodegenOperation.authMethods already reflects operation-level security, falling back to document-level security). The class-level provider blocks in api.mustache consume these new switches.
Unchanged on purpose:
- Operation request bodies already keyed their
ApiKeyProvider.GetAsynccalls off the per-operationauthMethods, so those keep working as before. - The document-wide switches keep their meaning for support files (
ClientUtils,RateLimitProvider, the generated DI tests), which register one token per declared scheme. Registering a token that ends up unused is harmless; the previously failing case was the unused constructor dependency.
Backward compatibility
- Specs whose operations (or document-level
security) use the scheme get the same provider wiring as before, and the property/constructor/assignment output is identical. - Constructor signatures change only for Api classes whose operations never referenced a declared scheme. Those are exactly the classes that previously could not be resolved from DI without a token registration, so the new output unblocks them; code that explicitly registered the provider keeps working, since the registration is simply no longer consumed by that class.
- Other csharp libraries and other generators do not consume the new switches.
Verification
- New tests
CSharpGenericHostSecuritySchemeTest:- a spec that only declares an
apiKeyscheme generates an Api class without anyTokenProvider<ApiKeyToken>dependency; - a mixed spec generates the provider only for the Api class whose operation uses it (property, constructor parameter, and
ApiKeyProvider.GetAsynccall), while the unsecured class gets none.
- a spec that only declares an
- Both tests fail on master and pass with this change.
- All
csharp-generichost-*samples undersamples/client/petstore/csharp/generichost/are regenerated: classes with secured operations keep their providers, the others drop them (e.g. theTagssample, whose spec declares anapiKeyscheme that no operation uses).
Summary by cubic
Fixes #25575 — the csharp generichost library no longer adds a TokenProvider dependency to Api classes whose operations don't use a declared security scheme. Previously, declaring an apiKey scheme in components.securitySchemes made every generated Api class require a token provider, so resolving such a class from DI failed with InvalidOperationException: Unable to resolve service ... TokenProvider.
Backward compatibility
- Api classes whose operations (or document-level security) use a scheme keep the same provider wiring as before.
- Constructor signatures change only for classes that previously couldn't be resolved from DI without an unused token registration.
- The document-wide switches still apply to support files and the generated DI tests.