#639 - Added support for Optional controller method parameters in ControllerLinkBuilder.

We now properly consider Optional controller method parameters and correctly map its values to URIs created and optionality of the parameter derived.
This commit is contained in:
Oliver Gierke
2017-10-05 15:25:57 +02:00
parent 3006c47616
commit ed7e430628
4 changed files with 119 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
@@ -149,10 +149,12 @@ class AnnotatedParametersParameterAccessor {
Assert.notNull(parameter, "MethodParameter must not be null!");
boolean isOptional = Java8Utils.isJava8Optional(parameter.getParameterType());
this.parameter = parameter;
this.value = value;
this.attribute = attribute;
this.parameterTypeDescriptor = TypeDescriptor.nested(parameter, 0);
this.parameterTypeDescriptor = TypeDescriptor.nested(parameter, isOptional ? 1 : 0);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
@@ -299,7 +299,7 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
RequestParam annotation = parameter.getParameterAnnotation(RequestParam.class);
if (parameter.getParameterType().getName().equals("java.lang.Optional")) {
if (Java8Utils.isJava8Optional(parameter.getParameterType())) {
return false;
}
@@ -318,11 +318,13 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
RequestParam annotation = parameter.getParameterAnnotation(RequestParam.class);
value = Java8Utils.unwrapJava8Optional(value);
if (value != null) {
return value;
}
if (!annotation.required()) {
if (!annotation.required() || Java8Utils.isJava8Optional(parameter.getParameterType())) {
return SKIP_VALUE;
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright 2017 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
*
* http://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.mvc;
import java.util.Optional;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* Utilities to reflectively work with JDK 8's {@link Optional} on JDK 6.
*
* @author Oliver Gierke
*/
final class Java8Utils {
private static boolean OPTIONAL_PRESENT = ClassUtils.isPresent("java.util.Optional",
OptionalValueAccessor.class.getClassLoader());
/**
* Returns whether the given type is the JDK 8's {@link Optional}.
*
* @param type must not be {@literal null}.
* @return
*/
static boolean isJava8Optional(Class<?> type) {
Assert.notNull(type, "Type must not be null!");
return OPTIONAL_PRESENT && OptionalValueAccessor.isOptional(type);
}
/**
* Returns whether the given source value is a JDK 8 {@link Optional}.
*
* @param source can be {@literal null}.
* @return
*/
static boolean isJava8Optional(Object source) {
return OPTIONAL_PRESENT && OptionalValueAccessor.isOptional(source);
}
/**
* Unwraps the value contained in a JDK 8 {@link Optional} if the value is one.
*
* @param source can be {@literal null}.
* @return
*/
static Object unwrapJava8Optional(Object source) {
return OPTIONAL_PRESENT && isJava8Optional(source) ? OptionalValueAccessor.unwrapOptional(source) : source;
}
private static class OptionalValueAccessor {
static boolean isOptional(Class<?> type) {
return Optional.class.isAssignableFrom(type);
}
static boolean isOptional(Object source) {
return Optional.class.isInstance(source);
}
static Object unwrapOptional(Object source) {
return ((Optional<?>) source).orElse(null);
}
}
}