Skip to content

Commit 0697393

Browse files
committed
GH-2385 - Allow detecting links in embedded documents through HalLinkDiscoverer.
1 parent 11bc4ec commit 0697393

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/main/java/org/springframework/hateoas/mediatype/hal/HalLinkDiscoverer.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.hateoas.LinkRelation;
2222
import org.springframework.hateoas.MediaTypes;
2323
import org.springframework.hateoas.client.JsonPathLinkDiscoverer;
24+
import org.springframework.hateoas.client.LinkDiscoverer;
2425
import org.springframework.http.MediaType;
2526
import org.springframework.util.Assert;
2627

@@ -32,6 +33,10 @@
3233
*/
3334
public class HalLinkDiscoverer extends JsonPathLinkDiscoverer {
3435

36+
private static final String PATH = "_links..['%s']";
37+
private static final String JSON_PATH = "$." + PATH;
38+
private static final String RECURSIVE_JSON_PATH = "$.." + PATH;
39+
3540
/**
3641
* Constructor for {@link MediaTypes#HAL_JSON}.
3742
*/
@@ -40,7 +45,25 @@ public HalLinkDiscoverer() {
4045
}
4146

4247
protected HalLinkDiscoverer(MediaType... mediaTypes) {
43-
super("$._links..['%s']", mediaTypes);
48+
super(JSON_PATH, mediaTypes);
49+
}
50+
51+
/**
52+
* Creates a new {@link LinkDiscoverer} that looks up HAL links in the document recursively. In other words, it also
53+
* finds ones contained in the {@code _embedded} clause.
54+
*
55+
* @return will never be {@literal null}.
56+
* @since 3.0
57+
*/
58+
public LinkDiscoverer inspectEmbeddeds() {
59+
60+
return new JsonPathLinkDiscoverer(RECURSIVE_JSON_PATH, mediaTypes.toArray(MediaType[]::new)) {
61+
62+
@Override
63+
protected Link extractLink(Object element, LinkRelation rel) {
64+
return HalLinkDiscoverer.this.extractLink(element, rel);
65+
}
66+
};
4467
}
4568

4669
/*

src/test/java/org/springframework/hateoas/mediatype/hal/HalLinkDiscovererUnitTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.springframework.hateoas.Link;
2525
import org.springframework.hateoas.MappingTestUtils;
2626
import org.springframework.hateoas.MappingTestUtils.ContextualMapper;
27-
import org.springframework.hateoas.client.LinkDiscoverer;
2827
import org.springframework.hateoas.client.LinkDiscovererUnitTest;
2928

3029
/**
@@ -35,7 +34,7 @@
3534
*/
3635
class HalLinkDiscovererUnitTest extends LinkDiscovererUnitTest {
3736

38-
LinkDiscoverer discoverer = new HalLinkDiscoverer();
37+
HalLinkDiscoverer discoverer = new HalLinkDiscoverer();
3938
ContextualMapper $ = MappingTestUtils.createMapper();
4039

4140
/**
@@ -69,11 +68,24 @@ void discoversAllTheLinkAttributes() throws IOException {
6968

7069
assertThat(getDiscoverer().findLinkWithRel(IanaLinkRelations.SELF, linkText)).hasValue(expected);
7170
});
71+
}
72+
73+
@Test // GH-2385
74+
void detectsEmbeddedLinks() {
75+
76+
var discoverer = getDiscoverer().inspectEmbeddeds();
7277

78+
$.assertFileContent("hal-link-discoverer.json").satisfies(it -> {
79+
80+
assertThat(discoverer.findLinksWithRel("relation", it))
81+
.hasSize(3)
82+
.extracting(Link::getHref)
83+
.containsExactlyInAnyOrder("firstHref", "secondHref", "thirdHref");
84+
});
7385
}
7486

7587
@Override
76-
protected LinkDiscoverer getDiscoverer() {
88+
protected HalLinkDiscoverer getDiscoverer() {
7789
return discoverer;
7890
}
7991

src/test/resources/org/springframework/hateoas/mediatype/hal/hal-link-discoverer.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,14 @@
1414
"http://www.foo.com/bar": {
1515
"href": "fullRelHref"
1616
}
17+
},
18+
"_embedded": {
19+
"relation": {
20+
"_links": {
21+
"relation" : {
22+
"href": "thirdHref"
23+
}
24+
}
25+
}
1726
}
18-
}
27+
}

0 commit comments

Comments
 (0)