From a407c0f35cbb3749469fae1c5586208cca572c21 Mon Sep 17 00:00:00 2001
From: Viktor Kovtun <viktor.kovtun@id.ethz.ch>
Date: Mon, 30 Aug 2021 17:30:44 +0200
Subject: [PATCH] SSDM-11462 Added an exception in the case when unexpected
 fetch options are applied (not related to collections or maps).

---
 .../server/asapi/v3/helper/sort/SortAndPage.java  |  6 ++++++
 .../systemtest/asapi/v3/SearchSampleTest.java     | 15 +++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/helper/sort/SortAndPage.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/helper/sort/SortAndPage.java
index 654fcbc8e06..efb786a551e 100644
--- a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/helper/sort/SortAndPage.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/helper/sort/SortAndPage.java
@@ -175,6 +175,12 @@ public class SortAndPage {
                                 sortAndPage(((Map) value).values(), c, subFo);
                             } else
                             {
+                                if (subFo.getSortBy() != null && subFo.getSortBy().getSortings() != null &&
+                                        !subFo.getSortBy().getSortings().isEmpty())
+                                {
+                                    throw new IllegalArgumentException("Only collection and map types can be sorted.");
+                                }
+
                                 Collection newValue = sortAndPage(Collections.singleton(value), c, subFo);
                                 if (setMethod != null)
                                 {
diff --git a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/SearchSampleTest.java b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/SearchSampleTest.java
index 9945a916722..a9cb7b0db7a 100644
--- a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/SearchSampleTest.java
+++ b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/SearchSampleTest.java
@@ -847,6 +847,21 @@ public class SearchSampleTest extends AbstractSampleTest
         testSearch(TEST_USER, criteria, "/CISD/NEMO/CP-TEST-1", "/CISD/NEMO/DYNA-TEST-1");
     }
 
+    @Test(expectedExceptions = RuntimeException.class,
+            expectedExceptionsMessageRegExp = "Sorting and paging failed for object.*")
+    public void testSearchWithExperimentWithCodeSortingByExperiment()
+    {
+        final SampleSearchCriteria criteria = new SampleSearchCriteria().withOrOperator();
+        criteria.withExperiment().withCode().thatEquals("EXP-TEST-1");
+        criteria.withExperiment().withCode().thatEquals("EXP-TEST-2");
+
+        final SampleFetchOptions fetchOptions = new SampleFetchOptions();
+        fetchOptions.withExperiment().sortBy().code().asc();
+
+        final String sessionToken = v3api.login(TEST_USER, PASSWORD);
+        searchSamples(sessionToken, criteria, fetchOptions);
+    }
+
     @Test
     public void testSearchWithExperimentWithTypeIdSetToPermId()
     {
-- 
GitLab