fix(openapi): fix index out of bounds for sort order (#10168)

This commit is contained in:
RyanHolstien 2024-03-29 13:43:33 -05:00 committed by GitHub
parent cb9ee8987f
commit c1eff5982e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 20 deletions

View File

@ -16,10 +16,7 @@ import com.linkedin.metadata.graph.elastic.ElasticSearchGraphService;
import com.linkedin.metadata.models.registry.EntityRegistry;
import com.linkedin.metadata.query.filter.RelationshipDirection;
import com.linkedin.metadata.query.filter.RelationshipFilter;
import com.linkedin.metadata.query.filter.SortCriterion;
import com.linkedin.metadata.query.filter.SortOrder;
import com.linkedin.metadata.search.utils.QueryUtils;
import com.linkedin.metadata.utils.SearchUtil;
import io.datahubproject.openapi.exception.UnauthorizedException;
import io.datahubproject.openapi.v2.models.GenericRelationship;
import io.datahubproject.openapi.v2.models.GenericScrollResult;
@ -28,7 +25,6 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -49,16 +45,6 @@ import org.springframework.web.bind.annotation.RestController;
name = "Generic Relationships",
description = "APIs for ingesting and accessing entity relationships.")
public class RelationshipController {
private static final String[] SORT_ORDERS = {"ASCENDING", "ASCENDING", "ASCENDING", "ASCENDING"};
private static final List<SortCriterion> EDGE_SORT_CRITERION;
static {
EDGE_SORT_CRITERION =
IntStream.range(0, Edge.KEY_FIELDS.length)
.mapToObj(
idx -> SearchUtil.sortBy(Edge.KEY_FIELDS[idx], SortOrder.valueOf(SORT_ORDERS[idx])))
.collect(Collectors.toList());
}
@Autowired private EntityRegistry entityRegistry;
@Autowired private ElasticSearchGraphService graphService;
@ -97,7 +83,7 @@ public class RelationshipController {
null,
List.of(relationshipType),
new RelationshipFilter().setDirection(RelationshipDirection.UNDIRECTED),
EDGE_SORT_CRITERION,
Edge.EDGE_SORT_CRITERION,
scrollId,
count,
null,
@ -180,7 +166,7 @@ public class RelationshipController {
new RelationshipFilter()
.setDirection(RelationshipDirection.UNDIRECTED)
.setOr(QueryUtils.newFilter("destination.urn", entityUrn).getOr()),
EDGE_SORT_CRITERION,
Edge.EDGE_SORT_CRITERION,
scrollId,
count,
null,
@ -197,7 +183,7 @@ public class RelationshipController {
new RelationshipFilter()
.setDirection(RelationshipDirection.UNDIRECTED)
.setOr(QueryUtils.newFilter("source.urn", entityUrn).getOr()),
EDGE_SORT_CRITERION,
Edge.EDGE_SORT_CRITERION,
scrollId,
count,
null,

View File

@ -1,11 +1,17 @@
package com.linkedin.metadata.graph;
import com.linkedin.common.urn.Urn;
import com.linkedin.metadata.query.filter.SortCriterion;
import com.linkedin.metadata.query.filter.SortOrder;
import com.linkedin.metadata.utils.SearchUtil;
import com.linkedin.util.Pair;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -75,8 +81,20 @@ public class Edge {
}
}
public static final String[] KEY_FIELDS = {
"source.urn", "destination.urn", "relationshipType", "lifeCycleOwner"
};
public static final String SOURCE_URN_FIELD = "source.urn";
public static final String DESTINATION_URN_FIELD = "destination.urn";
public static final String RELATIONSHIP_TYPE_FIELD = "relationshipType";
public static final String LIFE_CYCLE_OWNER_FIELD = "lifeCycleOwner";
public static final List<Pair<String, SortOrder>> KEY_SORTS =
List.of(
new Pair<>(SOURCE_URN_FIELD, SortOrder.ASCENDING),
new Pair<>(DESTINATION_URN_FIELD, SortOrder.ASCENDING),
new Pair<>(RELATIONSHIP_TYPE_FIELD, SortOrder.ASCENDING),
new Pair<>(LIFE_CYCLE_OWNER_FIELD, SortOrder.ASCENDING));
public static List<SortCriterion> EDGE_SORT_CRITERION =
KEY_SORTS.stream()
.map(entry -> SearchUtil.sortBy(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
private static final String DOC_DELIMETER = "--";
}