Skip to content

Commit 47486f4

Browse files
committed
#118 - Always default to fallback ConversionService in WebMvcLinkBuilder.
1 parent 8e78cf2 commit 47486f4

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/main/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilderFactory.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,14 @@ public WebMvcLinkBuilder linkTo(Method method, Object... parameters) {
148148
return WebMvcLinkBuilder.linkTo(method, parameters);
149149
}
150150

151-
@SuppressWarnings("null")
152-
private Supplier<ConversionService> getConversionService() {
151+
private static Supplier<ConversionService> getConversionService() {
153152

154153
return () -> {
155154

156155
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
157156

158-
if (!ServletRequestAttributes.class.isInstance(attributes)) {
159-
return null;
157+
if (attributes == null || !ServletRequestAttributes.class.isInstance(attributes)) {
158+
return FALLBACK_CONVERSION_SERVICE;
160159
}
161160

162161
ServletContext servletContext = ((ServletRequestAttributes) attributes).getRequest().getServletContext();

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.web.bind.annotation.RequestMapping;
3939
import org.springframework.web.bind.annotation.RequestParam;
4040
import org.springframework.web.bind.annotation.RestController;
41+
import org.springframework.web.context.request.RequestContextHolder;
4142
import org.springframework.web.util.UriComponents;
4243
import org.springframework.web.util.UriComponentsBuilder;
4344

@@ -625,6 +626,14 @@ void parentInterfaceCanHoldSpringWebAnnotations() {
625626
assertThat(link.getHref()).endsWith("/api?view=short");
626627
}
627628

629+
@Test // #118
630+
void usesFallbackConversionServiceIfNoContextIsCurrentlyPresent() {
631+
632+
RequestContextHolder.setRequestAttributes(null);
633+
634+
linkTo(methodOn(ControllerWithHandlerMethodParameterThatNeedsConversion.class).method(41L)).withSelfRel();
635+
}
636+
628637
private static UriComponents toComponents(Link link) {
629638
return UriComponentsBuilder.fromUriString(link.expand().getHref()).build();
630639
}
@@ -752,4 +761,11 @@ public HttpEntity<?> root(String view) {
752761
return ResponseEntity.noContent().build();
753762
}
754763
}
764+
765+
// #???
766+
interface ControllerWithHandlerMethodParameterThatNeedsConversion {
767+
768+
@GetMapping("/{id}")
769+
HttpEntity<?> method(@PathVariable Long id);
770+
}
755771
}

0 commit comments

Comments
 (0)