Skip to content

Commit be1eb16

Browse files
committed
Fix eclipse-vertx#550: Fixed merging array-type attributes when only one of the two User objects has the attribute
1 parent a1cf361 commit be1eb16

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

vertx-auth-common/src/main/java/io/vertx/ext/auth/impl/UserImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public User merge(User other) {
128128
Object rhsValue = otherAttrs.getValue(key);
129129
// accumulate
130130
if (lhsValue == null) {
131-
attrs.put(key, rhsValue instanceof JsonArray ? new JsonArray().add(rhsValue) : rhsValue);
131+
attrs.put(key, rhsValue instanceof JsonArray ? new JsonArray().addAll((JsonArray) rhsValue) : rhsValue);
132132
} else if (lhsValue instanceof JsonArray) {
133133
if (rhsValue instanceof JsonArray) {
134134
((JsonArray) lhsValue).addAll((JsonArray) rhsValue);

vertx-auth-common/src/test/java/io/vertx/ext/auth/UserTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,27 @@ public void testMerge() {
159159
// expectation
160160
assertEquals("B", userA.principal().getString("access_token"));
161161
assertEquals(new JsonArray().add("read").add("write"), userA.attributes().getJsonArray("roles"));
162+
163+
// or 1st is array and 2nd is null
164+
165+
userA = User.create(new JsonObject().put("access_token", "A"), new JsonObject().put("roles", new JsonArray().add("read")));
166+
userB = User.create(new JsonObject().put("access_token", "B"));
167+
168+
userA.merge(userB);
169+
170+
// expectation
171+
assertEquals("B", userA.principal().getString("access_token"));
172+
assertEquals(new JsonArray().add("read"), userA.attributes().getJsonArray("roles"));
173+
174+
// or 1st is null and 2nd is array
175+
176+
userA = User.create(new JsonObject().put("access_token", "A"));
177+
userB = User.create(new JsonObject().put("access_token", "B"), new JsonObject().put("roles", new JsonArray().add("write")));
178+
179+
userA.merge(userB);
180+
181+
// expectation
182+
assertEquals("B", userA.principal().getString("access_token"));
183+
assertEquals(new JsonArray().add("write"), userA.attributes().getJsonArray("roles"));
162184
}
163185
}

0 commit comments

Comments
 (0)