From c6f0f3d8f5aeddae5746adfe02cc5d78ac9610dc Mon Sep 17 00:00:00 2001 From: Dilip Kola Date: Fri, 12 Jul 2024 17:04:54 +0530 Subject: [PATCH 1/2] fix: array index mapping error handling --- src/types.ts | 1 + src/utils/converter.ts | 10 +++++++++- test/scenarios/mappings/data.ts | 4 ++++ .../mappings/invalid_array_index_mappings.json | 10 ++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/scenarios/mappings/invalid_array_index_mappings.json diff --git a/src/types.ts b/src/types.ts index 0f2bd4a..fd08823 100644 --- a/src/types.ts +++ b/src/types.ts @@ -282,6 +282,7 @@ export type FlatMappingPaths = { to?: string; input?: string; output?: string; + [key: string]: any; }; export type FlatMappingAST = FlatMappingPaths & { diff --git a/src/utils/converter.ts b/src/utils/converter.ts index c95c74e..0f59170 100644 --- a/src/utils/converter.ts +++ b/src/utils/converter.ts @@ -58,7 +58,15 @@ function processArrayIndexFilter( ? flatMapping.inputExpr : createObjectExpression(); } - return currrentOutputPropAST.value.elements[filterIndex]; + const objectExpr = currrentOutputPropAST.value.elements[filterIndex]; + if (!isLastPart && objectExpr?.type !== SyntaxType.OBJECT_EXPR) { + throw new JsonTemplateMappingError( + 'Invalid mapping: invalid array index mapping', + flatMapping.input as string, + flatMapping.output as string, + ); + } + return objectExpr; } function isPathWithEmptyPartsAndObjectRoot(expr: Expression) { diff --git a/test/scenarios/mappings/data.ts b/test/scenarios/mappings/data.ts index aed7e80..0a4db53 100644 --- a/test/scenarios/mappings/data.ts +++ b/test/scenarios/mappings/data.ts @@ -187,6 +187,10 @@ export const data: Scenario[] = [ ], }, }, + { + mappingsPath: 'invalid_array_index_mappings.json', + error: 'Invalid mapping', + }, { description: 'Index mappings in last part', mappings: [ diff --git a/test/scenarios/mappings/invalid_array_index_mappings.json b/test/scenarios/mappings/invalid_array_index_mappings.json new file mode 100644 index 0000000..f6cbcb0 --- /dev/null +++ b/test/scenarios/mappings/invalid_array_index_mappings.json @@ -0,0 +1,10 @@ +[ + { + "from": "$.products[*].a", + "to": "$.items[*].a" + }, + { + "from": "$.products[*].b", + "to": "$.items[0].b" + } +] From a7952d57cb1a17f60b6a0f67c226c9c9019063b9 Mon Sep 17 00:00:00 2001 From: Dilip Kola Date: Fri, 12 Jul 2024 17:11:58 +0530 Subject: [PATCH 2/2] chore: upgrade github workflow --- .github/workflows/codecov.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 488cbdb..4c79e14 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -8,5 +8,5 @@ jobs: coverage: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v4 - uses: ArtiomTr/jest-coverage-report-action@v2