From 9b632f37737de6d3f957865866919d85cb83204b Mon Sep 17 00:00:00 2001 From: Greg Turnquist Date: Sat, 31 Aug 2019 08:43:08 -0500 Subject: [PATCH 1/2] #1060 - Properly handle an empty list of hypermedia types for EnableHypermediaSupport. --- ...HypermediaConfigurationImportSelector.java | 16 ++++++-- ...iaConfigurationImportSelectorUnitTest.java | 21 ++++++++++ ...CustomHypermediaConfigurationProvider.java | 38 +++++++++++++++++++ src/test/resources/META-INF/spring.factories | 2 + 4 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/springframework/hateoas/support/CustomHypermediaConfigurationProvider.java create mode 100644 src/test/resources/META-INF/spring.factories diff --git a/src/main/java/org/springframework/hateoas/config/HypermediaConfigurationImportSelector.java b/src/main/java/org/springframework/hateoas/config/HypermediaConfigurationImportSelector.java index eccb0a9a8..5b7d93964 100644 --- a/src/main/java/org/springframework/hateoas/config/HypermediaConfigurationImportSelector.java +++ b/src/main/java/org/springframework/hateoas/config/HypermediaConfigurationImportSelector.java @@ -44,7 +44,7 @@ public String[] selectImports(AnnotationMetadata metadata) { Map attributes = metadata.getAnnotationAttributes(EnableHypermediaSupport.class.getName()); - List types = attributes == null // + List mediaTypes = attributes == null // ? Collections.emptyList() // : Arrays.stream((HypermediaType[]) attributes.get("type")) // .flatMap(it -> it.getMediaTypes().stream()) // @@ -53,10 +53,18 @@ public String[] selectImports(AnnotationMetadata metadata) { List configurationProviders = SpringFactoriesLoader.loadFactories( MediaTypeConfigurationProvider.class, HypermediaConfigurationImportSelector.class.getClassLoader()); - // Filter the ones supporting the given media types return configurationProviders.stream() // - .filter(it -> it.supportsAny(types)) // - .map(MediaTypeConfigurationProvider::getConfiguration) // + .filter(it -> { + // If there are no types, then let them all through + if (mediaTypes.isEmpty()) { + return true; + } + // Filter the ones supporting the given media types + return it.supportsAny(mediaTypes); + }) // + .map((MediaTypeConfigurationProvider mediaTypeConfigurationProvider) -> { + return mediaTypeConfigurationProvider.getConfiguration(); + }) // .map(Class::getName) // .toArray(String[]::new); } diff --git a/src/test/java/org/springframework/hateoas/config/HypermediaConfigurationImportSelectorUnitTest.java b/src/test/java/org/springframework/hateoas/config/HypermediaConfigurationImportSelectorUnitTest.java index 08dbdd2af..a8711695c 100644 --- a/src/test/java/org/springframework/hateoas/config/HypermediaConfigurationImportSelectorUnitTest.java +++ b/src/test/java/org/springframework/hateoas/config/HypermediaConfigurationImportSelectorUnitTest.java @@ -89,6 +89,22 @@ void testAllImportConfigurations() { }); } + @Test // #1060 + void testEmptyHypermediaTypes() { + + withContext(NoConfig.class, context -> { + + Map linkDiscoverers = context.getBeansOfType(LinkDiscoverer.class); + + assertThat(linkDiscoverers.values()).extracting("class") // + .containsExactlyInAnyOrder( // + HalLinkDiscoverer.class, // + HalFormsLinkDiscoverer.class, // + UberLinkDiscoverer.class, // + CollectionJsonLinkDiscoverer.class); + }); + } + @EnableHypermediaSupport(type = HAL) static class HalConfig { @@ -108,4 +124,9 @@ static class HalAndHalFormsConfig { static class AllConfig { } + + @EnableHypermediaSupport(type = { }) + static class NoConfig { + + } } diff --git a/src/test/java/org/springframework/hateoas/support/CustomHypermediaConfigurationProvider.java b/src/test/java/org/springframework/hateoas/support/CustomHypermediaConfigurationProvider.java new file mode 100644 index 000000000..7e0295dcd --- /dev/null +++ b/src/test/java/org/springframework/hateoas/support/CustomHypermediaConfigurationProvider.java @@ -0,0 +1,38 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.hateoas.support; + +import java.util.Collection; + +import org.springframework.hateoas.config.HypermediaMappingInformation; +import org.springframework.hateoas.config.MediaTypeConfigurationProvider; +import org.springframework.http.MediaType; + +/** + * @author Greg Turnquist + */ +public class CustomHypermediaConfigurationProvider implements MediaTypeConfigurationProvider { + + @Override + public Class getConfiguration() { + return CustomHypermediaType.class; + } + + @Override + public boolean supportsAny(Collection mediaTypes) { + return mediaTypes.stream().anyMatch(mediaType -> mediaType.isCompatibleWith(CustomHypermediaType.FRODO_MEDIATYPE)); + } +} diff --git a/src/test/resources/META-INF/spring.factories b/src/test/resources/META-INF/spring.factories new file mode 100644 index 000000000..5bbbf9f27 --- /dev/null +++ b/src/test/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.hateoas.config.MediaTypeConfigurationProvider=\ + org.springframework.hateoas.support.CustomHypermediaConfigurationProvider From bc51cc7c5277baa8815c818d76b9507bb341e0f1 Mon Sep 17 00:00:00 2001 From: Greg Turnquist Date: Sat, 31 Aug 2019 18:55:07 -0500 Subject: [PATCH 2/2] #1060 - Configure EnableHypermediaSupport(types) as an empty list by default. --- .../hateoas/config/EnableHypermediaSupport.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/hateoas/config/EnableHypermediaSupport.java b/src/main/java/org/springframework/hateoas/config/EnableHypermediaSupport.java index 2c6779f80..8f67fe91e 100644 --- a/src/main/java/org/springframework/hateoas/config/EnableHypermediaSupport.java +++ b/src/main/java/org/springframework/hateoas/config/EnableHypermediaSupport.java @@ -44,11 +44,12 @@ public @interface EnableHypermediaSupport { /** - * The hypermedia type to be supported. + * The hypermedia type to be supported. By default, it is empty, thus activating all available + * {@link MediaTypeConfigurationProvider}s. * * @return */ - HypermediaType[] type(); + HypermediaType[] type() default {}; /** * Configures which {@link WebStack}s we're supposed to enable support for. By default we're activating it for all