Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Michael J. Simons
*/
class DefaultReactiveEntityCallbacks implements ReactiveEntityCallbacks {

Expand Down Expand Up @@ -112,7 +113,7 @@ public <T> Mono<T> invokeCallback(EntityCallback<T> callback, T entity,

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())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import static org.assertj.core.api.Assertions.*;

import java.util.ArrayList;
import java.util.List;

import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

Expand All @@ -26,6 +29,7 @@
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;
Expand All @@ -35,6 +39,7 @@
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Michael J. Simons
*/
class DefaultReactiveEntityCallbacksUnitTests {

Expand Down Expand Up @@ -124,6 +129,24 @@ void errorsOnNullValueReturnedByCallbackEntity() {
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 {

Expand Down