Skip to content

fix(csharp): require generichost token providers only on APIs whose operations use security - #25686

Open
Inf1mum wants to merge 1 commit into
OpenAPITools:masterfrom
Inf1mum:fix/25575-generichost-unused-tokenprovider
Open

Inf1mum wants to merge 1 commit into
OpenAPITools:masterfrom
Inf1mum:fix/25575-generichost-unused-tokenprovider

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

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.GetAsync calls off the per-operation authMethods, 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 apiKey scheme generates an Api class without any TokenProvider<ApiKeyToken> dependency;
    • a mixed spec generates the provider only for the Api class whose operation uses it (property, constructor parameter, and ApiKeyProvider.GetAsync call), while the unsecured class gets none.
  • Both tests fail on master and pass with this change.
  • All csharp-generichost-* samples under samples/client/petstore/csharp/generichost/ are regenerated: classes with secured operations keep their providers, the others drop them (e.g. the Tags sample, whose spec declares an apiKey scheme 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.

Written for commit c6c89c3. Summary will update on new commits.

View guided diff

…perations use security

Declaring a scheme in components.securitySchemes stamped the document-wide
auth switches onto every API bundle, so the generichost api template wired a
TokenProvider constructor dependency into every Api class even when none of
its operations referenced the scheme. Resolving such an API from DI failed
unless a token was registered.

Compute class-level switches from the security schemes the class's own
operations reference and use them for the provider property, constructor
parameter, and assignment. Specs whose operations (or document-level
security) use a scheme keep the previous wiring; document-wide switches keep
their meaning for the generated support files.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 134 files

View guided diff | Re-trigger cubic

@wing328

wing328 commented Nov 17, 2026

Copy link
Copy Markdown
Member

thanks for the PR

cc @devhl-labs

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][CSHARP] generichost: every Api class requires TokenProvider<ApiKeyToken> when an apiKey scheme is only declared

2 participants