fix(schema-compat): escape discriminator key in generated discriminatedUnion#18623
fix(schema-compat): escape discriminator key in generated discriminatedUnion#18623greymoth-jp wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 8d6f680 The changes in this PR will be included in the next version bump. This PR includes changesets to release 29 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
@greymoth-jp is attempting to deploy a commit to the Mastra Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
WalkthroughIn ChangesDiscriminator key escaping in z.discriminatedUnion codegen
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
PR triageThis PR needs to fix an existing issue. Please link an issue in the PR description, for example with Applied label: PRs without a linked issue will automatically close after 1 day(s) with the label. Changed test gateChanged Test Gate is pending. The |
Problem
@mastra/schema-compat'sjsonSchemaToZodbuilds a Zod schema as a source string that is later evaluated at runtime withFunction(). When it detects a discriminated union (ananyOfof objects that share aconstproperty), it inserts the discriminator property name into the generatedz.discriminatedUnion(...)call directly:Every other string this generator emits is serialized with
JSON.stringify(object keys,.describe(...),.default(...)), but the discriminator key is interpolated raw. A JSON Schema property name may legally contain a double quote, so a name such asty"peproduces:The object key is escaped correctly (
"ty\"pe"), but thediscriminatedUnionargument is not, so the generated string throwsSyntaxError: missing ) after argument listwhen it is evaluated. This is the same kind of runtimeFunction()failure that was fixed forparseOneOfin #11610.Solution
Serialize the discriminator key with
JSON.stringify, the same helper already used for the object keys it refers to:For any ordinary property name this is byte-identical to the previous output, since
JSON.stringify("type") === '"type"', so existing schemas generate exactly the same code.Verification
I added a test in the
DiscriminatedUnion Detectionblock that uses a discriminator name containing a double quote, evaluates the generated string withFunction()the way the output is consumed, and asserts it no longer throws and parses a matching value.ELI5
This change fixes a bug where a special character in a schema property name could break generated code. Now the property name is safely escaped before being put into the code, so schemas with quotes in their discriminator key work correctly.
Changes
jsonSchemaToZodto serialize discriminated-union keys withJSON.stringify, matching other generated strings.@mastra/schema-compat.Why
SyntaxErrorwhen JSON Schema discriminator keys contain double quotes.