Skip to content

Commit

Permalink
Back-off in ResultProcessor if the result object is an instance of th…
Browse files Browse the repository at this point in the history
…e target type.

We now do not attempt to convert the object if it is an instance of the declared target type.

Closes #2347.
  • Loading branch information
mp911de committed Apr 6, 2021
1 parent 7464325 commit d27ac79
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ public ChainingConverter and(final Converter<Object, Object> converter) {

return new ChainingConverter(targetType, source -> {

if (source == null || targetType.isInstance(source)) {
return source;
}

Object intermediate = ChainingConverter.this.convert(source);

return intermediate == null || targetType.isInstance(intermediate) ? intermediate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import static org.mockito.Mockito.*;

import io.reactivex.Flowable;
import lombok.Getter;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import rx.Observable;
import rx.Single;

Expand Down Expand Up @@ -308,6 +310,37 @@ void supportsFlowableProjections() throws Exception {
assertThat(content.get(0)).isInstanceOf(SampleProjection.class);
}

@Test // GH-2347
void findByListSkipsConversionIfTypeAlreadyMatches() throws Exception {

List<AbstractDto> result = getProcessor("findAllAbstractDtos")
.processResult(Collections.singletonList(new ConcreteDto("Walter", "White")));

assertThat(result.get(0)).isInstanceOf(ConcreteDto.class);
}

@Test // GH-2347
void streamBySkipsConversionIfTypeAlreadyMatches() throws Exception {

Stream<AbstractDto> result = getProcessor("streamAllAbstractDtos")
.processResult(Stream.of(new ConcreteDto("Walter", "White")));

assertThat(result.findFirst().get()).isInstanceOf(ConcreteDto.class);
}

@Test // GH-2347
void findFluxSkipsConversionIfTypeAlreadyMatches() throws Exception {

Flux<AbstractDto> result = getProcessor("findFluxOfAbstractDtos")
.processResult(Flux.just(new ConcreteDto("Walter", "White")));

result.as(StepVerifier::create) //
.consumeNextWith(actual -> {

assertThat(actual).isInstanceOf(ConcreteDto.class);
}).verifyComplete();
}

private static ResultProcessor getProcessor(String methodName, Class<?>... parameters) throws Exception {
return getQueryMethod(methodName, parameters).getResultProcessor();
}
Expand Down Expand Up @@ -356,6 +389,12 @@ interface SampleRepository extends Repository<Sample, Long> {
Observable<SampleProjection> findObservableProjection();

Flowable<SampleProjection> findFlowableProjection();

List<AbstractDto> findAllAbstractDtos();

Stream<AbstractDto> streamAllAbstractDtos();

Flux<AbstractDto> findFluxOfAbstractDtos();
}

static class Sample {
Expand All @@ -367,6 +406,23 @@ public Sample(String firstname, String lastname) {
}
}

@Getter
static abstract class AbstractDto {
final String firstname, lastname;

public AbstractDto(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
}

static class ConcreteDto extends AbstractDto {

public ConcreteDto(String firstname, String lastname) {
super(firstname, lastname);
}
}

static class SampleDto {}

@lombok.Value
Expand Down

0 comments on commit d27ac79

Please sign in to comment.