Skip to content

fix(postgrest): serialize bigint values in rpc() request body - #2230

Closed
Vansh1811 wants to merge 1 commit into
supabase:masterfrom
Vansh1811:fix/postgrest-bigint-serialization
Closed

Vansh1811 wants to merge 1 commit into
supabase:masterfrom
Vansh1811:fix/postgrest-bigint-serialization

Conversation

@Vansh1811

Copy link
Copy Markdown
Contributor

Description

What changed?

Added a bigint serialization replacer to JSON.stringify in PostgrestBuilder.ts. When the request body is serialized, any bigint value is now converted to its string representation instead of throwing a TypeError.

One-line change:

- body: JSON.stringify(this.body),
+ body: JSON.stringify(this.body, (_, v) => typeof v === 'bigint' ? v.toString() : v),

Why was this change needed?

JavaScript's JSON.stringify does not natively support bigint values and throws:

TypeError: Do not know how to serialize a BigInt

This means any user calling rpc() with a bigint argument (e.g., for int8 / bigint Postgres columns) had to manually call .toString() on every bigint value:

// Before (broken):
await supabase.rpc('my_func', { amount: 9007199254740993n }) // throws TypeError

// Workaround users needed:
await supabase.rpc('my_func', { amount: 9007199254740993n.toString() })

// After (works transparently):
await supabase.rpc('my_func', { amount: 9007199254740993n }) // works!

This approach is consistent with how many serialization libraries handle bigint and avoids a breaking change — bigint → string conversion matches what PostgREST expects for int8 parameters anyway.

Closes #2117

@Vansh1811
Vansh1811 requested review from a team as code owners April 7, 2026 05:20
@pkg-pr-new

pkg-pr-new Bot commented Apr 7, 2026

Copy link
Copy Markdown

Open in StackBlitz

@supabase/auth-js

npm i https://pkg.pr.new/@supabase/auth-js@2230

@supabase/functions-js

npm i https://pkg.pr.new/@supabase/functions-js@2230

@supabase/postgrest-js

npm i https://pkg.pr.new/@supabase/postgrest-js@2230

@supabase/realtime-js

npm i https://pkg.pr.new/@supabase/realtime-js@2230

@supabase/storage-js

npm i https://pkg.pr.new/@supabase/storage-js@2230

@supabase/supabase-js

npm i https://pkg.pr.new/@supabase/supabase-js@2230

commit: 93f0bc2

@mandarini mandarini left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi @Vansh1811, thank you so much for contributing to Supabase! 💚

This is a real, well-reported issue (#2117) and the core approach is correct, converting bigint to string is the standard workaround for JSON.stringify's limitation, and Postgres will coerce a JSON string to int8 just fine inside json_to_record.

Before we can merge this, we need at least one test. There's an rpc.test.ts in packages/core/postgrest-js/test/ that would be the right place for this. At minimum, a unit test that mocks fetch and asserts the serialized body contains the string representation of the bigint. Ideally, an integration test against an actual Postgres function with an int8 parameter -- you can add the function to test/supabase/migrations/00000000000000_schema.sql.

There's also one edge case worth thinking through: if a user passes a bigint value inside a jsonb parameter (e.g., { payload: { id: 9999n } } for an RPC that takes a jsonb), the bigint becomes a JSON string rather than a JSON number. That may or may not be the right behavior, but it should be either tested or called out in a comment so the tradeoff is explicit.

Could you add the tests and take a look at the jsonb case? Happy to help if you have questions about the test setup.

Thank you so much for contributing to Supabase, it's exactly this kind of fix that makes the SDK more reliable for real-world use cases.

@7ttp

7ttp commented Apr 14, 2026

Copy link
Copy Markdown
Member

Hi @Vansh1811, thank you so much for contributing to Supabase! 💚

This is a real, well-reported issue (#2117) and the core approach is correct, converting bigint to string is the standard workaround for JSON.stringify's limitation, and Postgres will coerce a JSON string to int8 just fine inside json_to_record.

Before we can merge this, we need at least one test. There's an rpc.test.ts in packages/core/postgrest-js/test/ that would be the right place for this. At minimum, a unit test that mocks fetch and asserts the serialized body contains the string representation of the bigint. Ideally, an integration test against an actual Postgres function with an int8 parameter -- you can add the function to test/supabase/migrations/00000000000000_schema.sql.

There's also one edge case worth thinking through: if a user passes a bigint value inside a jsonb parameter (e.g., { payload: { id: 9999n } } for an RPC that takes a jsonb), the bigint becomes a JSON string rather than a JSON number. That may or may not be the right behavior, but it should be either tested or called out in a comment so the tradeoff is explicit.

Could you add the tests and take a look at the jsonb case? Happy to help if you have questions about the test setup.

Thank you so much for contributing to Supabase, it's exactly this kind of fix that makes the SDK more reliable for real-world use cases.

ah, noticed this has been also waiting for a bit now ~(7-10d)
would it be fine if I open a new PR to address these suggestions
while also giving credit to the OP there? :) 💚 @mandarini

@mandarini

Copy link
Copy Markdown
Contributor

sure @7ttp go ahead!

@7ttp

7ttp commented Apr 14, 2026

Copy link
Copy Markdown
Member

sure @7ttp go ahead!

here you go:

@mandarini

Copy link
Copy Markdown
Contributor

Closing in favor of #2245

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.

support bigint parameters

3 participants