-
Notifications
You must be signed in to change notification settings - Fork 112
fix: handle clientContextParam collisions with builtin config keys #1788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3a3edc9
11e7af2
689f6ef
0e7c610
2576bf1
d6133b9
7102cb4
ee57d39
d99e6a4
4858763
fee039f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@smithy/middleware-endpoint": minor | ||
| --- | ||
|
|
||
| handle clientContextParam collisions with builtin config keys | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { describe, expect, test as it } from "vitest"; | ||
| import { XYZService } from "xyz"; | ||
|
|
||
| describe("client context parameters precedence integration test", () => { | ||
| it("should handle conflicting vs non-conflicting parameter precedence correctly", async () => { | ||
| // For non-conflicting params | ||
siddsriv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const clientWithNonConflicting = new XYZService({ | ||
| endpoint: "https://localhost", | ||
| apiKey: async () => ({ apiKey: "test-key" }), | ||
| customParam: "user-custom-value", | ||
| clientContextParams: { | ||
| apiKey: "test-key", | ||
| customParam: "nested-custom-value", | ||
| }, | ||
| }); | ||
|
|
||
| // Verify that endpoint resolution uses the nested value over root value | ||
| const resolvedConfig = clientWithNonConflicting.config; | ||
| const effectiveCustomParam = resolvedConfig.clientContextParams?.customParam ?? resolvedConfig.customParam; | ||
| expect(effectiveCustomParam).toBe("nested-custom-value"); | ||
|
|
||
| // For conflicting parameters | ||
| const clientWithConflicting = new XYZService({ | ||
| endpoint: "https://localhost", | ||
| apiKey: async () => ({ apiKey: "auth-key" }), | ||
| clientContextParams: { | ||
| apiKey: "endpoint-key", | ||
| }, | ||
| }); | ||
|
|
||
| // Verify that both auth and endpoint contexts can coexist | ||
| const resolvedConfigConflicting = clientWithConflicting.config; | ||
|
|
||
| // Verify endpoint context has the nested value | ||
| expect(resolvedConfigConflicting.clientContextParams?.apiKey).toBe("endpoint-key"); | ||
|
|
||
| // Verify auth context has the auth provider | ||
| const authIdentity = await resolvedConfigConflicting.apiKey?.(); | ||
| expect(authIdentity?.apiKey).toBe("auth-key"); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,16 +9,29 @@ import type { Endpoint, EndpointV2 } from "@smithy/types"; | |
| * it will most likely not contain the config | ||
| * value, but we use it as a fallback. | ||
| * @param config - container of the config values. | ||
| * @param isClientContextParam - whether this is a client context parameter. | ||
| * | ||
| * @returns async function that will resolve with the value. | ||
| */ | ||
| export const createConfigValueProvider = <Config extends Record<string, unknown>>( | ||
| configKey: string, | ||
| canonicalEndpointParamKey: string, | ||
| config: Config | ||
| config: Config, | ||
| isClientContextParam = false | ||
| ) => { | ||
| const configProvider = async () => { | ||
| const configValue: unknown = config[configKey] ?? config[canonicalEndpointParamKey]; | ||
| let configValue: unknown; | ||
|
|
||
| if (isClientContextParam) { | ||
| // For client context parameters, check clientContextParams first | ||
| const clientContextParams = config.clientContextParams as Record<string, unknown> | undefined; | ||
| const nestedValue: unknown = clientContextParams?.[canonicalEndpointParamKey]; | ||
| configValue = nestedValue ?? config[configKey] ?? config[canonicalEndpointParamKey]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for example, would this case correctly prefer the nested value? const client = new XYZService({
apiKey: async () => ({ apiKey: "auth-secret" }),
clientContextParams: {
apiKey: "endpoint-header-key"
}
});if the nested value looks for the canonical name, would it find |
||
| } else { | ||
| // For built-in parameters and other config properties | ||
| configValue = config[configKey] ?? config[canonicalEndpointParamKey]; | ||
| } | ||
|
|
||
| if (typeof configValue === "function") { | ||
| return configValue(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| // smithy-typescript generated code | ||
| import type { | ||
| HandlerExecutionContext, | ||
| HttpAuthOption, | ||
| HttpAuthScheme, | ||
| HttpAuthSchemeParameters, | ||
| HttpAuthSchemeParametersProvider, | ||
| HttpAuthSchemeProvider, | ||
| Provider, | ||
| import { doesIdentityRequireRefresh, isIdentityExpired, memoizeIdentityProvider } from "@smithy/core"; | ||
| import { | ||
| type ApiKeyIdentity, | ||
| type ApiKeyIdentityProvider, | ||
| type HandlerExecutionContext, | ||
| type HttpAuthOption, | ||
| type HttpAuthScheme, | ||
| type HttpAuthSchemeParameters, | ||
| type HttpAuthSchemeParametersProvider, | ||
| type HttpAuthSchemeProvider, | ||
| type Provider, | ||
| HttpApiKeyAuthLocation, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these new imports look like types. Change the codegen part to use |
||
| } from "@smithy/types"; | ||
| import { getSmithyContext, normalizeProvider } from "@smithy/util-middleware"; | ||
|
|
||
|
|
@@ -41,9 +45,14 @@ export const defaultXYZServiceHttpAuthSchemeParametersProvider = async ( | |
| }; | ||
| }; | ||
|
|
||
| function createSmithyApiNoAuthHttpAuthOption(authParameters: XYZServiceHttpAuthSchemeParameters): HttpAuthOption { | ||
| function createSmithyApiHttpApiKeyAuthHttpAuthOption(authParameters: XYZServiceHttpAuthSchemeParameters): HttpAuthOption { | ||
| return { | ||
| schemeId: "smithy.api#noAuth", | ||
| schemeId: "smithy.api#httpApiKeyAuth", | ||
| signingProperties: { | ||
| name: "X-Api-Key", | ||
| in: HttpApiKeyAuthLocation.HEADER, | ||
| scheme: undefined, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -59,7 +68,7 @@ export const defaultXYZServiceHttpAuthSchemeProvider: XYZServiceHttpAuthSchemePr | |
| const options: HttpAuthOption[] = []; | ||
| switch (authParameters.operation) { | ||
| default: { | ||
| options.push(createSmithyApiNoAuthHttpAuthOption(authParameters)); | ||
| options.push(createSmithyApiHttpApiKeyAuthHttpAuthOption(authParameters)); | ||
| } | ||
| } | ||
| return options; | ||
|
|
@@ -88,6 +97,10 @@ export interface HttpAuthSchemeInputConfig { | |
| * @internal | ||
| */ | ||
| httpAuthSchemeProvider?: XYZServiceHttpAuthSchemeProvider; | ||
| /** | ||
| * The API key to use when making requests. | ||
| */ | ||
| apiKey?: ApiKeyIdentity | ApiKeyIdentityProvider; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -113,6 +126,10 @@ export interface HttpAuthSchemeResolvedConfig { | |
| * @internal | ||
| */ | ||
| readonly httpAuthSchemeProvider: XYZServiceHttpAuthSchemeProvider; | ||
| /** | ||
| * The API key to use when making requests. | ||
| */ | ||
| readonly apiKey?: ApiKeyIdentityProvider; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -121,7 +138,9 @@ export interface HttpAuthSchemeResolvedConfig { | |
| export const resolveHttpAuthSchemeConfig = <T>( | ||
| config: T & HttpAuthSchemeInputConfig | ||
| ): T & HttpAuthSchemeResolvedConfig => { | ||
| const apiKey = memoizeIdentityProvider(config.apiKey, isIdentityExpired, doesIdentityRequireRefresh); | ||
| return Object.assign(config, { | ||
| authSchemePreference: normalizeProvider(config.authSchemePreference ?? []), | ||
| apiKey, | ||
| }) as T & HttpAuthSchemeResolvedConfig; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.