Nested braces parse fix added for MongoDB

This commit is contained in:
Mel O'Hagan 2022-08-08 21:56:55 +01:00
parent fe0163ac29
commit 09a3052aa2
2 changed files with 68 additions and 0 deletions

View File

@ -118,6 +118,22 @@ module MongoDBModule {
parseQueryParams(params: string, mode: string) {
let queryParams = params.split(/(?<=}),[\n\s]*(?={)/g)
if (queryParams.length > 3) {
for (let i = 0; i < queryParams.length; i++) {
const openCount = queryParams[i].match(/{/g)?.length ?? 0
const closeCount = queryParams[i].match(/}/g)?.length ?? 0
if ((openCount + closeCount) % 2 !== 0) {
if (openCount > closeCount) {
queryParams[i] += `, ${queryParams[i+1]}`
queryParams.splice(i+1, 1)
} else {
queryParams[i-1] += `, ${queryParams[i]}`
queryParams.splice(i, 1)
i--
}
}
}
}
let group1 = queryParams[0] ? JSON.parse(queryParams[0]) : {}
let group2 = queryParams[1] ? JSON.parse(queryParams[1]) : {}
let group3 = queryParams[2] ? JSON.parse(queryParams[2]) : {}

View File

@ -214,4 +214,56 @@ describe("MongoDB Integration", () => {
upsert: false
})
})
it("can parse nested objects with arrays", async () => {
const query = {
json: `{
"_id": {
"$eq": "ObjectId('ACBD12345678ABCD12345678')"
}
},
{
"$set": {
"value": {
"data": [
{ "cid": 1 },
{ "cid": 2 },
{ "nested": {
"name": "test"
}}
]
}
}
},
{
"upsert": true
}`,
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: "test"
}}
]
},
},
})
expect(args[2]).toEqual({
upsert: true
})
})
})