Skip to content

Commit

Permalink
Filter nested unwritable attributes from updates #637
Browse files Browse the repository at this point in the history
refactoring ConstraintsHandler for maintainability
  • Loading branch information
andrus committed Jun 29, 2023
1 parent 57324b2 commit 508872d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import java.util.ArrayList;
import java.util.List;

class ReadConstraints {
class ReadPropertyFilter {

private static final Logger LOGGER = LoggerFactory.getLogger(ReadConstraints.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ReadPropertyFilter.class);

static <T> void apply(ResourceEntity<T> entity) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class SelectConstraints {

public <T> void apply(SelectContext<T> context) {
ReadConstraints.apply(context.getEntity());
SizeConstraints.apply(context.getEntity(), context.getSizeConstraints());
ReadPropertyFilter.apply(context.getEntity());
SizeFilter.apply(context.getEntity(), context.getSizeConstraints());
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package io.agrest.runtime.constraints;

import io.agrest.ResourceEntity;
import io.agrest.SizeConstraints;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class SizeConstraints {
class SizeFilter {

private static final Logger LOGGER = LoggerFactory.getLogger(SizeConstraints.class);
private static final Logger LOGGER = LoggerFactory.getLogger(SizeFilter.class);

public static <T> void apply(ResourceEntity<T> entity, io.agrest.SizeConstraints constraints) {
public static <T> void apply(ResourceEntity<T> entity, SizeConstraints constraints) {
if (constraints != null) {

// fetchOffset - do not exceed source offset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class UpdateConstraints {

public <T> void apply(UpdateContext<T> context) {
WriteConstraints.apply(context.getEntity(), context.getUpdates());
ReadConstraints.apply(context.getEntity());
WritePropertyFilter.apply(context.getEntity(), context.getUpdates());
ReadPropertyFilter.apply(context.getEntity());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import java.util.Set;
import java.util.function.Consumer;

class WriteConstraints {
class WritePropertyFilter {

private static final Logger LOGGER = LoggerFactory.getLogger(WriteConstraints.class);
private static final Logger LOGGER = LoggerFactory.getLogger(WritePropertyFilter.class);

public static <T> void apply(ResourceEntity<T> entity, Collection<EntityUpdate<T>> updates) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import static org.junit.jupiter.api.Assertions.*;

public class ReadConstraintsTest {
public class ReadPropertyFilterTest {

static final AgSchema schema = new LazySchema(List.of(new AnnotationsAgEntityCompiler(Map.of())));

Expand All @@ -28,7 +28,7 @@ public void constrainResponse_Default() {
te1.ensureAttribute("a", false);
te1.ensureAttribute("b", false);

ReadConstraints.apply(te1);
ReadPropertyFilter.apply(te1);
assertEquals(1, te1.getBaseProjection().getAttributes().size());
assertNotNull(te1.getBaseProjection().getAttribute("a"));
assertTrue(te1.getChildren().isEmpty());
Expand All @@ -43,7 +43,7 @@ public void constrainResponse_None() {
te1.ensureAttribute("m", false);
te1.ensureAttribute("n", false);

ReadConstraints.apply(te1);
ReadPropertyFilter.apply(te1);
assertEquals(2, te1.getBaseProjection().getAttributes().size());
assertNotNull(te1.getBaseProjection().getAttribute("m"));
assertNotNull(te1.getBaseProjection().getAttribute("n"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.agrest.ResourceEntity;
import io.agrest.RootResourceEntity;
import io.agrest.SizeConstraints;
import io.agrest.annotation.AgAttribute;
import io.agrest.annotation.AgId;
import io.agrest.annotation.AgRelationship;
Expand All @@ -18,7 +19,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SizeConstraintsTest {
public class SizeFilterTest {

private static AgSchema schema;

Expand All @@ -33,30 +34,30 @@ public void fetchOffset() {

AgEntity<Tr> entity = schema.getEntity(Tr.class);

io.agrest.SizeConstraints s1 = new io.agrest.SizeConstraints().fetchOffset(5);
io.agrest.SizeConstraints s2 = new io.agrest.SizeConstraints().fetchOffset(0);
SizeConstraints s1 = new SizeConstraints().fetchOffset(5);
SizeConstraints s2 = new SizeConstraints().fetchOffset(0);

ResourceEntity<Tr> t1 = new RootResourceEntity<>(entity);
t1.setStart(0);
SizeConstraints.apply(t1, s1);
SizeFilter.apply(t1, s1);
assertEquals(0, t1.getStart());
assertEquals(5, s1.getFetchOffset());

ResourceEntity<Tr> t2 = new RootResourceEntity<>(entity);
t2.setStart(3);
SizeConstraints.apply(t2, s1);
SizeFilter.apply(t2, s1);
assertEquals(3, t2.getStart());
assertEquals(5, s1.getFetchOffset());

ResourceEntity<Tr> t3 = new RootResourceEntity<>(entity);
t3.setStart(6);
SizeConstraints.apply(t3, s1);
SizeFilter.apply(t3, s1);
assertEquals(5, t3.getStart());
assertEquals(5, s1.getFetchOffset());

ResourceEntity<Tr> t4 = new RootResourceEntity<>(entity);
t4.setStart(6);
SizeConstraints.apply(t4, s2);
SizeFilter.apply(t4, s2);
assertEquals(6, t4.getStart());
assertEquals(0, s2.getFetchOffset());
}
Expand All @@ -66,41 +67,41 @@ public void fetchLimit() {

AgEntity<Tr> entity = schema.getEntity(Tr.class);

io.agrest.SizeConstraints s1 = new io.agrest.SizeConstraints().fetchLimit(5);
io.agrest.SizeConstraints s2 = new io.agrest.SizeConstraints().fetchLimit(0);
SizeConstraints s1 = new SizeConstraints().fetchLimit(5);
SizeConstraints s2 = new SizeConstraints().fetchLimit(0);

ResourceEntity<Tr> t1 = new RootResourceEntity<>(entity);
SizeConstraints.apply(t1, s1);
SizeFilter.apply(t1, s1);
assertEquals(5, t1.getLimit());
assertEquals(5, s1.getFetchLimit());

ResourceEntity<Tr> t1_1 = new RootResourceEntity<>(entity);
t1_1.setLimit(0);
SizeConstraints.apply(t1_1, s1);
SizeFilter.apply(t1_1, s1);
assertEquals(5, t1_1.getLimit());
assertEquals(5, s1.getFetchLimit());

ResourceEntity<Tr> t1_2 = new RootResourceEntity<>(entity);
t1_2.setLimit(-1);
SizeConstraints.apply(t1_2, s1);
SizeFilter.apply(t1_2, s1);
assertEquals(5, t1_2.getLimit());
assertEquals(5, s1.getFetchLimit());

ResourceEntity<Tr> t2 = new RootResourceEntity<>(entity);
t2.setLimit(3);
SizeConstraints.apply(t2, s1);
SizeFilter.apply(t2, s1);
assertEquals(3, t2.getLimit());
assertEquals(5, s1.getFetchLimit());

ResourceEntity<Tr> t3 = new RootResourceEntity<>(entity);
t3.setLimit(6);
SizeConstraints.apply(t3, s1);
SizeFilter.apply(t3, s1);
assertEquals(5, t3.getLimit());
assertEquals(5, s1.getFetchLimit());

ResourceEntity<Tr> t4 = new RootResourceEntity<>(entity);
t4.setLimit(6);
SizeConstraints.apply(t4, s2);
SizeFilter.apply(t4, s2);
assertEquals(6, t4.getLimit());
assertEquals(0, s2.getFetchLimit());
}
Expand Down

0 comments on commit 508872d

Please sign in to comment.