Repository: parse-community/Parse-SDK-JS
Pull request #1488 (2022-06-05 10:06:56)

fix: subscription to a LiveQuery containing `ParseQuery.select` overrides properties
### New Pull Request Checklist
<!--
    Check every following box [x] before submitting your PR.
    Click the "Preview" tab for better readability.
    Thanks for contributing to Parse Platform!
-->

- [x] I am not disclosing a [vulnerability](https://github.com/parse-community/parse-server/blob/master/SECURITY.md).
- [x] I am creating this PR in reference to an [issue](https://github.com/parse-community/Parse-SDK-JS/issues?q=is%3Aissue).

### Issue Description
<!-- Add a brief description of the issue this PR solves. -->

When using `.select` with LQ, the Parse JS SDK strips out all keys on objects that have the same reference.

Related issue: #1487

### Approach
<!-- Add a description of the approach in this PR. -->

If `.select` is used in the LQ query, don't set all keys to undefined.

### TODOs before merging
<!--
    Add TODOs that need to be completed before merging this PR.
    Delete TODOs that do not apply to this PR.
-->

- [x] Add tests

parse-github-assistant[bot]: 
### Thanks for opening this pull request!

- ❌ Please check all required checkboxes at the top, otherwise your pull request will be closed.

- ⚠️ Remember that a security vulnerability must only be reported confidentially, see our [Security Policy](https://github.com/parse-community/parse-server/blob/master/SECURITY.md). If you are not sure whether the issue is a security vulnerability, the safest way is to treat it as such and submit it confidentially to us for evaluation.

<!-- parse-issue-bot-meta-tag-id {"requireTopCheckboxes":true} -->


codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1488?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community) Report
> Merging [#1488](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1488?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community) (2906f67) into [alpha](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/8326a6f1d7cda0ca8c6f1a3a7ea82448881e118e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community) (8326a6f) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

> :exclamation: Current head 2906f67 differs from pull request most recent head ab5be44. Consider uploading reports for the commit ab5be44 to get more accurate results

```diff
@@           Coverage Diff           @@
##            alpha    #1488   +/-   ##
=======================================
  Coverage   99.94%   99.94%           
=======================================
  Files          61       61           
  Lines        5954     5955    +1     
  Branches     1357     1358    +1     
=======================================
+ Hits         5951     5952    +1     
  Misses          3        3           
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1488?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community) | Coverage Δ | |
|---|---|---|
| [src/LiveQueryClient.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1488/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community#diff-c3JjL0xpdmVRdWVyeUNsaWVudC5qcw==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1488?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1488?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community). Last update [841ca1f...ab5be44](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1488?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community).

parse-github-assistant[bot]: I will reformat the title to use the proper commit message syntax.
mtrezza: Waiting or https://github.com/parse-community/Parse-SDK-JS/issues/1487#issuecomment-1172873651 to resolve before merge

Patch:
diff --git a/src/LiveQueryClient.js b/src/LiveQueryClient.js
index 3066ac1..2210dee 100644
--- a/src/LiveQueryClient.js
+++ b/src/LiveQueryClient.js
@@ -419,7 +419,10 @@ class LiveQueryClient extends EventEmitter {
         data.original = ParseObject.fromJSON(data.original, false);
       }
       delete data.object.__type;
-      const parseObject = ParseObject.fromJSON(data.object, override);
+      const parseObject = ParseObject.fromJSON(
+        data.object,
+        !(subscription.query && subscription.query._select) ? override : false
+      );
 
       if (data.original) {
         subscription.emit(data.op, parseObject, data.original, response);


Test patch:
diff --git a/src/__tests__/LiveQueryClient-test.js b/src/__tests__/LiveQueryClient-test.js
index 812ebb9..4329bcd 100644
--- a/src/__tests__/LiveQueryClient-test.js
+++ b/src/__tests__/LiveQueryClient-test.js
@@ -522,6 +522,43 @@ describe('LiveQueryClient', () => {
     spy.mockRestore();
   });
 
+  it('can handle select in websocket payload', () => {
+    const liveQueryClient = new LiveQueryClient({
+      applicationId: 'applicationId',
+      serverURL: 'ws://test',
+      javascriptKey: 'javascriptKey',
+      masterKey: 'masterKey',
+      sessionToken: 'sessionToken',
+    });
+    // Add mock subscription
+    const subscription = new events.EventEmitter();
+    subscription.query = new ParseQuery('Test').select('foo');
+    liveQueryClient.subscriptions.set(1, subscription);
+    const object = new ParseObject('Test');
+    const original = new ParseObject('Test');
+    object.set('key', 'value');
+    original.set('key', 'old');
+    const data = {
+      op: 'update',
+      clientId: 1,
+      requestId: 1,
+      object: object._toFullJSON(),
+      original: original._toFullJSON(),
+    };
+    const event = {
+      data: JSON.stringify(data),
+    };
+
+    const spy = jest
+      .spyOn(ParseObject, 'fromJSON')
+      .mockImplementationOnce(() => original)
+      .mockImplementationOnce(() => object);
+
+    liveQueryClient._handleWebSocketMessage(event);
+    expect(ParseObject.fromJSON.mock.calls[1][1]).toEqual(false);
+    spy.mockRestore();
+  });
+
   it('can handle WebSocket response unset field', async () => {
     const liveQueryClient = new LiveQueryClient({
       applicationId: 'applicationId',