Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions packages/core/src/util/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/

import isEmpty from 'lodash/isEmpty';
import range from 'lodash/range';

export const compose = (path1: string, path2: string) => {
let p1 = path1;
Expand Down Expand Up @@ -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);

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.

One thing came up reviewing this PR regarding decode (path.ts:107). It uses .replace(/~0/, '~') without the g flag, so only the first ~0 in a segment is decoded. Adding the flag would fix it, ideally together with a test like a~0b~0c giving a~b~c.

If you want to fix that as part of this PR that would be appreciated but not expected as this is a pre-existing bug. Do however you see fit 😊

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') &&

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.

This changes the output for a scope that ends in a combinator index. #/properties/a/anyOf/0 used to give a.0 and now gives a, which is the right data path since anyOf/N doesn't add a data level. The old result only came from the regex requiring a trailing slash. The PR description says the output for all existing paths is unchanged, so please mention this and add a test for it.

/^\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;
};

/**
Expand Down
16 changes: 16 additions & 0 deletions packages/core/test/util/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
Loading