From 46d9acaa8aecab85e058099975a18fff6076be09 Mon Sep 17 00:00:00 2001 From: Mikhail Dzianishchyts Date: Wed, 2 Nov 2022 13:50:27 +0300 Subject: [PATCH 1/2] fix invalid related id path #585 --- .../agrest/cayenne/path/EntityPathCache.java | 6 +++- .../cayenne/path/EntityPathCacheTest.java | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/agrest-cayenne/src/main/java/io/agrest/cayenne/path/EntityPathCache.java b/agrest-cayenne/src/main/java/io/agrest/cayenne/path/EntityPathCache.java index 5f27b2294..7cd81e3ce 100644 --- a/agrest-cayenne/src/main/java/io/agrest/cayenne/path/EntityPathCache.java +++ b/agrest-cayenne/src/main/java/io/agrest/cayenne/path/EntityPathCache.java @@ -49,7 +49,11 @@ private PathDescriptor computePathDescriptor(String agPath) { if (last instanceof AgIdPart) { AgIdPart id = (AgIdPart) last; - return new PathDescriptor(id.getType(), PathOps.parsePath(id.getName()), true); + if (entity.getIdParts().contains(id)) { + return new PathDescriptor(id.getType(), PathOps.parsePath(id.getName()), true); + } else { + return new PathDescriptor(id.getType(), PathOps.parsePath(agPath), true); + } } AgRelationship relationship = (AgRelationship) last; diff --git a/agrest-cayenne/src/test/java/io/agrest/cayenne/path/EntityPathCacheTest.java b/agrest-cayenne/src/test/java/io/agrest/cayenne/path/EntityPathCacheTest.java index f6bf34f70..f8e22b1ac 100644 --- a/agrest-cayenne/src/test/java/io/agrest/cayenne/path/EntityPathCacheTest.java +++ b/agrest-cayenne/src/test/java/io/agrest/cayenne/path/EntityPathCacheTest.java @@ -2,6 +2,7 @@ import io.agrest.AgException; import io.agrest.annotation.AgAttribute; +import io.agrest.annotation.AgId; import io.agrest.annotation.AgRelationship; import io.agrest.compiler.AgEntityCompiler; import io.agrest.compiler.AnnotationsAgEntityCompiler; @@ -36,6 +37,17 @@ public void testGetOrCreate_Attribute() { assertSame(pd, cache.getOrCreate("name")); } + @Test + public void testGetOrCreate_Id() { + EntityPathCache cache = new EntityPathCache(schema.getEntity(X.class)); + PathDescriptor pd = cache.getOrCreate("id"); + assertNotNull(pd); + assertTrue(pd.isAttributeOrId()); + assertEquals(Integer.class, pd.getType()); + assertEquals("id", pd.getPathExp().getPath()); + assertSame(pd, cache.getOrCreate("id")); + } + @Test public void testGetOrCreate_Relationship() { EntityPathCache cache = new EntityPathCache(schema.getEntity(X.class)); @@ -58,6 +70,17 @@ public void testGetOrCreate_RelatedAttribute() { assertSame(pd, cache.getOrCreate("y.name")); } + @Test + public void testGetOrCreate_RelatedId() { + EntityPathCache cache = new EntityPathCache(schema.getEntity(X.class)); + PathDescriptor pd = cache.getOrCreate("y.id"); + assertNotNull(pd); + assertTrue(pd.isAttributeOrId()); + assertEquals(Integer.class, pd.getType()); + assertEquals("y.id", pd.getPathExp().getPath()); + assertSame(pd, cache.getOrCreate("y.id")); + } + @Test public void tesGetOrCreate_BadPath() { EntityPathCache cache = new EntityPathCache(schema.getEntity(X.class)); @@ -78,6 +101,11 @@ public void testGetOrCreate_OuterRelatedAttribute() { public static class X { + @AgId + public Integer getId() { + throw new UnsupportedOperationException(); + } + @AgAttribute public String getName() { throw new UnsupportedOperationException(); @@ -94,5 +122,10 @@ public static class Y { public String getName() { throw new UnsupportedOperationException(); } + + @AgId + public Integer getId() { + throw new UnsupportedOperationException(); + } } } From 6af4335f58fbd2e7d3f36ba9cbe750a2122d85ee Mon Sep 17 00:00:00 2001 From: Mikhail Dzianishchyts Date: Sun, 4 Feb 2024 17:05:26 +0300 Subject: [PATCH 2/2] Adjust tests to current EntityPathCache behaviour --- .../cayenne/path/EntityPathCacheTest.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/agrest-cayenne/src/test/java/io/agrest/cayenne/path/EntityPathCacheTest.java b/agrest-cayenne/src/test/java/io/agrest/cayenne/path/EntityPathCacheTest.java index bd01ec526..b537da9ac 100644 --- a/agrest-cayenne/src/test/java/io/agrest/cayenne/path/EntityPathCacheTest.java +++ b/agrest-cayenne/src/test/java/io/agrest/cayenne/path/EntityPathCacheTest.java @@ -66,11 +66,22 @@ public void getOrCreate_Id() { PathDescriptor pd = cache.getOrCreate("id"); assertNotNull(pd); assertTrue(pd.isAttributeOrId()); - assertEquals("java.lang.Integer", pd.getType()); - assertEquals("id", pd.getPathExp().getPath()); + assertEquals("java.lang.String", pd.getType()); + assertEquals("pkx1", pd.getPathExp().getPath()); assertSame(pd, cache.getOrCreate("id")); } + @Test + public void getOrCreate_RelatedId() { + EntityPathCache cache = new EntityPathCache(x); + PathDescriptor pd = cache.getOrCreate("y.db:pk1"); + assertNotNull(pd); + assertTrue(pd.isAttributeOrId()); + assertEquals("java.lang.Integer", pd.getType()); + assertEquals("pk1", pd.getPathExp().getPath()); + assertSame(pd, cache.getOrCreate("y.db:pk1")); + } + @Test public void getOrCreate_Relationship() { EntityPathCache cache = new EntityPathCache(x); @@ -93,17 +104,6 @@ public void getOrCreate_RelatedAttribute() { assertSame(pd, cache.getOrCreate("y.name")); } - @Test - public void getOrCreate_RelatedId() { - EntityPathCache cache = new EntityPathCache(x); - PathDescriptor pd = cache.getOrCreate("y.id"); - assertNotNull(pd); - assertTrue(pd.isAttributeOrId()); - assertEquals("java.lang.Integer", pd.getType()); - assertEquals("y.id", pd.getPathExp().getPath()); - assertSame(pd, cache.getOrCreate("y.id")); - } - @Test public void getOrCreate_BadPath() { EntityPathCache cache = new EntityPathCache(x);