Skip to content

Commit d1fcec5

Browse files
author
bgagent
committed
fix(cdk): accept -c JSON-string bedrockModels override (#628) resolveBedrockModelIds rejected any non-array context value, but CDK delivers a -c key=value context value as a raw string — so the -c bedrockModels='[...]' form the function's own JSDoc advertises threw at synth, while the cdk.context.json array form worked. JSON-parse a string context value before the array check so both forms behave identically. A non-JSON string (a true typo) is left as-is and still fails the array check with the same clear, key-named error, so loud-fail on malformed input is preserved. Same fix shape as resolveAgentCoreAzOverride (#358 review follow-up). Tests: added -c JSON-string array (success) and JSON-string-non-array (throws) cases.
1 parent f11d17a commit d1fcec5

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

cdk/src/constructs/bedrock-models.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,22 @@ export const BEDROCK_MODELS_CONTEXT_KEY = 'bedrockModels';
5858
* granting nothing or an invalid ARN.
5959
*/
6060
export function resolveBedrockModelIds(node: Node): readonly string[] {
61-
const override = node.tryGetContext(BEDROCK_MODELS_CONTEXT_KEY);
62-
if (override === undefined || override === null) {
61+
const raw = node.tryGetContext(BEDROCK_MODELS_CONTEXT_KEY);
62+
if (raw === undefined || raw === null) {
6363
return DEFAULT_BEDROCK_MODEL_IDS;
6464
}
65+
// `cdk.context.json` delivers a real array, but the `-c key=value` form
66+
// documented above delivers a raw string. Parse the string form so both
67+
// behave identically. A non-JSON string — a true typo — is left as-is and
68+
// fails the array check below with the same clear, key-named error.
69+
let override: unknown = raw;
70+
if (typeof raw === 'string') {
71+
try {
72+
override = JSON.parse(raw);
73+
} catch {
74+
override = raw;
75+
}
76+
}
6577
if (!Array.isArray(override) || override.length === 0) {
6678
throw new Error(
6779
`Context '${BEDROCK_MODELS_CONTEXT_KEY}' must be a non-empty array of foundation-model IDs `

cdk/test/constructs/bedrock-models.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ describe('resolveBedrockModelIds', () => {
4141
expect(ids).toEqual(override);
4242
});
4343

44+
it('parses a JSON-string override (the `-c key=value` CLI form)', () => {
45+
// CDK delivers `-c bedrockModels=[...]` as a raw string, not a parsed array;
46+
// the documented `-c` form must work identically to the file/array form.
47+
const ids = resolveBedrockModelIds(
48+
nodeWithContext({ [BEDROCK_MODELS_CONTEXT_KEY]: '["anthropic.claude-opus-4-8","anthropic.claude-sonnet-4-6"]' }),
49+
);
50+
expect(ids).toEqual(['anthropic.claude-opus-4-8', 'anthropic.claude-sonnet-4-6']);
51+
});
52+
53+
it('throws on a JSON string that does not parse to an array', () => {
54+
expect(() =>
55+
resolveBedrockModelIds(nodeWithContext({ [BEDROCK_MODELS_CONTEXT_KEY]: '"anthropic.claude-sonnet-4-6"' })),
56+
).toThrow(/must be a non-empty array/);
57+
});
58+
4459
it('throws on a non-array override (typo guard)', () => {
4560
expect(() =>
4661
resolveBedrockModelIds(nodeWithContext({ [BEDROCK_MODELS_CONTEXT_KEY]: 'anthropic.claude-opus-4-8' })),

0 commit comments

Comments
 (0)