fix(pg-core): prevent double JSON.parse in jsonb mapFromDriverValue (#5485)#5487
Open
guoyangzhen wants to merge 1 commit intodrizzle-team:mainfrom
Open
fix(pg-core): prevent double JSON.parse in jsonb mapFromDriverValue (#5485)#5487guoyangzhen wants to merge 1 commit intodrizzle-team:mainfrom
guoyangzhen wants to merge 1 commit intodrizzle-team:mainfrom
Conversation
The pg driver automatically parses JSON/JSONB columns using JSON.parse.
When a JSONB column contains a string value like "0.1", the driver
returns it as the JavaScript string "0.1".
The previous code called JSON.parse on all string values, which incorrectly
converted JSON strings like "0.1" to numbers (0.1).
This fix only parses strings that look like raw JSON objects or arrays
(starting with { or [), which indicates the driver didn't auto-parse.
String values from already-parsed JSON are returned as-is.
Fixes drizzle-team#5485
Contributor
|
this is something that will be properly fixed for all drivers and dialects and column types with the new reworked |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The pg driver automatically parses JSON/JSONB columns using
JSON.parse. When a JSONB column contains a JSON string value like"0.1", the driver returns it as the JavaScript string"0.1".The current
mapFromDriverValueinPgJsonbcallsJSON.parseon all string values, which incorrectly converts JSON strings:This causes silent data corruption for any JSONB column storing numeric-looking strings.
Root Cause
Fix
Only parse strings that look like raw JSON objects/arrays (starting with
{or[), which indicates the driver returned unparsed JSON. String values from already-parsed JSON are returned as-is.Related
Fixes #5485