Skip to content

Commit

Permalink
fix: non-matching inScope constraints must be excluded when deriving …
Browse files Browse the repository at this point in the history
…request of different scope
  • Loading branch information
novoj committed Dec 19, 2024
1 parent 9474937 commit d937fb5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@
import io.evitadb.api.query.Constraint;
import io.evitadb.api.query.Query;
import io.evitadb.api.query.QueryUtils;
import io.evitadb.api.query.filter.EntityLocaleEquals;
import io.evitadb.api.query.filter.EntityPrimaryKeyInSet;
import io.evitadb.api.query.filter.EntityScope;
import io.evitadb.api.query.filter.FilterBy;
import io.evitadb.api.query.filter.HierarchyFilterConstraint;
import io.evitadb.api.query.filter.HierarchyWithin;
import io.evitadb.api.query.filter.PriceInCurrency;
import io.evitadb.api.query.filter.PriceInPriceLists;
import io.evitadb.api.query.filter.PriceValidIn;
import io.evitadb.api.query.filter.*;
import io.evitadb.api.query.head.Collection;
import io.evitadb.api.query.order.OrderBy;
import io.evitadb.api.query.require.*;
Expand Down Expand Up @@ -1121,6 +1113,7 @@ private static class ScopeEnforcer implements BiFunction<ConstraintCloneVisitor,
private final EntityScope enforcedScope;
private boolean scopeFound;

@Nullable
@Override
public Constraint<?> apply(ConstraintCloneVisitor constraintCloneVisitor, Constraint<?> constraint) {
if (constraint instanceof EntityScope) {
Expand All @@ -1129,6 +1122,9 @@ public Constraint<?> apply(ConstraintCloneVisitor constraintCloneVisitor, Constr
} else if (constraint instanceof FilterBy && !this.scopeFound) {
constraintCloneVisitor.addOnCurrentLevel(this.enforcedScope);
return constraint;
} else if (constraint instanceof FilterInScope fis) {
// when the `inScope` doesn't match the enforced scope, exclude the container with its contents
return this.enforcedScope.getScope().contains(fis.getScope()) ? fis : null;
} else {
return constraint;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@
import java.util.Locale;

import static io.evitadb.api.query.Query.query;
import static io.evitadb.api.query.QueryConstraints.collection;
import static io.evitadb.api.query.QueryConstraints.entityLocaleEquals;
import static io.evitadb.api.query.QueryConstraints.filterBy;
import static io.evitadb.api.query.QueryConstraints.scope;
import static io.evitadb.api.query.QueryConstraints.*;
import static io.evitadb.api.query.filter.AttributeSpecialValue.NOT_NULL;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
Expand Down Expand Up @@ -138,4 +136,37 @@ void shouldReplaceScopeInRequest() {
);
}

@Test
void shouldReplaceScopeInRequestAndExcludeNonMatchingContainers() {
final EvitaRequest request = new EvitaRequest(
query(
collection("a"),
filterBy(
inScope(Scope.LIVE, attributeIs("code", NOT_NULL)),
scope(Scope.LIVE, Scope.ARCHIVED)
)
),
OffsetDateTime.now(),
SealedEntity.class,
null
);
final EvitaRequest copy = request.deriveCopyWith(
"b", null, null, Locale.ENGLISH, EnumSet.of(Scope.ARCHIVED)
);
assertEquals("b", copy.getEntityType());
assertEquals(Locale.ENGLISH, copy.getLocale());
assertEquals(EnumSet.of(Scope.ARCHIVED), copy.getScopes());
assertEquals(
"""
query(
collection('b'),
filterBy(
scope(ARCHIVED)
),
require()
)""",
copy.getQuery().prettyPrint()
);
}

}

0 comments on commit d937fb5

Please sign in to comment.