From 532c4c2b133c7e8f2e1459c363d337ab3f445394 Mon Sep 17 00:00:00 2001 From: Animesh Kumar Date: Mon, 10 Jun 2024 11:39:05 +0530 Subject: [PATCH] fix: update script to handle component object ex Changes: - modified the script so that it can handle component object example - the script as of now add the fields and sub-fields in the updated-doc so that the other references remain unaffected - the earlier version was removinga and adding the whole part of the document altogether which affected the references and dependencies in the entire document --- .../embedded-jsonPath-replacement.js | 64 ++++++++++++++----- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/scripts/validation/embedded-files-test/embedded-jsonPath-replacement.js b/scripts/validation/embedded-files-test/embedded-jsonPath-replacement.js index b08529e5..251f9ba8 100644 --- a/scripts/validation/embedded-files-test/embedded-jsonPath-replacement.js +++ b/scripts/validation/embedded-files-test/embedded-jsonPath-replacement.js @@ -31,7 +31,11 @@ function extractExamples(content) { const examples = []; while ((match = exampleRegex.exec(content)) !== null) { - examples.push(JSON.parse(match[1])); + try { + examples.push(JSON.parse(match[1])); + } catch (e) { + console.error("Failed to parse example JSON:", match[1], e); + } } return examples; @@ -43,6 +47,18 @@ const examples = extractExamples(markdownContent); // Read the base AsyncAPI document for v3 const baseDoc = JSON.parse(fs.readFileSync('ex-base-doc.json', 'utf8')); +// Function to deeply merge two objects without overwriting existing nested structures +function deepMerge(target, source) { + for (const key of Object.keys(source)) { + if (source[key] instanceof Object && key in target) { + target[key] = deepMerge(target[key], source[key]); + } else { + target[key] = source[key]; + } + } + return target; +} + // Function to set a value in a JSON object using JSONPath, creating missing fields if necessary function setValueByPath(obj, path, value) { const pathParts = path.replace(/\$/g, '').split('.').slice(1); // Remove the root "$" and split path @@ -50,7 +66,11 @@ function setValueByPath(obj, path, value) { pathParts.forEach((part, index) => { if (index === pathParts.length - 1) { - current[part] = value; // Set value at the end of the path + if (current[part] === undefined) { + current[part] = value; // Set the new value if the path does not exist + } else { + current[part] = deepMerge(current[part], value); // Deep merge if the path exists + } } else { if (!current[part]) { current[part] = {}; // Create object if it doesn't exist @@ -69,19 +89,33 @@ const updates = comments.map((comment, index) => ({ // Apply updates updates.forEach(update => { - const results = JSONPath({ path: update.json_path, json: baseDoc, resultType: 'all' }); - - if (results.length === 0) { - setValueByPath(baseDoc, update.json_path, update.data); // Create the path if it doesn't exist - } else { - results.forEach(result => { - const parent = result.parent; - const parentProperty = result.parentProperty; - parent[parentProperty] = { - ...parent[parentProperty], - ...update.data // Merge the existing data with the new data - }; - }); + try { + const results = JSONPath({ path: update.json_path, json: baseDoc, resultType: 'all' }); + + console.log(`Processing update for test: ${update.test} at path: ${update.json_path}`); + + const pathParts = update.json_path.split('.'); + const targetKey = pathParts[pathParts.length - 1]; + + // Check if the top-level key of the example JSON matches the target key + let dataToMerge = update.data; + if (dataToMerge.hasOwnProperty(targetKey)) { + dataToMerge = dataToMerge[targetKey]; + } + + if (results.length === 0) { + console.log(`Path not found, creating path: ${update.json_path}`); + setValueByPath(baseDoc, update.json_path, dataToMerge); // Create the path if it doesn't exist + } else { + results.forEach(result => { + const parent = result.parent; + const parentProperty = result.parentProperty; + console.log(`Merging data at path: ${update.json_path}`); + parent[parentProperty] = deepMerge(parent[parentProperty], dataToMerge); // Deep merge the existing data with the new data + }); + } + } catch (e) { + console.error(`Error processing update for test: ${update.test} at path: ${update.json_path}`, e); } });