Source:
public Set<PersistentResource> getRelation(String relation, List<String> ids, Optional<FilterExpression> filter, Optional<Sorting> sorting, Optional<Pagination> pagination) { FilterExpression filterExpression = null; boolean skipNew = false; Class<?> entityType = dictionary.getParameterizedType(getResourceClass(), relation); Set<PersistentResource> relations = new LinkedHashSet<>(); if (entityType == null) { throw new InvalidAttributeException(relation, type); } else { Class<?> idType = dictionary.getIdType(entityType); String idField = dictionary.getIdFieldName(entityType); if (!ids.isEmpty()) { FilterExpression idExpression = new FilterPredicate( new FilterPredicate.PathElement( entityType, idType, idField), Operator.IN, ids.stream() .filter(id -> requestScope.getObjectById(dictionary.getJsonAliasFor(entityType), id) == null) .map(id -> CoerceUtil.coerce(id, idType)).collect(Collectors.toList())); filterExpression = filter .map(fe -> (FilterExpression) new AndFilterExpression(idExpression, fe)) .orElse(idExpression); } else { filterExpression = filter.orElse(null); } } Set<PersistentResource> resources = filter(ReadPermission.class, (Set) getRelationChecked(relation, Optional.ofNullable(filterExpression), sorting, pagination)); Set<String> added = new HashSet<>(); for (PersistentResource childResource : resources) { for (String id : ids) { if (childResource.matchesId(id)) { relations.add(childResource); added.add(id); break; } } } String missedIds = ids.stream() .filter(id -> !added.contains(id)) .reduce(new StringBuilder(), (prev, curr) -> (prev.length() > 0) ? prev.append(curr).append(",") : prev.append(curr), StringBuilder::append) .toString(); if (!missedIds.isEmpty()) { throw new InvalidObjectIdentifierException(missedIds, relation); } return relations; }

Target:
public Set<PersistentResource> getRelation(String relation, List<String> ids, Optional<FilterExpression> filter, Optional<Sorting> sorting, Optional<Pagination> pagination) { FilterExpression filterExpression; Class<?> entityType = dictionary.getParameterizedType(getResourceClass(), relation); Set<PersistentResource> newResources = new LinkedHashSet<>(); if (entityType == null) { throw new InvalidAttributeException(relation, type); } else { if (!ids.isEmpty()) { String typeAlias = dictionary.getJsonAliasFor(entityType); newResources = requestScope.getNewPersistentResources().stream() .filter(resource -> typeAlias.equals(resource.getType()) && ids.contains(resource.getUUID().orElse(""))) .collect(Collectors.toSet()); FilterExpression idExpression = buildIdFilterExpression(ids, entityType); filterExpression = filter .map(fe -> (FilterExpression) new AndFilterExpression(idExpression, fe)) .orElse(idExpression); } else { filterExpression = filter.orElse(null); } } Set<PersistentResource> existingResources = filter(ReadPermission.class, (Set) getRelationChecked(relation, Optional.ofNullable(filterExpression), sorting, pagination)); Set<PersistentResource> allResources = Sets.union(newResources, existingResources); Set<String> allExpectedIds = allResources.stream() .map(resource -> (String) resource.getUUID().orElseGet(resource::getId)) .collect(Collectors.toSet()); Set<String> missedIds = Sets.difference(new HashSet<>(ids), allExpectedIds); if (!missedIds.isEmpty()) { throw new InvalidObjectIdentifierException(missedIds.toString(), relation); } return allResources; }