Handle braces within quotes

This commit is contained in:
Mel O'Hagan 2022-08-09 17:27:51 +01:00
parent e23d29b7ef
commit 40c846ce8f
2 changed files with 77 additions and 18 deletions

View File

@ -117,26 +117,31 @@ module MongoDBModule {
} }
parseQueryParams(params: string, mode: string) { parseQueryParams(params: string, mode: string) {
let queryParams = params.split(/(?<=}),[\n\s]*(?={)/g) let queryParams = []
if (queryParams.length > 3) { let openCount = 0
for (let i = 0; i < queryParams.length; i++) { let inQuotes = false
const openCount = queryParams[i].match(/{/g)?.length ?? 0 let i = 0
const closeCount = queryParams[i].match(/}/g)?.length ?? 0 let startIndex = 0
if ((openCount + closeCount) % 2 !== 0) { for (let c of params) {
if (openCount > closeCount) { if (c === '"') {
queryParams[i] += `, ${queryParams[i + 1]}` inQuotes = !inQuotes
queryParams.splice(i + 1, 1)
} else {
queryParams[i - 1] += `, ${queryParams[i]}`
queryParams.splice(i, 1)
i--
} }
if (c === '{' && !inQuotes) {
openCount++
if (openCount === 1) {
startIndex = i
} }
} else if (c === '}' && !inQuotes) {
if (openCount === 1) {
queryParams.push(JSON.parse(params.substring(startIndex, i+1)))
} }
openCount--
} }
let group1 = queryParams[0] ? JSON.parse(queryParams[0]) : {} i++
let group2 = queryParams[1] ? JSON.parse(queryParams[1]) : {} }
let group3 = queryParams[2] ? JSON.parse(queryParams[2]) : {} let group1 = queryParams[0] ?? {}
let group2 = queryParams[1] ?? {}
let group3 = queryParams[2] ?? {}
if (mode === "update") { if (mode === "update") {
return { return {
filter: group1, filter: group1,

View File

@ -266,4 +266,58 @@ describe("MongoDB Integration", () => {
upsert: true upsert: true
}) })
}) })
it("ignores braces within strings when parsing nested objects", async () => {
const query = {
json: `{
"_id": {
"$eq": "ObjectId('ACBD12345678ABCD12345678')"
}
},
{
"$set": {
"value": {
"data": [
{ "cid": 1 },
{ "cid": 2 },
{ "nested": {
"name": "te}st"
}}
]
}
}
},
{
"upsert": true,
"extra": "ad{d"
}`,
extra: { collection: "testCollection", actionTypes: "updateOne" },
}
await config.integration.update(query)
expect(config.integration.client.updateOne).toHaveBeenCalled()
const args = config.integration.client.updateOne.mock.calls[0]
expect(args[0]).toEqual({
_id: {
$eq: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"),
}
})
expect(args[1]).toEqual({
$set: {
value: {
data: [
{ cid: 1 },
{ cid: 2 },
{ nested: {
name: "te}st"
}}
]
},
},
})
expect(args[2]).toEqual({
upsert: true,
extra: "ad{d"
})
})
}) })