Skip to content

Commit f7af48d

Browse files
flovilmartArthur Cinader
authored and
Arthur Cinader
committed
Fix/3678 overloaded query constraints (#3723)
* Added failing test * Updated test description * Properly handle equalities with additional operator constraints * adds continuation to silence rejected promises * Wrap json parsing * nits
1 parent d2b5be2 commit f7af48d

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

spec/ParseQuery.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,6 +1891,41 @@ describe('Parse.Query testing', () => {
18911891
});
18921892
});
18931893

1894+
it("equalTo on same column as $dontSelect should not break $dontSelect functionality (#3678)", function(done) {
1895+
var AuthorObject = Parse.Object.extend("Author");
1896+
var BlockedObject = Parse.Object.extend("Blocked");
1897+
var PostObject = Parse.Object.extend("Post");
1898+
1899+
var postAuthor = null;
1900+
var requestUser = null;
1901+
1902+
return new AuthorObject({ name: "Julius"}).save().then((user) => {
1903+
postAuthor = user;
1904+
return new AuthorObject({ name: "Bob"}).save();
1905+
}).then((user) => {
1906+
requestUser = user;
1907+
var objects = [
1908+
new PostObject({ author: postAuthor, title: "Lorem ipsum" }),
1909+
new PostObject({ author: requestUser, title: "Kafka" }),
1910+
new PostObject({ author: requestUser, title: "Brown fox" }),
1911+
new BlockedObject({ blockedBy: postAuthor, blockedUser: requestUser})
1912+
];
1913+
return Parse.Object.saveAll(objects);
1914+
}).then(() => {
1915+
var banListQuery = new Parse.Query(BlockedObject);
1916+
banListQuery.equalTo("blockedUser", requestUser);
1917+
1918+
return new Parse.Query(PostObject)
1919+
.equalTo("author", postAuthor)
1920+
.doesNotMatchKeyInQuery("author", "blockedBy", banListQuery)
1921+
.find()
1922+
.then((r) => {
1923+
expect(r.length).toEqual(0);
1924+
done();
1925+
}, done.fail);
1926+
})
1927+
});
1928+
18941929
it("object with length", function(done) {
18951930
var TestObject = Parse.Object.extend("TestObject");
18961931
var obj = new TestObject();

src/RestQuery.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ RestQuery.prototype.buildRestWhere = function() {
169169
return this.replaceInQuery();
170170
}).then(() => {
171171
return this.replaceNotInQuery();
172+
}).then(() => {
173+
return this.replaceEquality();
172174
});
173175
}
174176

@@ -438,6 +440,39 @@ const cleanResultAuthData = function (result) {
438440
}
439441
};
440442

443+
const replaceEqualityConstraint = (constraint) => {
444+
if (typeof constraint !== 'object') {
445+
return constraint;
446+
}
447+
const equalToObject = {};
448+
let hasDirectConstraint = false;
449+
let hasOperatorConstraint = false;
450+
for (const key in constraint) {
451+
if (key.indexOf('$') !== 0) {
452+
hasDirectConstraint = true;
453+
equalToObject[key] = constraint[key];
454+
} else {
455+
hasOperatorConstraint = true;
456+
}
457+
}
458+
if (hasDirectConstraint && hasOperatorConstraint) {
459+
constraint['$eq'] = equalToObject;
460+
Object.keys(equalToObject).forEach((key) => {
461+
delete constraint[key];
462+
});
463+
}
464+
return constraint;
465+
}
466+
467+
RestQuery.prototype.replaceEquality = function() {
468+
if (typeof this.restWhere !== 'object') {
469+
return;
470+
}
471+
for (const key in this.restWhere) {
472+
this.restWhere[key] = replaceEqualityConstraint(this.restWhere[key]);
473+
}
474+
}
475+
441476
// Returns a promise for whether it was successful.
442477
// Populates this.response with an object that only has 'results'.
443478
RestQuery.prototype.runFind = function(options = {}) {

0 commit comments

Comments
 (0)