Repository navigation
Conversation
…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.
Member
|
thanks for the PR cc @devhl-labs |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #25575
Problem
With the
csharpgenerichost library, declaring anapiKeyscheme incomponents.securitySchemesmade every generated Api class take aTokenProvider<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:Root cause
DefaultGenerator.addAuthenticationSwitchesderiveshasApiKeyMethods(and the sibling switches) from the document-widecomponents.securitySchemesmap and stamps them onto every API bundle. The generichostapi.mustachekeyed the class-levelApiKeyProviderproperty, 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.postProcessOperationsWithModelsnow computes class-level switches (classHasApiKeyMethods,classHasHttpBearerMethods,classHasHttpBasicMethods,classHasHttpSignatureMethods,classHasOAuthMethods) from the security schemes actually referenced by the class's own operations (CodegenOperation.authMethodsalready reflects operation-level security, falling back to document-levelsecurity). The class-level provider blocks inapi.mustacheconsume these new switches.Unchanged on purpose:
ApiKeyProvider.GetAsynccalls off the per-operationauthMethods, so those keep working as before.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
security) use the scheme get the same provider wiring as before, and the property/constructor/assignment output is identical.Verification
CSharpGenericHostSecuritySchemeTest:apiKeyscheme generates an Api class without anyTokenProvider<ApiKeyToken>dependency;ApiKeyProvider.GetAsynccall), while the unsecured class gets none.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
csharpgenerichost library no longer adds aTokenProviderdependency to Api classes whose operations don't use a declared security scheme. Previously, declaring anapiKeyscheme incomponents.securitySchemesmade every generated Api class require a token provider, so resolving such a class from DI failed withInvalidOperationException: Unable to resolve service ... TokenProvider.Backward compatibility
Written for commit c6c89c3. Summary will update on new commits.