Skip to content

Commit 192764d

Browse files
committed
GH-2333 - Fix custom media type registration in HalConfiguration.
With the addition of the vnd.hal+json media type, our registration of custom media types ordered these behind the legacy hal+json media type. We now try to find the first of the default media types and insert custom ones right before that one.
1 parent 68b3c19 commit 192764d

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
public class HalConfiguration {
4141

4242
private static final PathMatcher MATCHER = new AntPathMatcher();
43+
private static final List<MediaType> DEFAULT_MEDIA_TYPES = List.of(MediaTypes.HAL_JSON, MediaTypes.VND_HAL_JSON);
4344

4445
/**
4546
* Configures how to render links in case there is exactly one defined for a given link relation in general. By
@@ -67,8 +68,7 @@ public class HalConfiguration {
6768
*/
6869
public HalConfiguration() {
6970

70-
this(RenderSingleLinks.AS_SINGLE, new LinkedHashMap<>(), true, true, __ -> {},
71-
List.of(MediaTypes.HAL_JSON, MediaTypes.VND_HAL_JSON));
71+
this(RenderSingleLinks.AS_SINGLE, new LinkedHashMap<>(), true, true, __ -> {}, DEFAULT_MEDIA_TYPES);
7272
}
7373

7474
private HalConfiguration(RenderSingleLinks renderSingleLinks, Map<String, RenderSingleLinks> singleLinksPerPattern,
@@ -233,8 +233,10 @@ public HalConfiguration withMediaType(MediaType mediaType) {
233233
return this;
234234
}
235235

236+
var index = mediaTypes.indexOf(DEFAULT_MEDIA_TYPES.get(0));
237+
236238
List<MediaType> newMediaTypes = new ArrayList<>(mediaTypes);
237-
newMediaTypes.add(mediaTypes.size() - 1, mediaType);
239+
newMediaTypes.add(index, mediaType);
238240

239241
return new HalConfiguration(renderSingleLinks, singleLinksPerPattern, applyPropertyNamingStrategy,
240242
enforceEmbeddedCollections, objectMapperCustomizer, newMediaTypes);

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
import org.junit.jupiter.api.Test;
2121
import org.springframework.hateoas.LinkRelation;
22+
import org.springframework.hateoas.MediaTypes;
2223
import org.springframework.hateoas.mediatype.hal.HalConfiguration.RenderSingleLinks;
24+
import org.springframework.http.MediaType;
2325

2426
/**
2527
* Unit tests for {@link HalConfiguration}.
@@ -65,4 +67,18 @@ void registersWildcardedArrayLinksPatternForUri() {
6567
assertThat(configuration.getSingleLinkRenderModeFor(LinkRelation.of("https://somehost/bar")))
6668
.isEqualTo(RenderSingleLinks.AS_SINGLE);
6769
}
70+
71+
@Test // GH-2333
72+
void registersCustomMediaTypesFirst() {
73+
74+
var config = new HalConfiguration()
75+
.withMediaType(MediaType.APPLICATION_CBOR)
76+
.withMediaType(MediaType.APPLICATION_PDF);
77+
78+
assertThat(config.getMediaTypes()).containsExactly(
79+
MediaType.APPLICATION_CBOR,
80+
MediaType.APPLICATION_PDF,
81+
MediaTypes.HAL_JSON,
82+
MediaTypes.VND_HAL_JSON);
83+
}
6884
}

0 commit comments

Comments
 (0)