-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: escape dots in key names during flatten/unflatten #3192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,40 @@ export const CIRCULAR_REFERENCE_SENTINEL = "$@circular(("; | |
|
|
||
| const DEFAULT_MAX_DEPTH = 128; | ||
|
|
||
| /** Escape literal dots in a key segment so they are not confused with the path delimiter. */ | ||
| function escapeKey(key: string): string { | ||
| return key.replace(/\\/g, "\\\\").replace(/\./g, "\\."); | ||
| } | ||
|
|
||
| /** Unescape a key segment that was escaped by `escapeKey`. */ | ||
| function unescapeKey(key: string): string { | ||
| return key.replace(/\\(.)/g, "$1"); | ||
| } | ||
|
|
||
| /** | ||
| * Split a flattened attribute path on unescaped dots. | ||
| * Escaped dots (`\\.`) are preserved inside key segments and later unescaped. | ||
| */ | ||
| function splitKey(key: string): string[] { | ||
| const parts: string[] = []; | ||
| let current = ""; | ||
| for (let i = 0; i < key.length; i++) { | ||
| const ch = key[i]; | ||
| if (ch === "\\" && i + 1 < key.length) { | ||
| // Keep the escape sequence intact for now; unescapeKey will handle it | ||
| current += ch + key[i + 1]; | ||
| i++; | ||
| } else if (ch === ".") { | ||
| parts.push(current); | ||
| current = ""; | ||
| } else { | ||
| current += ch; | ||
| } | ||
| } | ||
| parts.push(current); | ||
| return parts; | ||
| } | ||
|
|
||
| export function flattenAttributes( | ||
| obj: unknown, | ||
| prefix?: string, | ||
|
|
@@ -116,7 +150,7 @@ class AttributeFlattener { | |
| for (const [key, value] of obj) { | ||
| if (!this.canAddMoreAttributes()) break; | ||
| // Use the key directly if it's a string, otherwise convert it | ||
| const keyStr = typeof key === "string" ? key : String(key); | ||
| const keyStr = typeof key === "string" ? escapeKey(key) : escapeKey(String(key)); | ||
| this.#processValue(value, `${prefix || "map"}.${keyStr}`, depth); | ||
| } | ||
| return; | ||
|
|
@@ -200,7 +234,8 @@ class AttributeFlattener { | |
| break; | ||
| } | ||
|
|
||
| const newPrefix = `${prefix ? `${prefix}.` : ""}${Array.isArray(obj) ? `[${key}]` : key}`; | ||
| const escapedKey = Array.isArray(obj) ? `[${key}]` : escapeKey(key); | ||
| const newPrefix = `${prefix ? `${prefix}.` : ""}${escapedKey}`; | ||
|
|
||
| if (Array.isArray(value)) { | ||
| for (let i = 0; i < value.length; i++) { | ||
|
|
@@ -278,19 +313,20 @@ export function unflattenAttributes( | |
| continue; | ||
| } | ||
|
|
||
| const parts = key.split(".").reduce( | ||
| const parts = splitKey(key).reduce( | ||
| (acc, part) => { | ||
| if (part.startsWith("[") && part.endsWith("]")) { | ||
| // Handle array indices more precisely | ||
| const match = part.match(/^\[(\d+)\]$/); | ||
| if (match && match[1]) { | ||
| acc.push(parseInt(match[1])); | ||
| const inner = part.slice(1, -1); | ||
| const match = inner.match(/^\d+$/); | ||
| if (match) { | ||
| acc.push(parseInt(inner)); | ||
| } else { | ||
| // Remove brackets for non-numeric array keys | ||
| acc.push(part.slice(1, -1)); | ||
| acc.push(unescapeKey(inner)); | ||
| } | ||
| } else { | ||
| acc.push(part); | ||
| acc.push(unescapeKey(part)); | ||
|
Comment on lines
+316
to
+329
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚩 Backward compatibility preserved for existing stored data The change from Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| } | ||
| return acc; | ||
| }, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚩 Missing changeset for public package change
This PR modifies
packages/corewhich is the published@trigger.dev/corepackage. Per CONTRIBUTING.md and CLAUDE.md, changes to packages inpackages/*require a changeset (pnpm run changeset:add). The diff only shows changes topackages/core/src/v3/utils/flattenAttributes.tsandpackages/core/test/flattenAttributes.test.tswith no changeset file visible. This is a process requirement rather than a code bug.Was this helpful? React with 👍 or 👎 to provide feedback.