Repository navigation
postgres: send a string parameter bound to json/jsonb verbatim #40944
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
Open
robobun
wants to merge
9
commits into
main
Choose a base branch
from
robobun/01b36ead/postgres-json-string-param
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0319ea4
postgres: send a string parameter bound to json/jsonb verbatim
robobun a3c9289
postgres: widen the string escape hatch to json/jsonb instead of a ne…
robobun 3df90fd
postgres: trim the bind escape hatch comment
robobun 418a9b5
[autofix.ci] apply automated fixes
autofix-ci[bot] 60261ba
test: cover the Latin-1 and UTF-16 transcode arms separately
robobun 25ee32d
test: parameterize the transcode cases with test.each
robobun 3688b79
Merge remote-tracking branch 'origin/main' into robobun/01b36ead/post…
robobun 8dc6fbb
postgres: cover json/jsonb column inserts, document how json paramete…
robobun aaffd74
docs: note that JSON null needs JSON.stringify(null) for json/jsonb p…
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { SQL } from "bun"; | ||
| import { expect, test } from "bun:test"; | ||
| import { describeWithContainer } from "harness"; | ||
|
|
||
| // https://github.com/oven-sh/bun/issues/40942 | ||
| // A string parameter bound to a json/jsonb placeholder carries the JSON text | ||
| // itself and must be sent verbatim, not re-stringified into a JSON string | ||
| // scalar. This matches node-postgres and postgres.js. | ||
| describeWithContainer("postgres json string parameters", { image: "postgres_plain" }, container => { | ||
| const options = () => | ||
| ({ | ||
| db: "bun_sql_test", | ||
| username: "bun_sql_test", | ||
| host: container.host, | ||
| port: container.port, | ||
| max: 1, | ||
| }) as const; | ||
|
|
||
| test("string parameter bound to ::json is sent as the JSON text itself", async () => { | ||
| await container.ready; | ||
| await using sql = new SQL(options()); | ||
| const payload = JSON.stringify([{ id: 1, name: "a" }]); | ||
| const [row] = await sql`select json_typeof(${payload}::json) as kind, ${payload}::json as value`; | ||
| expect(row).toEqual({ kind: "array", value: [{ id: 1, name: "a" }] }); | ||
| }); | ||
|
|
||
| test("string parameter bound to ::jsonb is sent as the JSON text itself", async () => { | ||
| await container.ready; | ||
| await using sql = new SQL(options()); | ||
| const payload = JSON.stringify({ a: "hello", b: 42 }); | ||
| const [row] = await sql`select jsonb_typeof(${payload}::jsonb) as kind, ${payload}::jsonb as value`; | ||
| expect(row).toEqual({ kind: "object", value: { a: "hello", b: 42 } }); | ||
| }); | ||
|
|
||
| // A JS string is 8-bit or 16-bit as a whole, so two documents cover both | ||
| // transcode arms: "café" stays Latin-1, the emoji/CJK one forces UTF-16. | ||
| test.each([ | ||
| ["Latin-1", { name: "caf\u00e9" }], | ||
| ["UTF-16", { name: "\u{1F680}", text: "\u4e16\u754c" }], | ||
| ])("non-ASCII JSON text (%s) round-trips through the verbatim path", async (_label, doc) => { | ||
| await container.ready; | ||
| await using sql = new SQL(options()); | ||
| const payload = JSON.stringify(doc); | ||
| const [row] = await sql`select jsonb_typeof(${payload}::jsonb) as kind, ${payload}::jsonb as value`; | ||
| expect(row).toEqual({ kind: "object", value: doc }); | ||
| }); | ||
|
|
||
| test("json_to_recordset accepts a stringified array parameter", async () => { | ||
| await container.ready; | ||
| await using sql = new SQL(options()); | ||
| const payload = JSON.stringify([ | ||
| { id: 1, name: "a" }, | ||
| { id: 2, name: "b" }, | ||
| ]); | ||
| const rows = await sql.unsafe("select * from json_to_recordset($1::json) as x(id int, name text)", [payload]); | ||
| expect(rows).toEqual([ | ||
| { id: 1, name: "a" }, | ||
| { id: 2, name: "b" }, | ||
| ]); | ||
| }); | ||
|
|
||
| test("object parameter bound to ::json is still stringified", async () => { | ||
| await container.ready; | ||
| await using sql = new SQL(options()); | ||
| const [row] = await sql`select json_typeof(${{ a: 1 }}::json) as kind, ${{ a: 1 }}::json as value`; | ||
| expect(row).toEqual({ kind: "object", value: { a: 1 } }); | ||
| }); | ||
|
|
||
| test("string parameter bound to ::json with prepare: false", async () => { | ||
| await container.ready; | ||
| await using sql = new SQL({ ...options(), prepare: false }); | ||
| const payload = JSON.stringify([{ id: 1 }]); | ||
| const [row] = await sql`select json_typeof(${payload}::json) as kind`; | ||
| expect(row).toEqual({ kind: "array" }); | ||
| }); | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
|
|
||
| test("non-JSON string bound to ::json fails on the server", async () => { | ||
| await container.ready; | ||
| await using sql = new SQL(options()); | ||
| // .execute() because `expect(query).rejects` on the lazy Query hangs: | ||
| // https://github.com/oven-sh/bun/issues/40949 | ||
| await expect(sql`select ${"not json"}::json as x`.execute()).rejects.toThrow(/invalid input syntax for type json/); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.