Skip to content

fix: input escape mapping#311

Merged
Skn0tt merged 3 commits into
mainfrom
input-escape-mapping
Aug 6, 2025
Merged

fix: input escape mapping#311
Skn0tt merged 3 commits into
mainfrom
input-escape-mapping

Conversation

@Skn0tt

@Skn0tt Skn0tt commented Jan 17, 2025

Copy link
Copy Markdown
Collaborator

Closes #310

There's a bug in the path escaping logic where meta was applied to the wrong values. This PR fixes the escape bug. It also introduces a version tag to toggle the path escaping when deserializing. This ensures backwards compatibility for SuperJSON results that were created on a version with the bug.

@Skn0tt Skn0tt requested a review from Copilot January 17, 2025 09:03
@Skn0tt Skn0tt self-assigned this Jan 17, 2025
@Skn0tt Skn0tt changed the title Input escape mapping fix: input escape mapping Jan 17, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 5 out of 6 changed files in this pull request and generated no comments.

Files not reviewed (1)
  • src/index.ts: Evaluated as low risk
Comments suppressed due to low confidence (1)

src/pathstringifier.test.ts:11

  • The expected output for the input 'test\a.b' should be ['test\a', 'b'] based on the escaping logic.
['test\\a.b', ['test\a', 'b']]

@Skn0tt Skn0tt mentioned this pull request Jan 17, 2025

@gibson042 gibson042 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't realize you'd be willing to embed a data version—but since you are, 👍.

I do still think it would be valuable to require that every property in meta always finds a json node, though, because there are quite possibly current serializations of relatively simple objects that are silently broken (e.g., { "a.0": /regex that becomes a string/ }, which you'll note does not have any backslashes at all).

Comment thread src/pathstringifier.ts Outdated
Comment on lines +20 to +27
if (!legacyPaths) {
const isEscapedBackslash = char === '\\' && string.charAt(i + 1) === '\\';
if (isEscapedBackslash) {
segment += '\\';
i++;
continue;
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This correctly handles escaped backslashes, but does not implement special behavior for backslashes that are in final position (e.g., "foo.bar.baz\\") or followed by something other than a dot or backslash (e.g., "foo.bar.b\\az" or "foo.bar.b\\\n"). I strongly recommend rejecting such input rather than treating it as synonymous with e.g. "foo.bar.baz\\\\"/"foo.bar.b\\\\az"/"foo.bar.b\\\\\n" (respectively).

Suggested change
if (!legacyPaths) {
const isEscapedBackslash = char === '\\' && string.charAt(i + 1) === '\\';
if (isEscapedBackslash) {
segment += '\\';
i++;
continue;
}
}
if (!legacyPaths && char === '\\') {
const escaped = string.charAt(i + 1);
if (escaped === '\\') {
segment += '\\';
i++;
continue;
} else if (escaped !== '.') {
throw Error('invalid path');
}
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, addressed in 38df195.

@hanayashiki

Copy link
Copy Markdown

Is there any schedule to publish this? Thank you very much

@Skn0tt Skn0tt requested a review from flybayer August 6, 2025 09:34
@Skn0tt Skn0tt merged commit 4054f3f into main Aug 6, 2025
0 of 3 checks passed
@Skn0tt Skn0tt deleted the input-escape-mapping branch August 6, 2025 14:47
@jdachtera

jdachtera commented Oct 10, 2025

Copy link
Copy Markdown

Hello @Skn0tt @flybayer, thanks for implementing this amazing fix. It works great for my use case (deserializing dot string property names combined with custom classes). I see the pr is already merged into main. Would it be possible to publish a new npm version of superjson soon with this fix included?

@Skn0tt

Skn0tt commented Oct 13, 2025

Copy link
Copy Markdown
Collaborator Author

cc @flybayer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Input mapping is buggy

6 participants