Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: array index mapping error handling #115

Merged
merged 2 commits into from
Jul 12, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ export type FlatMappingPaths = {
to?: string;
input?: string;
output?: string;
[key: string]: any;
};

export type FlatMappingAST = FlatMappingPaths & {
Expand Down
10 changes: 9 additions & 1 deletion src/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@
? flatMapping.inputExpr
: createObjectExpression();
}
return currrentOutputPropAST.value.elements[filterIndex];
const objectExpr = currrentOutputPropAST.value.elements[filterIndex];
if (!isLastPart && objectExpr?.type !== SyntaxType.OBJECT_EXPR) {
throw new JsonTemplateMappingError(

Check warning on line 63 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
'Invalid mapping: invalid array index mapping',

Check warning on line 64 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
flatMapping.input as string,

Check warning on line 65 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
flatMapping.output as string,

Check warning on line 66 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
);

Check warning on line 67 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 68 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 68 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 68 in src/utils/converter.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/converter.ts#L63-L68

Added lines #L63 - L68 were not covered by tests
koladilip marked this conversation as resolved.
Show resolved Hide resolved
return objectExpr;
}

function isPathWithEmptyPartsAndObjectRoot(expr: Expression) {
Expand Down
4 changes: 4 additions & 0 deletions test/scenarios/mappings/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ export const data: Scenario[] = [
],
},
},
{
mappingsPath: 'invalid_array_index_mappings.json',
error: 'Invalid mapping',
},
{
description: 'Index mappings in last part',
mappings: [
Expand Down
10 changes: 10 additions & 0 deletions test/scenarios/mappings/invalid_array_index_mappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"from": "$.products[*].a",
"to": "$.items[*].a"
},
{
"from": "$.products[*].b",
"to": "$.items[0].b"
}
]
Loading