diff --git a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/providers/HateoasHalProvider.java b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/providers/HateoasHalProvider.java index 820b111c5..1e57c0b9e 100644 --- a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/providers/HateoasHalProvider.java +++ b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/providers/HateoasHalProvider.java @@ -26,12 +26,15 @@ package org.springdoc.core.providers; +import java.util.List; import java.util.Optional; import jakarta.annotation.PostConstruct; import org.springframework.boot.autoconfigure.hateoas.HateoasProperties; import org.springframework.hateoas.mediatype.hal.Jackson2HalModule; +import org.springframework.lang.NonNull; +import org.springframework.util.ReflectionUtils; /** * The type Hateoas hal provider. @@ -79,8 +82,26 @@ protected void init() { */ public boolean isHalEnabled() { return hateoasPropertiesOptional - .map(HateoasProperties::getUseHalAsDefaultJsonMediaType) + .map(HateoasHalProvider::isHalEnabled) .orElse(true); } + private static boolean isHalEnabled(@NonNull HateoasProperties hateoasProperties) { + // In spring-boot 3.5, the method name was changed from getUseHalAsDefaultJsonMediaType to isUseHalAsDefaultJsonMediaType + var possibleMethodNames = List.of("isUseHalAsDefaultJsonMediaType", "getUseHalAsDefaultJsonMediaType"); + + for (var methodName : possibleMethodNames) { + var method = ReflectionUtils.findMethod(HateoasProperties.class, methodName); + if (method != null) { + var result = ReflectionUtils.invokeMethod(method, hateoasProperties); + if (result instanceof Boolean halEnabled) { + return halEnabled; + } + + throw new IllegalStateException("Method " + methodName + " did not return a boolean value"); + } + } + + throw new IllegalStateException("No suitable method found to determine if HAL is enabled"); + } }