Skip to content

Commit 05c82a3

Browse files
Brutus5000claude
andcommitted
Carry the related entity type through relationshipLink
Mirror navigateRelationship by taking the related entity class: relationshipLink(Class<R>, name) now returns ElideNavigatorOnRelationshipLink<R>, which exposes getDtoClass() so callers can derive the related type (e.g. to build the linkage body) instead of passing it separately. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 79b210e commit 05c82a3

4 files changed

Lines changed: 29 additions & 13 deletions

File tree

api/src/main/java/com/faforever/commons/api/elide/ElideNavigator.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,24 @@ public <R extends ElideEntity> ElideNavigatorSelector<R> navigateRelationship(@N
115115
}
116116

117117
@Override
118-
public ElideNavigatorOnRelationshipLink relationshipLink(@NotNull String name) {
118+
public <R extends ElideEntity> ElideNavigatorOnRelationshipLink<R> relationshipLink(@NotNull Class<R> entityClass,
119+
@NotNull String name) {
119120
if (!includes.isEmpty()) {
120121
throw new IllegalStateException("Cannot navigate to a relationship link with includes on parent");
121122
}
122123
log.trace("relationship link added: {}", name);
123124
String route = build() + "/relationships/" + name;
124-
return () -> route;
125+
return new ElideNavigatorOnRelationshipLink<>() {
126+
@Override
127+
public String build() {
128+
return route;
129+
}
130+
131+
@Override
132+
public Class<R> getDtoClass() {
133+
return entityClass;
134+
}
135+
};
125136
}
126137

127138
/**

api/src/main/java/com/faforever/commons/api/elide/ElideNavigatorOnId.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ public interface ElideNavigatorOnId<T extends ElideEntity> extends ElideEndpoint
1414
*
1515
* <p>This is a terminal operation: the returned navigator only allows {@link
1616
* ElideNavigatorOnRelationshipLink#build()}. Includes are not allowed on the parent.
17+
*
18+
* @param entityClass the type of the related resource(s) the linkage points at
1719
*/
18-
ElideNavigatorOnRelationshipLink relationshipLink(String name);
20+
<R extends ElideEntity> ElideNavigatorOnRelationshipLink<R> relationshipLink(Class<R> entityClass, String name);
1921

2022
}

api/src/main/java/com/faforever/commons/api/elide/ElideNavigatorOnRelationshipLink.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
/**
44
* Terminal navigator pointing at a JSON:API relationship endpoint
5-
* ({@code /data/{type}/{id}/relationships/{name}}). It only exposes {@link #build()} because a relationship
6-
* link addresses the linkage itself (read or modify via PATCH/POST/DELETE) and cannot be navigated further.
5+
* ({@code /data/{type}/{id}/relationships/{name}}). It only exposes {@link #build()} and the related entity
6+
* type, because a relationship link addresses the linkage itself (read or modify via PATCH/POST/DELETE) and
7+
* cannot be navigated further. {@code T} is the type of the related resource(s) the linkage points at.
78
*/
8-
@FunctionalInterface
9-
public interface ElideNavigatorOnRelationshipLink {
9+
public interface ElideNavigatorOnRelationshipLink<T extends ElideEntity> {
1010
String build();
11+
12+
Class<T> getDtoClass();
1113
}

api/src/test/java/com/faforever/commons/api/elide/ElideNavigatorTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ void testNavigateFromIdToId() {
8989

9090
@Test
9191
void testRelationshipLink() {
92-
assertThat(ElideNavigator.of(MapPool.class)
93-
.id("5")
94-
.relationshipLink("mapVersion")
95-
.build(), is("/data/mapPool/5/relationships/mapVersion"));
92+
ElideNavigatorOnRelationshipLink<MapVersion> navigator = ElideNavigator.of(MapPool.class)
93+
.id("5")
94+
.relationshipLink(MapVersion.class, "mapVersion");
95+
assertThat(navigator.build(), is("/data/mapPool/5/relationships/mapVersion"));
96+
assertThat(navigator.getDtoClass(), is(MapVersion.class));
9697
}
9798

9899
@Test
@@ -101,7 +102,7 @@ void testRelationshipLinkAfterNavigateRelationship() {
101102
.id("5")
102103
.navigateRelationship(MapVersion.class, "mapVersion")
103104
.id("1234")
104-
.relationshipLink("map")
105+
.relationshipLink(MapVersion.class, "map")
105106
.build(), is("/data/mapPool/5/mapVersion/1234/relationships/map"));
106107
}
107108

@@ -110,7 +111,7 @@ void testCannotRelationshipLinkAfterIncludes() {
110111
assertThrows(IllegalStateException.class, () -> ElideNavigator.of(MapPool.class)
111112
.id("5")
112113
.addInclude("mapVersion")
113-
.relationshipLink("mapVersion"));
114+
.relationshipLink(MapVersion.class, "mapVersion"));
114115
}
115116

116117
@Test

0 commit comments

Comments
 (0)