Skip to content
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

fix(util): safely stringify path segments named as GROQ data types (e.g. null) #5986

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions packages/@sanity/util/src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const EMPTY_PATH: Path = []

export const FOCUS_TERMINATOR = '$'

// Fields named as GROQ data types cannot be accessed using dot notation. These fields must instead
// be serialized using square bracket notation.
const GROQ_DATA_TYPE_VALUES = ['true', 'false', 'null']

export function get<R>(obj: unknown, path: Path | string): R | undefined
export function get<R>(obj: unknown, path: Path | string, defaultValue: R): R
export function get(obj: unknown, path: Path | string, defaultVal?: unknown): unknown {
Expand Down Expand Up @@ -164,14 +168,22 @@ export function toString(path: Path): string {
}

return path.reduce<string>((target, segment, i) => {
const segmentType = typeof segment
if (segmentType === 'number') {
const isHead = i === 0

if (typeof segment === 'number') {
return `${target}[${segment}]`
}

if (segmentType === 'string') {
const separator = i === 0 ? '' : '.'
return `${target}${separator}${segment}`
if (typeof segment === 'string') {
if (isHead) {
return segment
}

if (GROQ_DATA_TYPE_VALUES.includes(segment)) {
return `${target}["${segment}"]`
}

return `${target}.${segment}`
}

if (isKeySegment(segment) && segment._key) {
Expand Down
9 changes: 9 additions & 0 deletions packages/@sanity/util/test/PathUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect, test} from '@jest/globals'

/* eslint-disable max-nested-callbacks, @typescript-eslint/ban-ts-comment */
import {fromString, get, resolveKeyedPath, toString} from '../src/paths'

Expand Down Expand Up @@ -104,6 +105,14 @@ test('toString: handles deep prop segments', () => {
expect(toString(['bar', 'foo', 'baz'])).toEqual('bar.foo.baz')
})

test('toString: handles deep prop segments named as GROQ data types', () => {
expect(toString(['foo', 'true'])).toEqual('foo["true"]')
expect(toString(['bar', 'false'])).toEqual('bar["false"]')
expect(toString(['bat', 'null'])).toEqual('bat["null"]')
expect(toString(['true', 'true'])).toEqual('true["true"]')
expect(toString(['true', 'false', 'null'])).toEqual('true["false"]["null"]')
})

test('toString: handles deep array index segments', () => {
expect(toString(['foo', 13])).toEqual('foo[13]')
expect(toString(['bar', 'foo', 3])).toEqual('bar.foo[3]')
Expand Down