Gracefully skip non-assignable reactive lambda callbacks on Java 18+.

Closes #2808, #2809.
This commit is contained in:
Michael J. Simons
2023-04-01 00:15:11 +02:00
committed by Oliver Drotbohm
parent e6c3243155
commit ba2e0f917c
2 changed files with 25 additions and 1 deletions

View File

@@ -37,6 +37,7 @@ import org.springframework.util.ReflectionUtils;
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Michael J. Simons
*/
class DefaultReactiveEntityCallbacks implements ReactiveEntityCallbacks {
@@ -112,7 +113,7 @@ class DefaultReactiveEntityCallbacks implements ReactiveEntityCallbacks {
throw new IllegalArgumentException(
String.format("Callback invocation on %s returned null value for %s", callback.getClass(), entity));
} catch (ClassCastException ex) {
} catch (IllegalArgumentException | ClassCastException ex) {
String msg = ex.getMessage();
if (msg == null || EntityCallbackInvoker.matchesClassCastMessage(msg, entity.getClass())) {

View File

@@ -17,6 +17,9 @@ package org.springframework.data.mapping.callback;
import static org.assertj.core.api.Assertions.*;
import java.util.ArrayList;
import java.util.List;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@@ -26,6 +29,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mapping.Person;
import org.springframework.data.mapping.PersonDocument;
import org.springframework.data.mapping.PersonNoId;
import org.springframework.data.mapping.callback.CapturingEntityCallback.FirstCallback;
import org.springframework.data.mapping.callback.CapturingEntityCallback.SecondCallback;
import org.springframework.data.mapping.callback.CapturingEntityCallback.ThirdCallback;
@@ -35,6 +39,7 @@ import org.springframework.data.mapping.callback.CapturingEntityCallback.ThirdCa
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Michael J. Simons
*/
class DefaultReactiveEntityCallbacksUnitTests {
@@ -124,6 +129,24 @@ class DefaultReactiveEntityCallbacksUnitTests {
assertThat(third.capturedValues()).isEmpty();
}
@Test // GH-2808
void skipsInvocationUsingJava18ReflectiveTypeRejection() {
DefaultReactiveEntityCallbacks callbacks = new DefaultReactiveEntityCallbacks();
callbacks.addEntityCallback(new DefaultEntityCallbacksUnitTests.Java18ClassCastStyle());
Person person = new PersonNoId(42, "Walter", "White");
List<Person> capturedPerson = new ArrayList<>();
callbacks.callback(DefaultEntityCallbacksUnitTests.BeforeConvertCallback.class, person) //
.as(StepVerifier::create)
.recordWith(() -> capturedPerson)
.expectNextCount(1)
.verifyComplete();
assertThat(capturedPerson.get(0)).isSameAs(person);
}
@Configuration
static class MyConfig {