Skip to content

Commit 25f5d03

Browse files
committed
#1060 - When EnableHypermediaSupport has an empty list, enable all.
Default it to being empty. Existing code, `@EnableHypermediaSupport(types = HAL)` will still operate as expected. But simply using the annotation with no arguments will activate ALL types.
1 parent e4fc105 commit 25f5d03

File tree

5 files changed

+74
-12
lines changed

5 files changed

+74
-12
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@
4444
public @interface EnableHypermediaSupport {
4545

4646
/**
47-
* The hypermedia type to be supported.
47+
* The hypermedia type to be supported. By default, it is empty, thus activating all available
48+
* {@link MediaTypeConfigurationProvider}s.
4849
*
4950
* @return
5051
*/
51-
HypermediaType[] type();
52+
HypermediaType[] type() default {};
5253

5354
/**
5455
* Configures which {@link WebStack}s we're supposed to enable support for. By default we're activating it for all

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public String[] selectImports(AnnotationMetadata metadata) {
6262

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

65-
List<MediaType> types = attributes == null //
65+
List<MediaType> mediaTypes = attributes == null //
6666
? Collections.emptyList() //
6767
: Arrays.stream((HypermediaType[]) attributes.get("type")) //
6868
.flatMap(it -> it.getMediaTypes().stream()) //
@@ -71,9 +71,9 @@ public String[] selectImports(AnnotationMetadata metadata) {
7171
List<MediaTypeConfigurationProvider> configurationProviders = SpringFactoriesLoader.loadFactories(
7272
MediaTypeConfigurationProvider.class, HypermediaConfigurationImportSelector.class.getClassLoader());
7373

74-
// Filter the ones supporting the given media types
74+
// Filter the ones supporting the given media types, or let them all through if none declared.
7575
Stream<String> imports = configurationProviders.stream() //
76-
.filter(it -> it.supportsAny(types)) //
76+
.filter(it -> mediaTypes.isEmpty() || it.supportsAny(mediaTypes)) //
7777
.map(MediaTypeConfigurationProvider::getConfiguration) //
7878
.map(Class::getName);
7979

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

+28-7
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,32 @@ void includesWebTestHateoasConfigurationIfWebTestClientIsOnTheClasspath() {
106106
@Test // #1252
107107
void doesNotIncludeWebTestHateoasConfigurationIfWebTestClientIsNotOnTheClasspath() {
108108

109-
HidingClassLoader delegate = HidingClassLoader.hide(WebTestClientConfigurer.class);
110-
ShadowingClassLoader loader = new ShadowingClassLoader(delegate);
111-
loader.excludePackage("org.springframework");
109+
HidingClassLoader delegate = HidingClassLoader.hide(WebTestClientConfigurer.class);
110+
ShadowingClassLoader loader = new ShadowingClassLoader(delegate);
111+
loader.excludePackage("org.springframework");
112112

113-
withContext(HalConfig.class, context -> {
113+
withContext(HalConfig.class, context -> {
114+
115+
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
116+
.isThrownBy(() -> context.getBean(WebTestHateoasConfiguration.class));
117+
118+
}, loader);
119+
}
114120

115-
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
116-
.isThrownBy(() -> context.getBean(WebTestHateoasConfiguration.class));
121+
@Test // #1060
122+
void testEmptyHypermediaTypes() {
123+
124+
withContext(NoConfig.class, context -> {
125+
126+
Map<String, LinkDiscoverer> linkDiscoverers = context.getBeansOfType(LinkDiscoverer.class);
117127

118-
}, loader);
128+
assertThat(linkDiscoverers.values()).extracting("class") //
129+
.containsExactlyInAnyOrder( //
130+
HalLinkDiscoverer.class, //
131+
HalFormsLinkDiscoverer.class, //
132+
UberLinkDiscoverer.class, //
133+
CollectionJsonLinkDiscoverer.class);
134+
});
119135
}
120136

121137
@EnableHypermediaSupport(type = HAL)
@@ -137,4 +153,9 @@ static class HalAndHalFormsConfig {
137153
static class AllConfig {
138154

139155
}
156+
157+
@EnableHypermediaSupport(type = { })
158+
static class NoConfig {
159+
160+
}
140161
}
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)