Skip to content

Commit bf52312

Browse files
committed
Fixes for Spring Boot 3.5.0 API
Spring Boot renamed the property methods to determine if HAL is enabled or not. Use reflection to determine which method to call so we can support both versions Fixes #3005
1 parent 8439195 commit bf52312

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/providers/HateoasHalProvider.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@
2626

2727
package org.springdoc.core.providers;
2828

29+
import java.util.List;
2930
import java.util.Optional;
3031

3132
import jakarta.annotation.PostConstruct;
3233

3334
import org.springframework.boot.autoconfigure.hateoas.HateoasProperties;
3435
import org.springframework.hateoas.mediatype.hal.Jackson2HalModule;
36+
import org.springframework.lang.NonNull;
37+
import org.springframework.util.ReflectionUtils;
3538

3639
/**
3740
* The type Hateoas hal provider.
@@ -79,8 +82,26 @@ protected void init() {
7982
*/
8083
public boolean isHalEnabled() {
8184
return hateoasPropertiesOptional
82-
.map(HateoasProperties::getUseHalAsDefaultJsonMediaType)
85+
.map(this::isHalEnabled)
8386
.orElse(true);
8487
}
8588

89+
private boolean isHalEnabled(@NonNull HateoasProperties hateoasProperties) {
90+
// In spring-boot 3.5, the method name was changed from getUseHalAsDefaultJsonMediaType to isUseHalAsDefaultJsonMediaType
91+
var possibleMethodNames = List.of("isUseHalAsDefaultJsonMediaType", "getUseHalAsDefaultJsonMediaType");
92+
93+
for (var methodName : possibleMethodNames) {
94+
var method = ReflectionUtils.findMethod(HateoasProperties.class, methodName);
95+
if (method != null) {
96+
var result = ReflectionUtils.invokeMethod(method, hateoasProperties);
97+
if (result instanceof Boolean) {
98+
return (boolean) result;
99+
}
100+
101+
throw new IllegalStateException("Method " + methodName + " did not return a boolean value");
102+
}
103+
}
104+
105+
throw new IllegalStateException("No suitable method found to determine if HAL is enabled");
106+
}
86107
}

0 commit comments

Comments
 (0)