Skip to content

Commit 14ce1ac

Browse files
committed
#1060 - Properly handle an empty list of hypermedia types for EnableHypermediaSupport.
1 parent e5669ee commit 14ce1ac

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

src/main/java/org/springframework/hateoas/config/HypermediaConfigurationImportSelector.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public String[] selectImports(AnnotationMetadata metadata) {
4444

4545
Map<String, Object> attributes = metadata.getAnnotationAttributes(EnableHypermediaSupport.class.getName());
4646

47-
List<MediaType> types = attributes == null //
47+
List<MediaType> mediaTypes = attributes == null //
4848
? Collections.emptyList() //
4949
: Arrays.stream((HypermediaType[]) attributes.get("type")) //
5050
.flatMap(it -> it.getMediaTypes().stream()) //
@@ -53,10 +53,18 @@ public String[] selectImports(AnnotationMetadata metadata) {
5353
List<MediaTypeConfigurationProvider> configurationProviders = SpringFactoriesLoader.loadFactories(
5454
MediaTypeConfigurationProvider.class, HypermediaConfigurationImportSelector.class.getClassLoader());
5555

56-
// Filter the ones supporting the given media types
5756
return configurationProviders.stream() //
58-
.filter(it -> it.supportsAny(types)) //
59-
.map(MediaTypeConfigurationProvider::getConfiguration) //
57+
.filter(it -> {
58+
// If there are no types, then let them all through
59+
if (mediaTypes.isEmpty()) {
60+
return true;
61+
}
62+
// Filter the ones supporting the given media types
63+
return it.supportsAny(mediaTypes);
64+
}) //
65+
.map((MediaTypeConfigurationProvider mediaTypeConfigurationProvider) -> {
66+
return mediaTypeConfigurationProvider.getConfiguration();
67+
}) //
6068
.map(Class::getName) //
6169
.toArray(String[]::new);
6270
}

src/test/java/org/springframework/hateoas/config/HypermediaConfigurationImportSelectorUnitTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ void testAllImportConfigurations() {
8989
});
9090
}
9191

92+
@Test // #1060
93+
void testEmptyHypermediaTypes() {
94+
95+
withContext(NoConfig.class, context -> {
96+
97+
Map<String, LinkDiscoverer> linkDiscoverers = context.getBeansOfType(LinkDiscoverer.class);
98+
99+
assertThat(linkDiscoverers.values()).extracting("class") //
100+
.containsExactlyInAnyOrder( //
101+
HalLinkDiscoverer.class, //
102+
HalFormsLinkDiscoverer.class, //
103+
UberLinkDiscoverer.class, //
104+
CollectionJsonLinkDiscoverer.class);
105+
});
106+
}
107+
92108
@EnableHypermediaSupport(type = HAL)
93109
static class HalConfig {
94110

@@ -108,4 +124,9 @@ static class HalAndHalFormsConfig {
108124
static class AllConfig {
109125

110126
}
127+
128+
@EnableHypermediaSupport(type = { })
129+
static class NoConfig {
130+
131+
}
111132
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.hateoas.support;
17+
18+
import java.util.Collection;
19+
20+
import org.springframework.hateoas.config.HypermediaMappingInformation;
21+
import org.springframework.hateoas.config.MediaTypeConfigurationProvider;
22+
import org.springframework.http.MediaType;
23+
24+
/**
25+
* @author Greg Turnquist
26+
*/
27+
public class CustomHypermediaConfigurationProvider implements MediaTypeConfigurationProvider {
28+
29+
@Override
30+
public Class<? extends HypermediaMappingInformation> getConfiguration() {
31+
return CustomHypermediaType.class;
32+
}
33+
34+
@Override
35+
public boolean supportsAny(Collection<MediaType> mediaTypes) {
36+
return mediaTypes.stream().anyMatch(mediaType -> mediaType.isCompatibleWith(CustomHypermediaType.FRODO_MEDIATYPE));
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.hateoas.config.MediaTypeConfigurationProvider=\
2+
org.springframework.hateoas.support.CustomHypermediaConfigurationProvider

0 commit comments

Comments
 (0)