Skip to content

Commit 03bb76c

Browse files
Merge pull request #955 from contentstack/bugfix/CMG-792
Enhance LoadSelectCms and LoadUploadFile components to preserve exist…
2 parents cebcfe1 + f3c9fed commit 03bb76c

File tree

20 files changed

+7078
-12584
lines changed

20 files changed

+7078
-12584
lines changed

api/package-lock.json

Lines changed: 276 additions & 1647 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/package.json

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,14 @@
4141
"fs-readdir-recursive": "^1.1.0",
4242
"helmet": "^8.0.0",
4343
"html-to-json-parser": "^2.0.1",
44-
"http": "^0.0.1-security",
45-
"js-yaml": "^4.1.1",
4644
"jsdom": "^24.1.0",
47-
"jsonpath": "^1.2.0",
4845
"jsonwebtoken": "^9.0.3",
46+
"lodash": "^4.17.21",
4947
"lowdb": "^7.0.1",
5048
"mkdirp": "^3.0.1",
5149
"mysql2": "^3.16.2",
5250
"p-limit": "^6.2.0",
53-
"path-to-regexp": "^8.2.0",
5451
"php-serialize": "^5.1.3",
55-
"router": "^2.0.0",
56-
"shelljs": "^0.9.0",
5752
"socket.io": "^4.7.5",
5853
"uuid": "^9.0.1",
5954
"winston": "^3.11.0"
@@ -64,22 +59,21 @@
6459
"@types/fs-extra": "^11.0.4",
6560
"@types/fs-readdir-recursive": "^1.1.3",
6661
"@types/jsdom": "^21.1.7",
67-
"@types/jsonpath": "^0.2.4",
6862
"@types/jsonwebtoken": "^9.0.5",
6963
"@types/lodash": "^4.17.0",
70-
"@types/mkdirp": "^2.0.0",
7164
"@types/node": "^20.10.4",
72-
"@types/shelljs": "^0.8.15",
7365
"@types/uuid": "^9.0.8",
7466
"@typescript-eslint/eslint-plugin": "^6.15.0",
7567
"@typescript-eslint/parser": "^6.15.0",
7668
"eslint": "^8.56.0",
77-
"eslint-config-airbnb": "^19.0.0",
7869
"eslint-config-prettier": "^8.3.0",
79-
"lodash": "^4.17.21",
8070
"prettier": "^2.4.1",
8171
"tsx": "^4.7.1",
8272
"typescript": "^5.4.3"
8373
},
74+
"overrides": {
75+
"qs": ">=6.14.2",
76+
"tmp": ">=0.2.4"
77+
},
8478
"keywords": []
8579
}

api/src/services/contentful.service.ts

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import path from "path";
44
import { v4 as uuidv4 } from "uuid";
55
import _ from "lodash";
66
import axios from "axios";
7-
import jsonpath from "jsonpath";
87
import pLimit from 'p-limit';
98
import { JSDOM } from "jsdom";
109
import { jsonToHtml, jsonToMarkdown, htmlToJson } from '@contentstack/json-rte-serializer';
@@ -368,23 +367,45 @@ const cleanBrackets = (lang_value: any) => {
368367
};
369368

370369
// Helper function to process arrays and resolve IDs for entries and assets
370+
// Replaces jsonpath.query(array, "$..id") with direct item-level processing
371+
// (following the same pattern as Team Fury's processField for arrays)
371372
const processArrayFields = (array: any, entryId: any, assetId: any) => {
372-
const ids = jsonpath.query(array, "$..id");
373-
ids.forEach((id: any, i: number) => {
374-
if (id in entryId) {
375-
array.splice(i, 1, entryId[id]);
376-
} else if (id in assetId) {
377-
array.splice(i, 1, assetId?.[id]);
373+
// Handle primitive arrays directly (strings, numbers, booleans)
374+
if (array.every((item: any) => typeof item !== 'object' || item === null)) {
375+
return array;
376+
}
377+
378+
const processedArray = array.reduce((acc: any[], item: any) => {
379+
if (item?.sys?.id) {
380+
// Handle Contentful system links (Entry or Asset references)
381+
const { linkType, id } = item.sys;
382+
if (linkType === 'Entry' && id in entryId) {
383+
acc.push(entryId[id]);
384+
} else if (linkType === 'Asset' && id in assetId) {
385+
acc.push(assetId[id]);
386+
} else {
387+
// Keep unresolved references intact — the import process has a
388+
// separate reference update step that resolves these later.
389+
acc.push(item);
390+
}
391+
} else if (item !== null && typeof item === 'object') {
392+
// Keep non-reference objects as-is (nested objects, RTE fragments, etc.)
393+
acc.push(item);
394+
} else if (item !== null && item !== undefined) {
395+
// Keep primitive values (strings, numbers, booleans)
396+
acc.push(item);
378397
}
379-
});
380-
// Clean up empty objects
381-
const cleanedArray = JSON.stringify(array)
398+
return acc;
399+
}, []);
400+
401+
// Clean up empty objects (matches original cleanup behavior)
402+
const cleanedArray = JSON.stringify(processedArray)
382403
.replace(/{},/g, "")
383404
.replace(/,{}/g, "")
384405
.replace(/,{},/g, "")
385406
.replace(/{}/g, "");
386407
const result = typeof cleanedArray === 'string' && JSON.parse(cleanedArray);
387-
return result.length > 0 ? result : undefined;
408+
return Array.isArray(result) && result.length > 0 ? result : undefined;
388409
};
389410

390411
// Helper function to process Rich Text Editor (RTE) or nested object

0 commit comments

Comments
 (0)