Skip to content

Commit

Permalink
fix invalid related id path agrestio#585
Browse files Browse the repository at this point in the history
  • Loading branch information
m-dzianishchyts committed Nov 2, 2022
1 parent d03967f commit 77d4301
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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();
Expand All @@ -94,5 +122,10 @@ public static class Y {
public String getName() {
throw new UnsupportedOperationException();
}

@AgId
public Integer getId() {
throw new UnsupportedOperationException();
}
}
}

0 comments on commit 77d4301

Please sign in to comment.