fix(config): coerce env var strings to booleans and numbers in settings validation#25641
fix(config): coerce env var strings to booleans and numbers in settings validation#25641ixchio wants to merge 1 commit intogoogle-gemini:mainfrom
Conversation
…gs validation
When settings.json uses environment variable interpolation like
"${GEMINI_AUTO_THEME:-true}", the resolved value is the string "true"
rather than boolean true. Zod rejects this with a validation error,
effectively breaking env-based configuration for boolean and number
settings.
This adds z.preprocess coercion for boolean and number types in both
schema-building paths (buildPrimitiveSchema and buildZodSchemaFromJsonSchema).
Only "true"/"false" (case-insensitive, trimmed) are coerced to booleans —
arbitrary strings like "yes" still fail validation as expected.
Fixes google-gemini#25573
|
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. |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a configuration validation issue where environment variables injected into settings are treated as strings rather than their intended types. By integrating Zod's preprocessing pipeline, the system now correctly coerces these strings into booleans and numbers, preventing validation errors and improving the robustness of the configuration loading process. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request implements string-to-boolean and string-to-number coercion for configuration settings validation. By utilizing Zod's preprocess feature along with new helper functions coerceBooleanString and coerceNumberString, the system now correctly handles environment variables that resolve as strings (e.g., "true", "50"). The changes include comprehensive unit tests covering case-insensitivity, whitespace trimming, and numeric conversion. I have no feedback to provide.
Hey team! Ran into this while debugging a config issue.
What's going on
When you use env vars in
settings.jsonlike this:{ "ui": { "autoThemeSwitching": "${GEMINI_AUTO_THEME:-true}" } }The resolved value is the string
"true"— not booleantrue. Zod blows up with a validation error because it expects an actual boolean. Same thing happens with number fields getting string values from env vars.How I fixed it
Added
z.preprocesscoercion in both schema-building paths (buildPrimitiveSchemaandbuildZodSchemaFromJsonSchema). Pretty straightforward:"true"/"false"→true/false(case-insensitive, whitespace-trimmed)"50"→50"yes"or"maybe") still fails validation as it shouldThis avoids the schema-traversal complexity that tripped up earlier PRs — just hooks into Zod's own preprocessing pipeline.
Why not the existing PRs?
$refschema references and has type compatibility issuesTests
Added 6 tests covering all the edge cases — coercion, case insensitivity, whitespace, rejection of non-boolean strings, and numeric coercion. All existing tests still pass.
Fixes #25573
cc @scidomino @NTaylorMullen would appreciate a look when you get a chance 🙏