Skip to content

Commit 0ff80df

Browse files
committed
Merge pull request #965 from odrotbohm
* gh-965: Polish "Support link extraction with official HAL and HAL-FORMS media types" Support link extraction with official HAL and HAL-FORMS media types Closes gh-965
2 parents c74602f + cd16feb commit 0ff80df

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,14 +30,18 @@
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+
LinkExtractor halLinkExtractor = new HalLinkExtractor();
42+
this.linkExtractors.put(HalLinkExtractor.HAL_MEDIA_TYPE, halLinkExtractor);
43+
this.linkExtractors.put(HalLinkExtractor.VND_HAL_MEDIA_TYPE, halLinkExtractor);
44+
this.linkExtractors.put(HalLinkExtractor.HAL_FORMS_MEDIA_TYPE, halLinkExtractor);
4145
}
4246

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

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,11 +30,16 @@
3030
* format.
3131
*
3232
* @author Andy Wilkinson
33+
* @author Oliver Drotbohm
3334
*/
3435
class HalLinkExtractor extends AbstractJsonLinkExtractor {
3536

3637
static final MediaType HAL_MEDIA_TYPE = new MediaType("application", "hal+json");
3738

39+
static final MediaType VND_HAL_MEDIA_TYPE = new MediaType("application", "vnd.hal+json");
40+
41+
static final MediaType HAL_FORMS_MEDIA_TYPE = new MediaType("application", "prs.hal-forms+json");
42+
3843
@Override
3944
public Map<String, List<Link>> extractLinks(Map<String, Object> json) {
4045
Map<String, List<Link>> extractedLinks = new LinkedHashMap<>();

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+
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+
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)