From 45612c85327543a29a61e975b9057d425525c2bc Mon Sep 17 00:00:00 2001 From: trackoor <55851864+trackoor@users.noreply.github.com> Date: Sat, 21 Nov 2026 02:02:51 +0000 Subject: [PATCH] fix(core): only drop whole keyword segments in toDataPathSegments The anyOf/allOf/oneOf and then/else regexes matched inside property names (something_else, strengthen) and also removed properties literally named then or else. Walk the pointer segment by segment instead and only skip keywords where a keyword is expected. Fixes #2664 --- packages/core/src/util/path.ts | 36 ++++++++++++++++++---------- packages/core/test/util/path.test.ts | 16 +++++++++++++ 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/packages/core/src/util/path.ts b/packages/core/src/util/path.ts index b16c027436..6c2a5b27ad 100644 --- a/packages/core/src/util/path.ts +++ b/packages/core/src/util/path.ts @@ -24,7 +24,6 @@ */ import isEmpty from 'lodash/isEmpty'; -import range from 'lodash/range'; export const compose = (path1: string, path2: string) => { let p1 = path1; @@ -55,18 +54,29 @@ export { compose as composePaths }; * @returns {string[]} an array containing only non-schema-specific segments */ export const toDataPathSegments = (schemaPath: string): string[] => { - const s = schemaPath - .replace(/(anyOf|allOf|oneOf)\/[\d]+\//g, '') - .replace(/(then|else)\//g, ''); - const segments = s.split('/'); - - const decodedSegments = segments.map(decode); - - const startFromRoot = decodedSegments[0] === '#' || decodedSegments[0] === ''; - const startIndex = startFromRoot ? 2 : 1; - return range(startIndex, decodedSegments.length, 2).map( - (idx) => decodedSegments[idx] - ); + const segments = schemaPath.split('/').map(decode); + const startFromRoot = segments[0] === '#' || segments[0] === ''; + const dataSegments: string[] = []; + // Walk the pointer segment by segment so that keywords are only skipped + // where a keyword is expected, never inside or in place of a property name + let i = startFromRoot ? 1 : 0; + while (i < segments.length) { + const segment = segments[i]; + if ( + (segment === 'anyOf' || segment === 'allOf' || segment === 'oneOf') && + /^\d+$/.test(segments[i + 1] ?? '') + ) { + i += 2; + } else if (segment === 'then' || segment === 'else') { + i += 1; + } else { + if (i + 1 < segments.length) { + dataSegments.push(segments[i + 1]); + } + i += 2; + } + } + return dataSegments; }; /** diff --git a/packages/core/test/util/path.test.ts b/packages/core/test/util/path.test.ts index b43c00e3f9..efa50e687f 100644 --- a/packages/core/test/util/path.test.ts +++ b/packages/core/test/util/path.test.ts @@ -108,6 +108,22 @@ test('toDataPath replace all combinators', (t) => { test('toDataPath use of keywords', (t) => { t.is(toDataPath('#/properties/properties'), 'properties'); }); +test('toDataPath keeps property names ending in then or else', (t) => { + t.is( + toDataPath('#/properties/something_else/properties/name'), + 'something_else.name' + ); + t.is(toDataPath('#/properties/strengthen/properties/x'), 'strengthen.x'); +}); +test('toDataPath keeps properties named like keywords', (t) => { + t.is(toDataPath('#/properties/then/properties/name'), 'then.name'); + t.is(toDataPath('#/properties/else/properties/name'), 'else.name'); + t.is(toDataPath('#/properties/anyOf/properties/0'), 'anyOf.0'); + t.is( + toDataPath('/anyOf/1/then/properties/then/properties/else'), + 'then.else' + ); +}); test('toDataPath use of encoded paths', (t) => { const fooBar = encodeURIComponent('foo.bar'); t.is(toDataPath(`#/properties/${fooBar}`), `${fooBar}`);