Skip to content

Commit 8110282

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. Related issues: #1015. Related pull request: #1065, #1265.
1 parent 023e999 commit 8110282

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -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 -> types.isEmpty() || it.supportsAny(types)) //
7777
.map(MediaTypeConfigurationProvider::getConfiguration) //
7878
.map(Class::getName);
7979

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

+21
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,22 @@ void doesNotIncludeWebTestHateoasConfigurationIfWebTestClientIsNotOnTheClasspath
118118
}, loader);
119119
}
120120

121+
@Test // #1060
122+
void testEmptyHypermediaTypes() {
123+
124+
withContext(NoConfig.class, context -> {
125+
126+
Map<String, LinkDiscoverer> linkDiscoverers = context.getBeansOfType(LinkDiscoverer.class);
127+
128+
assertThat(linkDiscoverers.values()).extracting("class") //
129+
.containsExactlyInAnyOrder( //
130+
HalLinkDiscoverer.class, //
131+
HalFormsLinkDiscoverer.class, //
132+
UberLinkDiscoverer.class, //
133+
CollectionJsonLinkDiscoverer.class);
134+
});
135+
}
136+
121137
@EnableHypermediaSupport(type = HAL)
122138
static class HalConfig {
123139

@@ -137,4 +153,9 @@ static class HalAndHalFormsConfig {
137153
static class AllConfig {
138154

139155
}
156+
157+
@EnableHypermediaSupport
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)