Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions renderers/web_core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Add locale support to `SurfaceModel` and `DataContext` in v0.9.
- Update `pluralize`, `formatNumber`, and `formatCurrency` to use the context locale instead of hardcoding 'en-US'.
- Remove `.passthrough()` from `PluralizeApi` schema for stricter validation.
- Allow overriding hard-coded recursion depth in `DataValueSchema` for v0.8 by introducing `createDataValueSchema` factory function.

## 0.10.0

Expand Down
2 changes: 1 addition & 1 deletion renderers/web_core/src/v0_8/data/model-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ describe('A2uiMessageProcessor', () => {
},
},
]);
}, /Value must have exactly one value property/);
}, /must have exactly one value property/);
});

it('path resolves through primitive objects and arrays', () => {
Expand Down
133 changes: 133 additions & 0 deletions renderers/web_core/src/v0_8/schema/common-types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/**
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {describe, it} from 'node:test';
import * as assert from 'node:assert';
import {DataValueSchema, createDataValueSchema} from './common-types.js';

describe('DataValueSchema recursion depth', () => {
it('should allow depth <= 5 by default', () => {
const validData = {
key: 'root',
valueMap: [
{
key: 'level2',
valueMap: [
{
key: 'level3',
valueMap: [
{
key: 'level4',
valueMap: [
{
key: 'level5',
valueString: 'leaf',
},
],
},
],
},
],
},
],
};

const result = DataValueSchema.safeParse(validData);
assert.strictEqual(result.success, true);
});

it('should reject depth > 5 by default', () => {
const invalidData = {
key: 'root',
valueMap: [
{
key: 'level2',
valueMap: [
{
key: 'level3',
valueMap: [
{
key: 'level4',
valueMap: [
{
key: 'level5',
valueMap: [
{
key: 'level6',
valueString: 'leaf',
},
],
},
],
},
],
},
],
},
],
};

const result = DataValueSchema.safeParse(invalidData);
assert.strictEqual(result.success, false);
if (!result.success) {
assert.strictEqual(
result.error.issues[0].message,
'valueMap recursion exceeded maximum depth of 5.',
);
}
});

it('should allow overriding depth limit', () => {
const CustomSchema = createDataValueSchema({maxDepth: 2});

const validData = {
key: 'root',
valueMap: [
{
key: 'level2',
valueString: 'leaf',
},
],
};

const invalidData = {
key: 'root',
valueMap: [
{
key: 'level2',
valueMap: [
{
key: 'level3',
valueString: 'leaf',
},
],
},
],
};

const validResult = CustomSchema.safeParse(validData);
assert.strictEqual(validResult.success, true);

const invalidResult = CustomSchema.safeParse(invalidData);
assert.strictEqual(invalidResult.success, false);
if (!invalidResult.success) {
assert.strictEqual(
invalidResult.error.issues[0].message,
'valueMap recursion exceeded maximum depth of 2.',
);
}
});
});
33 changes: 8 additions & 25 deletions renderers/web_core/src/v0_8/schema/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,34 +65,14 @@ const DataValueMapItemSchema: z.ZodType<any> = z.lazy(() =>
}),
);

export const DataValueSchema = z
.object({
key: z.string(),
valueString: z.string().optional(),
valueNumber: z.number().optional(),
valueBoolean: z.boolean().optional(),
valueMap: z.array(DataValueMapItemSchema).optional(),
})
.strict()
.superRefine((val: any, ctx: z.RefinementCtx) => {
let count = 0;
if (val.valueString !== undefined) count++;
if (val.valueNumber !== undefined) count++;
if (val.valueBoolean !== undefined) count++;
if (val.valueMap !== undefined) count++;
if (count !== 1) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Value must have exactly one value property (valueString, valueNumber, valueBoolean, valueMap), found ${count}.`,
});
}
})
.superRefine((val: any, ctx: z.RefinementCtx) => {
export function createDataValueSchema(options: {maxDepth?: number} = {}) {
const maxDepth = options.maxDepth ?? 5;
return DataValueMapItemSchema.superRefine((val: any, ctx: z.RefinementCtx) => {
const checkDepth = (v: any, currentDepth: number) => {
if (currentDepth > 5) {
if (currentDepth > maxDepth) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'valueMap recursion exceeded maximum depth of 5.',
message: `valueMap recursion exceeded maximum depth of ${maxDepth}.`,
});
return;
}
Expand All @@ -104,6 +84,9 @@ export const DataValueSchema = z
};
checkDepth(val, 1);
});
}

export const DataValueSchema = createDataValueSchema();

export const NumberValueSchema = z
.object({
Expand Down
8 changes: 8 additions & 0 deletions renderers/web_core/src/v0_8/schema/verify-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ function getObjectDiff(obj1: any, obj2: any, path = ''): Record<string, any> {
continue;
}

// Zod generates a $ref for recursive structures (DataValueMapItemSchema),
// which differs from the inline definition in the JSON spec.
if (
currentPath.includes('dataModelUpdate.properties.contents.items.properties.valueMap.items')
) {
continue;
}

if (typeof val1 === 'object' && val1 !== null && typeof val2 === 'object' && val2 !== null) {
if (Array.isArray(val1) && Array.isArray(val2)) {
// Sort arrays to ignore order differences (like `required`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,9 @@ void main() {
print('Joke from AI:\n\n$result\n\n');
});

test(
'test can start restaurant_finder',
() async {
final restaurantFinderClient = TestRestaurantFinderClient();
addTearDown(restaurantFinderClient.dispose);
await restaurantFinderClient.startAndVerify();
},
timeout: const Timeout(Duration(minutes: 5)),
);
test('test can start restaurant_finder', () async {
final restaurantFinderClient = TestRestaurantFinderClient();
addTearDown(restaurantFinderClient.dispose);
await restaurantFinderClient.startAndVerify();
}, timeout: const Timeout(Duration(minutes: 5)));
}
Loading