Skip to content

Commit

Permalink
fix(openapi): fix sort criteria parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanHolstien committed Dec 10, 2024
1 parent 84e50d8 commit c498603
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -190,7 +191,8 @@ public ResponseEntity<S> getEntities(
@RequestParam(value = "count", defaultValue = "10") Integer count,
@RequestParam(value = "query", defaultValue = "*") String query,
@RequestParam(value = "scrollId", required = false) String scrollId,
@RequestParam(value = "sort", required = false, defaultValue = "urn") String sortField,
@RequestParam(value = "sort", required = false, defaultValue = "urn") @Deprecated
String sortField,
@RequestParam(value = "sortCriteria", required = false) List<String> sortFields,
@RequestParam(value = "sortOrder", required = false, defaultValue = "ASCENDING")
String sortOrder,
Expand Down Expand Up @@ -222,14 +224,20 @@ public ResponseEntity<S> getEntities(
authentication.getActor().toUrnStr() + " is unauthorized to " + READ + " entities.");
}

SortOrder finalSortOrder =
SortOrder.valueOf(Optional.ofNullable(sortOrder).orElse("ASCENDING"));

List<SortCriterion> sortCriteria;
if (!CollectionUtils.isEmpty(sortFields)) {
if (!CollectionUtils.isEmpty(sortFields)
&& sortFields.stream().anyMatch(StringUtils::isNotBlank)) {
sortCriteria = new ArrayList<>();
sortFields.forEach(
field -> sortCriteria.add(SearchUtil.sortBy(field, SortOrder.valueOf(sortOrder))));
sortFields.stream()
.filter(StringUtils::isNotBlank)
.forEach(field -> sortCriteria.add(SearchUtil.sortBy(field, finalSortOrder)));
} else if (StringUtils.isNotBlank(sortField)) {
sortCriteria = Collections.singletonList(SearchUtil.sortBy(sortField, finalSortOrder));
} else {
sortCriteria =
Collections.singletonList(SearchUtil.sortBy(sortField, SortOrder.valueOf(sortOrder)));
sortCriteria = Collections.singletonList(SearchUtil.sortBy("urn", finalSortOrder));
}

ScrollResult result =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,19 +570,15 @@ private static void addExtraParameters(final Components components) {
"SortBy" + MODEL_VERSION,
new Parameter()
.in(NAME_QUERY)
.name("sort")
.name("sortCriteria")
.explode(true)
.description("Sort fields for pagination.")
.example(PROPERTY_URN)
.schema(
new Schema()
.type(TYPE_ARRAY)
._default(List.of(PROPERTY_URN))
.items(
new Schema<>()
.type(TYPE_STRING)
._enum(List.of(PROPERTY_URN))
._default(PROPERTY_URN))));
.items(new Schema<>().type(TYPE_STRING)._default(PROPERTY_URN))));
components.addParameters(
"SortOrder" + MODEL_VERSION,
new Parameter()
Expand Down

0 comments on commit c498603

Please sign in to comment.