Skip to content

Conversation

@dev-hari-prasad
Copy link

@dev-hari-prasad dev-hari-prasad commented Dec 10, 2025

Description:
What changed?
Fixed the type inference for JSON path selections (->) by adding | null when the underlying JSON column can be null.

Why was this change needed?
When selecting a JSON path from a nullable JSON column, the generated TypeScript types incorrectly assumed the value could never be null.
In reality, the runtime returns null when the whole column is null, so the types were unsafe.
This patch brings the inferred type in line with actual behavior.

Closes/Fixes #1635

Screenshots/Examples:
N/A since it is a type-only change.

Breaking changes:
This PR contains no breaking changes.

📋 Checklist:

  • I have read the Contributing Guidelines
  • My PR title follows the conventional commit format: <type>(<scope>): <description>
  • I have run npx nx format to ensure consistent code formatting
  • I have added tests for new functionality (if applicable)
  • I have updated documentation (if applicable)

Additional notes:
The change is intentionally minimal and only affects the nullable branch of JsonPathToType.
It preserves existing behavior for non-nullable JSON columns while correctly reflecting actual return values for nullable ones.

CC @avallete & @mandarini

@dev-hari-prasad dev-hari-prasad requested review from a team as code owners December 10, 2025 17:27
@mandarini mandarini changed the title fix(postgrest-js): preserve nullability when using JSON path (->) (#1635) fix(postgrest): preserve nullability when using JSON path (->) (#1635) Dec 15, 2025
…upabase#1635)

The JSON path resolver (JsonPathToType) stripped `null` using Exclude<T, null>
but never added it back to the resulting type. This caused `.select('col->a')`
to infer a non-nullable type even when the underlying JSON column was nullable.

Example:
  json_col: { a: string } | null
  .select('json_col->a')   // inferred `string` instead of `string | null`

This patch re-attaches `| null` when the root JSON column contains null,
ensuring correct type inference for nullable JSON columns and nullable
nested paths.

Fixes: supabase#1635
@mandarini mandarini force-pushed the fix/Null-should-be-included-in-return-type branch from 60a5dcc to 621e051 Compare December 15, 2025 15:37
@mandarini mandarini added the postgrest-js Related to the postgrest-js library. label Dec 15, 2025
@mandarini
Copy link
Contributor

Thanks for the fix @dev-hari-prasad ! This correctly addresses the nullability issue in #1635. However, since this changes type inference in a way that could cause compilation errors for existing users (nullable JSON paths will now correctly return T | null instead of T), I will be treating this as a breaking change.

Before merging, could you please:

  1. Add type tests that verify the new nullable behavior
  2. Update any existing type tests that may be affected

I'll tag this for v3 (next major release). And we can merge it when the time comes!

@mandarini mandarini added v3 Planned for v3 do-not-merge Do not merge this PR. labels Dec 15, 2025
@mandarini mandarini self-assigned this Dec 15, 2025
@coveralls
Copy link

coveralls commented Dec 15, 2025

Coverage Status

coverage: 95.349% (+14.1%) from 81.261%
when pulling 9a0e074 on dev-hari-prasad:fix/Null-should-be-included-in-return-type
into 31280b2 on supabase:master.

@mandarini mandarini changed the title fix(postgrest): preserve nullability when using JSON path (->) (#1635) fix(postgrest): preserve nullability when using JSON path (#1635) Dec 15, 2025
@mandarini mandarini changed the title fix(postgrest): preserve nullability when using JSON path (#1635) fix(postgrest): preserve nullability when using JSON path Dec 15, 2025
@dev-hari-prasad
Copy link
Author

Before merging, could you please:

  1. Add type tests that verify the new nullable behavior
  2. Update any existing type tests that may be affected

Sure

Added tests to verify JsonPathToType correctly returns T | null when
accessing paths on nullable JSON columns. Also updated existing tests
to reflect the new nullable behavior in type expectations.
@dev-hari-prasad
Copy link
Author

@mandarini Tests are now added, let me know if anything else is required

@mandarini
Copy link
Contributor

Thanks @dev-hari-prasad ! We'll wait for v3 now!

Copy link
Member

@avallete avallete left a comment

Choose a reason for hiding this comment

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

A few comments, I think we should add more tests over the runtime behavior of this to ensure we don't regress on it further down the line.

@dev-hari-prasad
Copy link
Author

Happy to update or rewrite the affected tests as part of this pr. Just let me know if you’d like me to take that on, or if you prefer to handle it internally.

@mandarini @avallete

@avallete
Copy link
Member

avallete commented Dec 19, 2025

Happy to update or rewrite the affected tests as part of this pr. Just let me know if you’d like me to take that on, or if you prefer to handle it internally.

@mandarini @avallete

Hey @dev-hari-prasad first of all, thanks a lot for the contribution!

You’re very welcome to address the comments I left, that would be much appreciated and should help speed up getting this merged and released.

Otherwise, I can look into adding or updating the tests myself once I have the bandwidth.

@avallete avallete requested review from a team as code owners January 7, 2026 19:45
@avallete avallete changed the base branch from master to chore/add-supabase-cli-as-devdeps-package January 7, 2026 19:45
avallete
avallete approved these changes Jan 7, 2026
@avallete
Copy link
Member

avallete commented Jan 7, 2026

I don't expect this to really be a BC anymore. But as @mandarini mentioned, some users might rely over the "non null" behavior so that might break their typecheck (even if that's more correct).

@mandarini mandarini deleted the branch supabase:chore/add-supabase-cli-as-devdeps-package January 8, 2026 12:39
@mandarini mandarini closed this Jan 8, 2026
@mandarini
Copy link
Contributor

hmmm i don't know what happened, i did not close this. give me a moment

@mandarini
Copy link
Contributor

i merged the other PR so base branch got deleted. give me a moment to fix this

@mandarini
Copy link
Contributor

mandarini commented Jan 8, 2026

Yeah, something weird happen, and we cannot reopen the PR and target the master branch, this is what github copilot says:

What happened to this PR

This PR was automatically closed when its base branch (chore/add-supabase-cli-as-devdeps-package) was merged to master and subsequently deleted.

Timeline of events:

  1. PR created: fix/Null-should-be-included-in-return-typechore/add-supabase-cli-as-devdeps-package
  2. Base branch merged: chore/add-supabase-cli-as-devdeps-package was merged into master and deleted
  3. PR auto-closed: GitHub closed this PR instead of retargeting it to master

Why GitHub closed it instead of retargeting:

The head branch (fix/Null-should-be-included-in-return-type) was based on chore/add-supabase-cli-as-devdeps-package and contained all of its commits plus additional changes. When the base branch was merged to master, those commits became part of master, creating an ambiguous situation where many commits in this PR were now already in the target branch.

Rather than auto-retarget and show duplicate commits, GitHub closed the PR.

Next steps:

I created a new PR (#2011) that:

  • Targets master directly
  • Contains only the unique commits from this branch (rebased onto master)
  • Preserves the fix for preserving nullability when using JSON path

Thanks @dev-hari-prasad for the contribution! The work continues in the new PR.

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

Labels

do-not-merge Do not merge this PR. postgrest-js Related to the postgrest-js library. v3 Planned for v3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Null should be included in return type when using json path (->)

4 participants