From bf52312e4e45da9ccc306a99e695167567a5506a Mon Sep 17 00:00:00 2001 From: Michael Schout Date: Fri, 23 May 2025 13:32:48 -0500 Subject: [PATCH 1/5] 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 --- .../core/providers/HateoasHalProvider.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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..7ae1198e9 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(this::isHalEnabled) .orElse(true); } + private 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) { + return (boolean) result; + } + + throw new IllegalStateException("Method " + methodName + " did not return a boolean value"); + } + } + + throw new IllegalStateException("No suitable method found to determine if HAL is enabled"); + } } From 3a403a59b81fc839278ba6f44cd4d4a8b8010d94 Mon Sep 17 00:00:00 2001 From: Michael Schout Date: Tue, 3 Jun 2025 08:39:04 -0500 Subject: [PATCH 2/5] Make isHalEnabled static --- .../java/org/springdoc/core/providers/HateoasHalProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 7ae1198e9..42bff0704 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 @@ -86,7 +86,7 @@ public boolean isHalEnabled() { .orElse(true); } - private boolean isHalEnabled(@NonNull HateoasProperties hateoasProperties) { + 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"); From fc5ef23f23edb0d3fda5577c6f964646586336ea Mon Sep 17 00:00:00 2001 From: Michael Schout Date: Tue, 3 Jun 2025 08:39:28 -0500 Subject: [PATCH 3/5] Avoid type cast --- .../java/org/springdoc/core/providers/HateoasHalProvider.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 42bff0704..e02e49ae8 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 @@ -94,8 +94,8 @@ private static boolean isHalEnabled(@NonNull HateoasProperties hateoasProperties var method = ReflectionUtils.findMethod(HateoasProperties.class, methodName); if (method != null) { var result = ReflectionUtils.invokeMethod(method, hateoasProperties); - if (result instanceof Boolean) { - return (boolean) result; + if (result instanceof Boolean halEnabled) { + return halEnabled; } throw new IllegalStateException("Method " + methodName + " did not return a boolean value"); From ccf2d2da828ba5674d750b71335c2b5f158e67c0 Mon Sep 17 00:00:00 2001 From: Michael Schout Date: Tue, 3 Jun 2025 11:26:22 -0500 Subject: [PATCH 4/5] Update springdoc-openapi-starter-common/src/main/java/org/springdoc/core/providers/HateoasHalProvider.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Krzysztof Dębski --- .../java/org/springdoc/core/providers/HateoasHalProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e02e49ae8..789a8d075 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 @@ -82,7 +82,7 @@ protected void init() { */ public boolean isHalEnabled() { return hateoasPropertiesOptional - .map(this::isHalEnabled) + .map(HateoasHalProvider ::isHalEnabled) .orElse(true); } From 392aec6d56e449b48aaa53a38cac6b729f9294a1 Mon Sep 17 00:00:00 2001 From: Michael Schout Date: Tue, 3 Jun 2025 16:35:30 -0500 Subject: [PATCH 5/5] Fix formatting --- .../java/org/springdoc/core/providers/HateoasHalProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 789a8d075..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 @@ -82,7 +82,7 @@ protected void init() { */ public boolean isHalEnabled() { return hateoasPropertiesOptional - .map(HateoasHalProvider ::isHalEnabled) + .map(HateoasHalProvider::isHalEnabled) .orElse(true); }