Skip to content

Commit f81b777

Browse files
authored
fix(csm): resolve root path mappings (#1172)
1 parent 88c6d63 commit f81b777

File tree

2 files changed

+247
-1
lines changed

2 files changed

+247
-1
lines changed

src/csm/resolveMapping.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function resolveMapping(
2828
}
2929

3030
const resultMappingPathArray = jsonPathArray(jsonPathToMappingPath(resultPath))
31-
for (let i = resultMappingPathArray.length - 1; i > 0; i--) {
31+
for (let i = resultMappingPathArray.length - 1; i >= 0; i--) {
3232
const key = `$${resultMappingPathArray.slice(0, i).join('')}`
3333
const mappingFound = csm.mappings[key]
3434
if (mappingFound) {

test/stega/encodeIntoResult.test.ts

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,252 @@ const encodeTestCases: {
246246
],
247247
},
248248
},
249+
{
250+
name: 'resolves root only mapping',
251+
queryResult: {
252+
result: [
253+
{
254+
_id: 'foo',
255+
this: 'that',
256+
},
257+
],
258+
resultSourceMap: {
259+
documents: [
260+
{
261+
_id: 'foo',
262+
_type: 'bar',
263+
},
264+
],
265+
paths: ['$'],
266+
mappings: {
267+
$: {
268+
source: {
269+
document: 0,
270+
path: 0,
271+
type: 'documentValue',
272+
},
273+
type: 'value',
274+
},
275+
},
276+
},
277+
},
278+
expected: {
279+
encoderCalls: 2,
280+
encoderArgs: [
281+
[
282+
{
283+
sourcePath: [0, '_id'],
284+
sourceDocument: {_id: 'foo', _type: 'bar'},
285+
resultPath: [0, '_id'],
286+
value: 'foo',
287+
},
288+
],
289+
[
290+
{
291+
sourcePath: [0, 'this'],
292+
sourceDocument: {_id: 'foo', _type: 'bar'},
293+
resultPath: [0, 'this'],
294+
value: 'that',
295+
},
296+
],
297+
],
298+
},
299+
},
300+
{
301+
name: 'single result resolves root only mapping',
302+
queryResult: {
303+
result: {
304+
_id: 'foo',
305+
this: 'that',
306+
},
307+
resultSourceMap: {
308+
documents: [
309+
{
310+
_id: 'foo',
311+
_type: 'bar',
312+
},
313+
],
314+
paths: ['$'],
315+
mappings: {
316+
$: {
317+
source: {
318+
document: 0,
319+
path: 0,
320+
type: 'documentValue',
321+
},
322+
type: 'value',
323+
},
324+
},
325+
},
326+
},
327+
expected: {
328+
encoderCalls: 2,
329+
encoderArgs: [
330+
[
331+
{
332+
sourcePath: ['_id'],
333+
sourceDocument: {_id: 'foo', _type: 'bar'},
334+
resultPath: ['_id'],
335+
value: 'foo',
336+
},
337+
],
338+
[
339+
{
340+
sourcePath: ['this'],
341+
sourceDocument: {_id: 'foo', _type: 'bar'},
342+
resultPath: ['this'],
343+
value: 'that',
344+
},
345+
],
346+
],
347+
},
348+
},
349+
{
350+
name: 'resolves mixture of root and non-root mappings',
351+
queryResult: {
352+
result: [
353+
{
354+
_id: 'foo',
355+
this: 'that',
356+
projected: {
357+
fred: 'yolo',
358+
},
359+
},
360+
],
361+
resultSourceMap: {
362+
documents: [
363+
{
364+
_id: 'foo',
365+
_type: 'bar',
366+
},
367+
],
368+
paths: ["$['_id']", "$['this']", "$['nested']"],
369+
mappings: {
370+
"$[0]['_id']": {
371+
source: {
372+
document: 0,
373+
path: 0,
374+
type: 'documentValue',
375+
},
376+
type: 'value',
377+
},
378+
"$[0]['this']": {
379+
source: {
380+
document: 0,
381+
path: 1,
382+
type: 'documentValue',
383+
},
384+
type: 'value',
385+
},
386+
"$[0]['projected']": {
387+
source: {
388+
document: 0,
389+
path: 2,
390+
type: 'documentValue',
391+
},
392+
type: 'value',
393+
},
394+
},
395+
},
396+
},
397+
expected: {
398+
encoderCalls: 3,
399+
encoderArgs: [
400+
[
401+
{
402+
sourcePath: ['_id'],
403+
sourceDocument: {_id: 'foo', _type: 'bar'},
404+
resultPath: [0, '_id'],
405+
value: 'foo',
406+
},
407+
],
408+
[
409+
{
410+
sourcePath: ['this'],
411+
sourceDocument: {_id: 'foo', _type: 'bar'},
412+
resultPath: [0, 'this'],
413+
value: 'that',
414+
},
415+
],
416+
[
417+
{
418+
sourcePath: ['nested', 'fred'],
419+
sourceDocument: {_id: 'foo', _type: 'bar'},
420+
resultPath: [0, 'projected', 'fred'],
421+
value: 'yolo',
422+
},
423+
],
424+
],
425+
},
426+
},
427+
{
428+
name: 'single result resolves mixture of root and non-root mappings',
429+
queryResult: {
430+
result: {
431+
_id: 'foo',
432+
this: 'that',
433+
projected: {
434+
fred: 'yolo',
435+
},
436+
},
437+
resultSourceMap: {
438+
documents: [
439+
{
440+
_id: 'foo',
441+
_type: 'bar',
442+
},
443+
],
444+
paths: ['$', "$['nested']"],
445+
mappings: {
446+
$: {
447+
source: {
448+
document: 0,
449+
path: 0,
450+
type: 'documentValue',
451+
},
452+
type: 'value',
453+
},
454+
"$['projected']": {
455+
source: {
456+
document: 0,
457+
path: 1,
458+
type: 'documentValue',
459+
},
460+
type: 'value',
461+
},
462+
},
463+
},
464+
},
465+
expected: {
466+
encoderCalls: 3,
467+
encoderArgs: [
468+
[
469+
{
470+
sourcePath: ['_id'],
471+
sourceDocument: {_id: 'foo', _type: 'bar'},
472+
resultPath: ['_id'],
473+
value: 'foo',
474+
},
475+
],
476+
[
477+
{
478+
sourcePath: ['this'],
479+
sourceDocument: {_id: 'foo', _type: 'bar'},
480+
resultPath: ['this'],
481+
value: 'that',
482+
},
483+
],
484+
[
485+
{
486+
sourcePath: ['nested', 'fred'],
487+
sourceDocument: {_id: 'foo', _type: 'bar'},
488+
resultPath: ['projected', 'fred'],
489+
value: 'yolo',
490+
},
491+
],
492+
],
493+
},
494+
},
249495
]
250496

251497
test.each(encodeTestCases)('encode $name', ({queryResult, expected}) => {

0 commit comments

Comments
 (0)