Skip to content

Commit

Permalink
Annotation entity constraints block request-defined attributes #492
Browse files Browse the repository at this point in the history
  • Loading branch information
andrus committed Oct 10, 2021
1 parent 26635cb commit a5d38ec
Showing 1 changed file with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import io.agrest.Ag;
import io.agrest.DataResponse;
import io.agrest.cayenne.cayenne.main.E2;
import io.agrest.cayenne.cayenne.main.E22;
import io.agrest.cayenne.cayenne.main.E3;
import io.agrest.cayenne.cayenne.main.E4;
import io.agrest.cayenne.cayenne.main.*;
import io.agrest.cayenne.unit.AgCayenneTester;
import io.agrest.cayenne.unit.DbTest;
import io.agrest.meta.AgEntity;
Expand All @@ -28,7 +25,7 @@ public class GET_EntityOverlay_PerRequestIT extends DbTest {

@BQTestTool
static final AgCayenneTester tester = tester(Resource.class)
.entities(E2.class, E3.class, E4.class, E22.class)
.entities(E2.class, E3.class, E4.class, E10.class, E22.class)
.build();

@Test
Expand Down Expand Up @@ -81,6 +78,27 @@ public void test_Overlay_NoReaderCaching() {
.bodyEquals(2, "{\"fromRequest\":\"abc\"},{\"fromRequest\":\"abc\"}");
}

@Test
@Disabled("Until #492 is fixed by redesign in #491")
public void test_RequestOverlaidProperties_ConstrainedEntity() {

tester.e10().insertColumns("id", "c_int").values(2, 5).values(4, 8).exec();
tester.e22().insertColumns("id", "name")
.values(1, "a")
.values(2, "b")
.values(3, "c").exec();

tester.target("/e10/xyz")
.queryParam("include", "[\"id\",\"cInt\",\"fromRequest\",\"dynamicRelationship\"]")
.queryParam("sort", "id")
.get()
.wasOk()
.bodyEquals(
2,
"{\"id\":2,\"cInt\":6,\"dynamicRelationship\":{\"id\":2,\"name\":\"b\",\"prop1\":null,\"prop2\":null},\"fromRequest\":\"xyz\"}," +
"{\"id\":4,\"cInt\":9,\"dynamicRelationship\":null,\"fromRequest\":\"xyz\"}");
}

@Test
public void test_OverlaidRelationship() {

Expand Down Expand Up @@ -180,10 +198,30 @@ private static <T extends Persistent> T findMatching(Class<T> type, Persistent p
return SelectById.query(type, Cayenne.pkForObject(p)).selectOne(p.getObjectContext());
}

@GET
@Path("e10/{suffix}")
// request constraints mixed with entity constraints
public DataResponse<E10> getE10(@Context UriInfo uriInfo, @PathParam("suffix") String suffix) {

AgEntityOverlay<E10> overlay = AgEntity.overlay(E10.class)
// 1. Request-specific attribute
.redefineAttribute("fromRequest", String.class, e4 -> suffix)
// 3. Changing output of the existing property
.redefineAttribute("cInt", Integer.class, e10 -> e10.getCInt() + 1)
// 4. Dynamic relationship
.redefineToOne("dynamicRelationship", E22.class, e10 -> findMatching(E22.class, e10));

return Ag.service(config)
.select(E10.class)
.entityOverlay(overlay)
.uri(uriInfo)
.get();
}

@GET
@Path("e4/{suffix}")
// typical use case tested here is an AgEntityOverlay that depends on request parameters
public DataResponse<E4> get(@Context UriInfo uriInfo, @PathParam("suffix") String suffix) {
public DataResponse<E4> getE4(@Context UriInfo uriInfo, @PathParam("suffix") String suffix) {

AgEntityOverlay<E4> overlay = AgEntity.overlay(E4.class)
// 1. Request-specific attribute
Expand Down

0 comments on commit a5d38ec

Please sign in to comment.