|
26 | 26 |
|
27 | 27 | package org.springdoc.core.providers;
|
28 | 28 |
|
| 29 | +import java.util.List; |
29 | 30 | import java.util.Optional;
|
30 | 31 |
|
31 | 32 | import jakarta.annotation.PostConstruct;
|
32 | 33 |
|
33 | 34 | import org.springframework.boot.autoconfigure.hateoas.HateoasProperties;
|
34 | 35 | import org.springframework.hateoas.mediatype.hal.Jackson2HalModule;
|
| 36 | +import org.springframework.lang.NonNull; |
| 37 | +import org.springframework.util.ReflectionUtils; |
35 | 38 |
|
36 | 39 | /**
|
37 | 40 | * The type Hateoas hal provider.
|
@@ -79,8 +82,26 @@ protected void init() {
|
79 | 82 | */
|
80 | 83 | public boolean isHalEnabled() {
|
81 | 84 | return hateoasPropertiesOptional
|
82 |
| - .map(HateoasProperties::getUseHalAsDefaultJsonMediaType) |
| 85 | + .map(this::isHalEnabled) |
83 | 86 | .orElse(true);
|
84 | 87 | }
|
85 | 88 |
|
| 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 | + } |
86 | 107 | }
|
0 commit comments