fix(telemetry): resolve enum values through nested schema wrappers
What
generateToolMetrics reads an enum parameter's values by unwrapping at most
one optional wrapper:
if (schema._def.values?.length > 0) {
values = schema._def.values;
} else {
values = schema._def.innerType._def.values; // only one level
}
An enum parameter wrapped in .default().optional() (or any nested
optional/default/nullable combination) therefore falls through to
schema._def.innerType._def.values === undefined, and npm run update-metrics
(run as part of npm run gen) crashes:
TypeError: Cannot read properties of undefined (reading '0')
at validateEnumHomogeneity (build/src/telemetry/metricsRegistry.js:12)
at generateToolMetrics (build/src/telemetry/metricsRegistry.js:59)
Why it matters
npm run gen is required whenever a tool is added or changed (per
CONTRIBUTING.md). Declaring an enum parameter with a default — a natural,
documented zod pattern — silently breaks docs/metrics generation.
Fix
Add getEnumValues() in transformation.ts that recursively unwraps
optional / default / nullable / effects wrappers, mirroring the existing
getZodType(), and use it in generateToolMetrics. Behavior is unchanged for
the existing bare-enum and single-optional cases.
Tests
getEnumValuesunit tests: bare enum,optional,default, both orders ofdefault+optional, and throws for a non-enum type.generateToolMetricsregression test withzod.enum(...).default(...).optional().npm run typecheck, the telemetry tests, andnpm run check-formatpass.
First-time contributor here — happy to sign the CLA.
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).
View this failed invocation of the CLA check for more information.
For the most up to date status, view the checks section at the bottom of the pull request.
LGTM, but @yulunz can you please check it as well?
Thanks for taking a look, @nattallius! Happy to address anything @yulunz flags — the change is small and isolated (a recursive enum-value unwrap in the telemetry helper) with unit + regression tests.