From 85fefcff15e8a63130cccf369ed3671efdb3f8e8 Mon Sep 17 00:00:00 2001 From: Dilip Kola <33080863+koladilip@users.noreply.github.com> Date: Fri, 12 Jul 2024 17:33:30 +0530 Subject: [PATCH] fix: array index mapping error handling (#115) * fix: array index mapping error handling * chore: upgrade github workflow --- .github/workflows/codecov.yml | 2 +- src/types.ts | 1 + src/utils/converter.ts | 10 +++++++++- test/scenarios/mappings/data.ts | 4 ++++ .../mappings/invalid_array_index_mappings.json | 10 ++++++++++ 5 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 test/scenarios/mappings/invalid_array_index_mappings.json 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 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" + } +]