Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Parse/src/main/java/com/parse/ParseUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,16 @@ public Task<Void> then(Task<Void> task) throws Exception {
}
}

@Override
/* package */ void setState(ParseObject.State newState) {
// Avoid clearing sessionToken when updating the current user's State via ParseQuery result
if (isCurrentUser() && getSessionToken() != null
&& newState.get("sessionToken") == null) {
newState = newState.newBuilder().put("sessionToken", getSessionToken()).build();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic seems a tad messy, could we simplify it to the following?

if (/* ... */) {
  newState = // ...
}
super.setState(newState);

super.setState(newState);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we'd be creating unnecessary State objects with every setState(). Mind only creating a new State if necessary?


@Override
/* package */ void validateDelete() {
synchronized (mutex) {
Expand Down
35 changes: 35 additions & 0 deletions Parse/src/test/java/com/parse/ParseUserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,41 @@ public void testUpgradeToRevocableSessionAsync() throws Exception {
verify(currentUserController, times(1)).setAsync(user);
}

@Test
public void testDontOverwriteSessionTokenForCurrentUser() throws Exception {
ParseUser.State sessionTokenState = new ParseUser.State.Builder()
.sessionToken("sessionToken")
.put("key0", "value0")
.put("key1", "value1")
.isComplete(true)
.build();
ParseUser.State newState = new ParseUser.State.Builder()
.put("key0", "newValue0")
.put("key2", "value2")
.isComplete(true)
.build();
ParseUser.State emptyState = new ParseUser.State.Builder().isComplete(true).build();

ParseUser user = ParseObject.from(sessionTokenState);
user.setIsCurrentUser(true);
assertEquals(user.getSessionToken(), "sessionToken");
assertEquals(user.getString("key0"), "value0");
assertEquals(user.getString("key1"), "value1");

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: whitespace

user.setState(newState);
assertEquals(user.getSessionToken(), "sessionToken");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add some data in emptyState that we would want to merge in and verify that it's getting merged in correctly? A key that was in sessionTokenState that should be removed, a key that should be overwritten, and a key that should be added?

assertEquals(user.getString("key0"), "newValue0");
assertNull(user.getString("key1"));
assertEquals(user.getString("key2"), "value2");

user.setIsCurrentUser(false);
user.setState(emptyState);
assertNull(user.getSessionToken());
assertNull(user.getString("key0"));
assertNull(user.getString("key1"));
assertNull(user.getString("key2"));
}

//endregion

//region testUnlinkFromAsync
Expand Down