You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[http-client-js] Make credential optional when NoAuth is part of the auth union - #12583
With @useAuth(BearerAuth | NoAuth), the JS client emitter generated a required credential parameter, so there was no way to call the service anonymously. The Python emitter already handles this (#9298).
Changes:
http-client-js (utils/parameters.tsx): the credential is optional when NoAuth is an alternative for every operation of the client. An optional credential stays positional, after the required parameters and before options. A parent client that requires auth still gets a required credential even if a nested namespace uses NoAuth (covered by the existing sub_client_override.md).
http-client (utils/client-helpers.ts): the synthetic credential parameter was cached by scheme type only, so BearerAuth and BasicAuth could share one cache entry and produce the wrong credential type depending on test order. The cache key now includes the http scheme. I updated bearer.md and client_structure.md, which had been expecting BasicCredential for BearerAuth.
New scenario test/scenarios/auth/no_auth_union.md.
Chronus change entries for both packages.
No runtime change is needed: with credential: undefined, getClient from @typespec/ts-http-runtime adds no auth policy and sends no Authorization header.
Tests pass in http-client-js (248 passed) and http-client (46 passed), and lint and prettier are clean.
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The credential remains incorrectly required when every operation permits anonymous access because the check uses the namespace authentication default instead of effective per-operation requirements.
Updates the JavaScript HTTP client emitter to support anonymous calls when authentication allows NoAuth, addressing #12354. It also fixes credential-type reuse in the shared HTTP client helpers.
Changes:
Makes eligible credentials optional while keeping them positional before options.
Distinguishes HTTP authentication schemes in the credential cache.
Adds an anonymous-auth scenario, corrects bearer expectations, and records both fixes in changelogs.
…fault
Each operation's resolved auth already falls back to its interface or
namespace auth, so the service default auth only matters when no operation
resolved any auth. Including it unconditionally kept the credential required
when the service used @useAuth(BearerAuth) and every operation overrode it
with @useAuth(BearerAuth | NoAuth).
Add scenarios for the all-operations override and for a mixed service where
only some operations allow NoAuth.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The credential name check incorrectly moves ordinary optional server URL parameters out of the options bag, breaking existing constructor calls and conflicting with the endpoint’s options-based lookup.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Restrict credential constructor handling to credential properties
The name check also matches an ordinary optional @server URL parameter such as { credential?: string = "anonymous" } on a service without authentication, changing its constructor from (options?) to (credential?: string, options?) and breaking previously valid calls such as new TestClient({}). Restrict this exception to properties identified by $.modelProperty.isCredential; ordinary URL parameters must remain in the options bag because client-context/parametrized-endpoint.tsx:25-27 still reads them from options.
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
emitter:client:jsIssue for the JS client emitter: @typespec/http-client-js
2 participants
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 #12354
With
@useAuth(BearerAuth | NoAuth), the JS client emitter generated a requiredcredentialparameter, so there was no way to call the service anonymously. The Python emitter already handles this (#9298).Changes:
http-client-js(utils/parameters.tsx): the credential is optional whenNoAuthis an alternative for every operation of the client. An optional credential stays positional, after the required parameters and beforeoptions. A parent client that requires auth still gets a required credential even if a nested namespace usesNoAuth(covered by the existingsub_client_override.md).http-client(utils/client-helpers.ts): the synthetic credential parameter was cached by scheme type only, soBearerAuthandBasicAuthcould share one cache entry and produce the wrong credential type depending on test order. The cache key now includes the http scheme. I updatedbearer.mdandclient_structure.md, which had been expectingBasicCredentialforBearerAuth.test/scenarios/auth/no_auth_union.md.No runtime change is needed: with
credential: undefined,getClientfrom@typespec/ts-http-runtimeadds no auth policy and sends noAuthorizationheader.Tests pass in
http-client-js(248 passed) andhttp-client(46 passed), and lint and prettier are clean.