This is an automated email from the ASF dual-hosted git repository.

ilgrosso pushed a commit to branch 3_0_X
in repository https://gitbox.apache.org/repos/asf/syncope.git


The following commit(s) were added to refs/heads/3_0_X by this push:
     new a09c6ace81 [SYNCOPE-1830] fix membership plain attribute map (#862)
a09c6ace81 is described below

commit a09c6ace81887e00ba05c38fd0ae5e1f28d1d883
Author: Andrea Patricelli <[email protected]>
AuthorDate: Tue Oct 15 13:01:47 2024 +0200

    [SYNCOPE-1830] fix membership plain attribute map (#862)
---
 .../elasticsearch/client/ElasticsearchUtils.java   | 11 ++++++++---
 .../ext/opensearch/client/OpenSearchUtils.java     | 11 ++++++++---
 .../org/apache/syncope/fit/core/SearchITCase.java  | 22 ++++++++++++++++++++--
 3 files changed, 36 insertions(+), 8 deletions(-)

diff --git 
a/ext/elasticsearch/client-elasticsearch/src/main/java/org/apache/syncope/ext/elasticsearch/client/ElasticsearchUtils.java
 
b/ext/elasticsearch/client-elasticsearch/src/main/java/org/apache/syncope/ext/elasticsearch/client/ElasticsearchUtils.java
index 5d607b8e14..257a0fed44 100644
--- 
a/ext/elasticsearch/client-elasticsearch/src/main/java/org/apache/syncope/ext/elasticsearch/client/ElasticsearchUtils.java
+++ 
b/ext/elasticsearch/client-elasticsearch/src/main/java/org/apache/syncope/ext/elasticsearch/client/ElasticsearchUtils.java
@@ -103,6 +103,7 @@ public class ElasticsearchUtils {
      * @param any user, group or any object to index
      * @return document specialized with content from the provided any
      */
+    @SuppressWarnings("unchecked")
     @Transactional
     public Map<String, Object> document(final Any<?> any) {
         Collection<String> resources = any instanceof User
@@ -216,10 +217,14 @@ public class ElasticsearchUtils {
 
                 Optional.ofNullable(mAttr.getUniqueValue()).ifPresent(v -> 
values.add(v.getValue()));
 
-                if (!builder.containsKey(mAttr.getSchema().getKey())) {
-                    builder.put(mAttr.getSchema().getKey(), new HashSet<>());
+                Object attr = 
builder.computeIfAbsent(mAttr.getSchema().getKey(), k -> new HashSet<>());
+                // also support case in which there is also an existing 
attribute set previously
+                if (attr instanceof Collection) {
+                    ((Collection<Object>) attr).addAll(values);
+                } else {
+                    values.add(attr);
+                    builder.put(mAttr.getSchema().getKey(), values.size() == 1 
? values.get(0) : values);
                 }
-                builder.put(mAttr.getSchema().getKey(), values.size() == 1 ? 
values.get(0) : values);
             }));
         }
         
diff --git 
a/ext/opensearch/client-opensearch/src/main/java/org/apache/syncope/ext/opensearch/client/OpenSearchUtils.java
 
b/ext/opensearch/client-opensearch/src/main/java/org/apache/syncope/ext/opensearch/client/OpenSearchUtils.java
index ded9a814b7..8f79317a84 100644
--- 
a/ext/opensearch/client-opensearch/src/main/java/org/apache/syncope/ext/opensearch/client/OpenSearchUtils.java
+++ 
b/ext/opensearch/client-opensearch/src/main/java/org/apache/syncope/ext/opensearch/client/OpenSearchUtils.java
@@ -103,6 +103,7 @@ public class OpenSearchUtils {
      * @param any user, group or any object to index
      * @return document specialized with content from the provided any
      */
+    @SuppressWarnings("unchecked")
     @Transactional
     public Map<String, Object> document(final Any<?> any) {
         Collection<String> resources = any instanceof User
@@ -216,10 +217,14 @@ public class OpenSearchUtils {
 
                 Optional.ofNullable(mAttr.getUniqueValue()).ifPresent(v -> 
values.add(v.getValue()));
 
-                if (!builder.containsKey(mAttr.getSchema().getKey())) {
-                    builder.put(mAttr.getSchema().getKey(), new HashSet<>());
+                Object attr = 
builder.computeIfAbsent(mAttr.getSchema().getKey(), k -> new HashSet<>());
+                // also support case in which there is also an existing 
attribute set previously
+                if (attr instanceof Collection) {
+                    ((Collection<Object>) attr).addAll(values);
+                } else {
+                    values.add(attr);
+                    builder.put(mAttr.getSchema().getKey(), values.size() == 1 
? values.get(0) : values);
                 }
-                builder.put(mAttr.getSchema().getKey(), values.size() == 1 ? 
values.get(0) : values);
             }));
         }
         
diff --git 
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/SearchITCase.java
 
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/SearchITCase.java
index cfe7e8daa6..9f6364af0c 100644
--- 
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/SearchITCase.java
+++ 
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/SearchITCase.java
@@ -43,6 +43,7 @@ import org.apache.syncope.common.lib.request.AnyObjectCR;
 import org.apache.syncope.common.lib.request.AnyObjectUR;
 import org.apache.syncope.common.lib.request.AttrPatch;
 import org.apache.syncope.common.lib.request.GroupCR;
+import org.apache.syncope.common.lib.request.GroupUR;
 import org.apache.syncope.common.lib.request.MembershipUR;
 import org.apache.syncope.common.lib.request.UserCR;
 import org.apache.syncope.common.lib.request.UserUR;
@@ -54,6 +55,7 @@ import org.apache.syncope.common.lib.to.PagedConnObjectResult;
 import org.apache.syncope.common.lib.to.PagedResult;
 import org.apache.syncope.common.lib.to.RealmTO;
 import org.apache.syncope.common.lib.to.RoleTO;
+import org.apache.syncope.common.lib.to.TypeExtensionTO;
 import org.apache.syncope.common.lib.to.UserTO;
 import org.apache.syncope.common.lib.types.AnyTypeKind;
 import org.apache.syncope.common.lib.types.ClientExceptionType;
@@ -1058,16 +1060,32 @@ public class SearchITCase extends AbstractITCase {
         // search user by membership attribute
         UserTO puccini = USER_SERVICE.read("puccini");
         GroupTO additional = GROUP_SERVICE.read("additional");
+        GroupTO employee = GROUP_SERVICE.read("employee");
+        TypeExtensionTO typeExtensionTO = new TypeExtensionTO();
+        typeExtensionTO.setAnyType(AnyTypeKind.USER.name());
+        typeExtensionTO.getAuxClasses().add("other");
+        updateGroup(new 
GroupUR.Builder(employee.getKey()).typeExtension(typeExtensionTO).build());
         // add a membership and its plain attribute
-        updateUser(new UserUR.Builder(puccini.getKey()).memberships(
+        updateUser(new UserUR.Builder(puccini.getKey())
+                .plainAttr(attrAddReplacePatch("ctype", "myownctype"))
+                .memberships(
                 new 
MembershipUR.Builder(additional.getKey()).plainAttrs(attr("ctype", 
"additionalctype"))
-                        .build()).build());
+                        .build(), new MembershipUR.Builder(employee.getKey())
+                                .plainAttrs(attr("ctype", 
"additionalemployeectype"))
+                                .build()).build());
         await().until(() -> USER_SERVICE.search(new 
AnyQuery.Builder().page(1).size(10)
                 
.fiql(SyncopeClient.getUserSearchConditionBuilder().is("ctype").equalTo("additionalctype").query())
                 .build()).getTotalCount() == 1);
         assertTrue(USER_SERVICE.search(new AnyQuery.Builder().page(1).size(10)
                 
.fiql(SyncopeClient.getUserSearchConditionBuilder().is("ctype").equalTo("additionalctype").query())
                 .build()).getResult().stream().anyMatch(u -> 
"puccini".equals(u.getUsername())));
+        assertTrue(USER_SERVICE.search(new AnyQuery.Builder().page(1).size(10)
+                
.fiql(SyncopeClient.getUserSearchConditionBuilder().is("ctype").equalTo("additionalemployeectype")
+                        .query()).build()).getResult().stream().anyMatch(u -> 
"puccini".equals(u.getUsername())));
+        // check also that search on user plain attribute (not in membership) 
works
+        assertTrue(USER_SERVICE.search(new AnyQuery.Builder().page(1).size(10)
+                
.fiql(SyncopeClient.getUserSearchConditionBuilder().is("ctype").equalTo("myownctype").query())
+                .build()).getResult().stream().anyMatch(u -> 
"puccini".equals(u.getUsername())));
     }
     
     @Test

Reply via email to