From be1eb16cd4aca29dca8f707c53f987433bda9e1f Mon Sep 17 00:00:00 2001 From: Daniel Mironowicz <29960833+dmironowicz@users.noreply.github.com> Date: Tue, 12 Apr 2022 12:04:17 +0200 Subject: [PATCH] Fix #550: Fixed merging array-type attributes when only one of the two User objects has the attribute --- .../java/io/vertx/ext/auth/impl/UserImpl.java | 2 +- .../test/java/io/vertx/ext/auth/UserTest.java | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/vertx-auth-common/src/main/java/io/vertx/ext/auth/impl/UserImpl.java b/vertx-auth-common/src/main/java/io/vertx/ext/auth/impl/UserImpl.java index c025188a2..8be18665e 100644 --- a/vertx-auth-common/src/main/java/io/vertx/ext/auth/impl/UserImpl.java +++ b/vertx-auth-common/src/main/java/io/vertx/ext/auth/impl/UserImpl.java @@ -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); diff --git a/vertx-auth-common/src/test/java/io/vertx/ext/auth/UserTest.java b/vertx-auth-common/src/test/java/io/vertx/ext/auth/UserTest.java index d4d5e1007..03b2c7ea1 100644 --- a/vertx-auth-common/src/test/java/io/vertx/ext/auth/UserTest.java +++ b/vertx-auth-common/src/test/java/io/vertx/ext/auth/UserTest.java @@ -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")); } }