Skip to content

Commit 9144684

Browse files
odrotbohmwilkinsona
authored andcommitted
Support link extraction with official HAL and HAL-FORMS media types
Register the HalLinkExtractor for both the official HAL media type (application/vnd.hal+json) and the HAL-FORMS media type (application/prs.hal-forms+json) See gh-965
1 parent c74602f commit 9144684

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,19 @@
3030
* content type.
3131
*
3232
* @author Andy Wilkinson
33+
* @author Oliver Drotbohm
3334
*/
3435
class ContentTypeLinkExtractor implements LinkExtractor {
3536

3637
private Map<MediaType, LinkExtractor> linkExtractors = new HashMap<>();
3738

3839
ContentTypeLinkExtractor() {
3940
this.linkExtractors.put(MediaType.APPLICATION_JSON, new AtomLinkExtractor());
40-
this.linkExtractors.put(HalLinkExtractor.HAL_MEDIA_TYPE, new HalLinkExtractor());
41+
42+
LinkExtractor halLinkExtractor = new HalLinkExtractor();
43+
this.linkExtractors.put(HalLinkExtractor.HAL_MEDIA_TYPE, halLinkExtractor);
44+
this.linkExtractors.put(HalLinkExtractor.VND_HAL_MEDIA_TYPE, halLinkExtractor);
45+
this.linkExtractors.put(HalLinkExtractor.HAL_FORMS_MEDIA_TYPE, halLinkExtractor);
4146
}
4247

4348
ContentTypeLinkExtractor(Map<MediaType, LinkExtractor> linkExtractors) {

spring-restdocs-core/src/main/java/org/springframework/restdocs/hypermedia/HalLinkExtractor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
class HalLinkExtractor extends AbstractJsonLinkExtractor {
3535

3636
static final MediaType HAL_MEDIA_TYPE = new MediaType("application", "hal+json");
37+
static final MediaType VND_HAL_MEDIA_TYPE = new MediaType("application", "vnd.hal+json");
38+
static final MediaType HAL_FORMS_MEDIA_TYPE = new MediaType("application", "prs.hal-forms+json");
3739

3840
@Override
3941
public Map<String, List<Link>> extractLinks(Map<String, Object> json) {

spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractorTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.util.HashMap;
21+
import java.util.List;
2122
import java.util.Map;
2223

2324
import org.junit.jupiter.api.Test;
@@ -28,6 +29,7 @@
2829
import org.springframework.restdocs.operation.OperationResponse;
2930
import org.springframework.restdocs.operation.OperationResponseFactory;
3031

32+
import static org.assertj.core.api.Assertions.assertThat;
3133
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3234
import static org.mockito.Mockito.mock;
3335
import static org.mockito.Mockito.verify;
@@ -41,6 +43,8 @@ class ContentTypeLinkExtractorTests {
4143

4244
private final OperationResponseFactory responseFactory = new OperationResponseFactory();
4345

46+
private final String halBody = "{ \"_links\" : { \"someRel\" : { \"href\" : \"someHref\" }} }";
47+
4448
@Test
4549
void extractionFailsWithNullContentType() {
4650
assertThatIllegalStateException().isThrownBy(() -> new ContentTypeLinkExtractor()
@@ -71,4 +75,22 @@ void extractorCalledWithCompatibleContextType() throws IOException {
7175
verify(extractor).extractLinks(response);
7276
}
7377

78+
@Test
79+
public void extractsLinksFromVndHalMediaType() throws IOException {
80+
HttpHeaders httpHeaders = new HttpHeaders();
81+
httpHeaders.setContentType(MediaType.parseMediaType("application/vnd.hal+json"));
82+
OperationResponse response = this.responseFactory.create(HttpStatus.OK, httpHeaders, this.halBody.getBytes());
83+
Map<String, List<Link>> links = new ContentTypeLinkExtractor().extractLinks(response);
84+
assertThat(links).containsKey("someRel");
85+
}
86+
87+
@Test
88+
public void extractsLinksFromHalFormsMediaType() throws IOException {
89+
HttpHeaders httpHeaders = new HttpHeaders();
90+
httpHeaders.setContentType(MediaType.parseMediaType("application/prs.hal-forms+json"));
91+
OperationResponse response = this.responseFactory.create(HttpStatus.OK, httpHeaders, this.halBody.getBytes());
92+
Map<String, List<Link>> links = new ContentTypeLinkExtractor().extractLinks(response);
93+
assertThat(links).containsKey("someRel");
94+
}
95+
7496
}

0 commit comments

Comments
 (0)