Skip to content

Commit

Permalink
Fix eclipse-vertx#550: Fixed merging array-type attributes when only …
Browse files Browse the repository at this point in the history
…one of the two User objects has the attribute
  • Loading branch information
dmironowicz committed Apr 12, 2022
1 parent a1cf361 commit 3ea6603
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public User merge(User other) {
Object rhsValue = otherAttrs.getValue(key);
// accumulate
if (lhsValue == null) {
attrs.put(key, rhsValue instanceof JsonArray ? new JsonArray().add(rhsValue) : rhsValue);
attrs.put(key, rhsValue instanceof JsonArray ? new JsonArray().addAll((JsonArray) rhsValue) : rhsValue);
} else if (lhsValue instanceof JsonArray) {
if (rhsValue instanceof JsonArray) {
((JsonArray) lhsValue).addAll((JsonArray) rhsValue);
Expand Down
22 changes: 22 additions & 0 deletions vertx-auth-common/src/test/java/io/vertx/ext/auth/UserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,27 @@ public void testMerge() {
// expectation
assertEquals("B", userA.principal().getString("access_token"));
assertEquals(new JsonArray().add("read").add("write"), userA.attributes().getJsonArray("roles"));

// or 1st is array and 2nd is null

userA = User.create(new JsonObject().put("access_token", "A"), new JsonObject().put("roles", new JsonArray().add("read")));
userB = User.create(new JsonObject().put("access_token", "B"));

userA.merge(userB);

// expectation
assertEquals("B", userA.principal().getString("access_token"));
assertEquals(new JsonArray().add("read"), userA.attributes().getJsonArray("roles"));

// or 1st is null and 2nd is array

userA = User.create(new JsonObject().put("access_token", "A"));
userB = User.create(new JsonObject().put("access_token", "B"), new JsonObject().put("roles", new JsonArray().add("write")));

userA.merge(userB);

// expectation
assertEquals("B", userA.principal().getString("access_token"));
assertEquals(new JsonArray().add("write"), userA.attributes().getJsonArray("roles"));
}
}

0 comments on commit 3ea6603

Please sign in to comment.