Skip to content

Commit 628148a

Browse files
committed
#1003 - BoundMethodParameter now only uses first level nesting for JDK 8 optional.
1 parent 3b65aea commit 628148a

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/main/java/org/springframework/hateoas/server/core/WebHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Iterator;
3333
import java.util.List;
3434
import java.util.Map;
35+
import java.util.Optional;
3536
import java.util.function.BiFunction;
3637
import java.util.function.Function;
3738

@@ -370,10 +371,12 @@ public BoundMethodParameter(MethodParameter parameter, @Nullable Object value, A
370371

371372
Assert.notNull(parameter, "MethodParameter must not be null!");
372373

374+
boolean isOptionalWrapper = Optional.class.isAssignableFrom(parameter.getParameterType());
375+
373376
this.parameter = parameter;
374377
this.value = value;
375378
this.attribute = attribute;
376-
this.parameterTypeDescriptor = TypeDescriptor.nested(parameter, parameter.isOptional() ? 1 : 0);
379+
this.parameterTypeDescriptor = TypeDescriptor.nested(parameter, isOptionalWrapper ? 1 : 0);
377380
}
378381

379382
/**

src/test/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilderUnitTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,8 @@ void addsOptionalRequestParameterTemplateForMissingValue() {
521521

522522
Link link = linkTo(methodOn(ControllerWithMethods.class).methodForNextPage("1", null, 5)).withSelfRel();
523523

524-
assertThat(link.getVariables()).containsExactly(new TemplateVariable("offset", VariableType.REQUEST_PARAM_CONTINUED));
524+
assertThat(link.getVariables())
525+
.containsExactly(new TemplateVariable("offset", VariableType.REQUEST_PARAM_CONTINUED));
525526

526527
UriComponents components = toComponents(link);
527528

@@ -601,6 +602,18 @@ void alternativePathVariableParameter() {
601602
assertThat(link.getHref()).isEqualTo("http://localhost/something/bar/foo");
602603
}
603604

605+
/**
606+
* @see #1003, #122, #169
607+
*/
608+
@Test
609+
void appendsOptionalParameterIfSet() {
610+
611+
Link link = linkTo(methodOn(ControllerWithMethods.class).methodForOptionalNextPage(1)).withSelfRel();
612+
613+
assertThat(link.getVariables()).isEmpty();
614+
assertThat(link.expand().getHref()).endsWith("/foo?offset=1");
615+
}
616+
604617
private static UriComponents toComponents(Link link) {
605618
return UriComponentsBuilder.fromUriString(link.expand().getHref()).build();
606619
}

0 commit comments

Comments
 (0)