Fix for $in on ObjectIds (#11014)

This commit is contained in:
melohagan 2023-06-26 15:07:29 +01:00 committed by GitHub
parent 4788972362
commit 0ecda93ea9
1 changed files with 21 additions and 6 deletions

View File

@ -380,18 +380,26 @@ class MongoIntegration implements IntegrationBase {
return this.client.connect() return this.client.connect()
} }
matchId(value?: string) {
return value?.match(/(?<=objectid\(['"]).*(?=['"]\))/gi)?.[0]
}
hasObjectId(value?: any): boolean {
return (
typeof value === "string" && value.toLowerCase().startsWith("objectid")
)
}
createObjectIds(json: any): object { createObjectIds(json: any): object {
const self = this const self = this
function interpolateObjectIds(json: any) { function interpolateObjectIds(json: any) {
for (let field of Object.keys(json || {})) { for (let field of Object.keys(json || {})) {
if (json[field] instanceof Object) { if (json[field] instanceof Object) {
json[field] = self.createObjectIds(json[field]) json[field] = self.createObjectIds(json[field])
} }
if ( if (self.hasObjectId(json[field])) {
typeof json[field] === "string" && const id = self.matchId(json[field])
json[field].toLowerCase().startsWith("objectid")
) {
const id = json[field].match(/(?<=objectid\(['"]).*(?=['"]\))/gi)?.[0]
if (id) { if (id) {
json[field] = ObjectId.createFromHexString(id) json[field] = ObjectId.createFromHexString(id)
} }
@ -402,7 +410,14 @@ class MongoIntegration implements IntegrationBase {
if (Array.isArray(json)) { if (Array.isArray(json)) {
for (let i = 0; i < json.length; i++) { for (let i = 0; i < json.length; i++) {
json[i] = interpolateObjectIds(json[i]) if (self.hasObjectId(json[i])) {
const id = self.matchId(json[i])
if (id) {
json[i] = ObjectId.createFromHexString(id)
}
} else {
json[i] = interpolateObjectIds(json[i])
}
} }
return json return json
} }