2121
2222import io .r2dbc .spi .Parameters ;
2323import io .r2dbc .spi .R2dbcType ;
24+ import io .r2dbc .spi .Row ;
2425import io .r2dbc .spi .test .MockColumnMetadata ;
2526import io .r2dbc .spi .test .MockResult ;
2627import io .r2dbc .spi .test .MockRow ;
6263import org .springframework .data .relational .core .query .Query ;
6364import org .springframework .data .relational .core .query .Update ;
6465import org .springframework .data .relational .core .sql .SqlIdentifier ;
66+ import org .springframework .data .relational .domain .RowDocument ;
6567import org .springframework .lang .Nullable ;
6668import org .springframework .r2dbc .core .DatabaseClient ;
6769import org .springframework .r2dbc .core .Parameter ;
@@ -88,7 +90,8 @@ void before() {
8890 client = DatabaseClient .builder ().connectionFactory (recorder )
8991 .bindMarkers (PostgresDialect .INSTANCE .getBindMarkersFactory ()).build ();
9092
91- R2dbcCustomConversions conversions = R2dbcCustomConversions .of (PostgresDialect .INSTANCE , new MoneyConverter ());
93+ R2dbcCustomConversions conversions = R2dbcCustomConversions .of (PostgresDialect .INSTANCE , new MoneyConverter (),
94+ new RowConverter (), new RowDocumentConverter ());
9295
9396 entityTemplate = new R2dbcEntityTemplate (client , PostgresDialect .INSTANCE ,
9497 new MappingR2dbcConverter (new R2dbcMappingContext (), conversions ));
@@ -610,6 +613,42 @@ void shouldConsiderParameterConverter() {
610613 Parameter .from (Parameters .in (R2dbcType .VARCHAR , "$$$" )));
611614 }
612615
616+ @ Test // GH-1696
617+ void shouldConsiderRowConverter () {
618+
619+ MockRowMetadata metadata = MockRowMetadata .builder ()
620+ .columnMetadata (MockColumnMetadata .builder ().name ("foo" ).type (R2dbcType .INTEGER ).build ())
621+ .columnMetadata (MockColumnMetadata .builder ().name ("bar" ).type (R2dbcType .VARCHAR ).build ()).build ();
622+ MockResult result = MockResult .builder ().row (MockRow .builder ().identified ("foo" , Object .class , 42 )
623+ .identified ("bar" , String .class , "the-bar" ).metadata (metadata ).build ()).build ();
624+
625+ recorder .addStubbing (s -> s .startsWith ("SELECT" ), result );
626+
627+ entityTemplate .select (MyRowToEntityType .class ).all ().as (StepVerifier ::create ) //
628+ .assertNext (actual -> {
629+ assertThat (actual .foo ).isEqualTo (1 ); // converter-fixed value
630+ assertThat (actual .bar ).isEqualTo ("the-bar" ); // converted value
631+ }).verifyComplete ();
632+ }
633+
634+ @ Test // GH-1696
635+ void shouldConsiderRowDocumentConverter () {
636+
637+ MockRowMetadata metadata = MockRowMetadata .builder ()
638+ .columnMetadata (MockColumnMetadata .builder ().name ("foo" ).type (R2dbcType .INTEGER ).build ())
639+ .columnMetadata (MockColumnMetadata .builder ().name ("bar" ).type (R2dbcType .VARCHAR ).build ()).build ();
640+ MockResult result = MockResult .builder ().row (MockRow .builder ().identified ("foo" , Object .class , 42 )
641+ .identified ("bar" , Object .class , "the-bar" ).metadata (metadata ).build ()).build ();
642+
643+ recorder .addStubbing (s -> s .startsWith ("SELECT" ), result );
644+
645+ entityTemplate .select (MyRowDocumentToEntityType .class ).all ().as (StepVerifier ::create ) //
646+ .assertNext (actual -> {
647+ assertThat (actual .foo ).isEqualTo (1 ); // converter-fixed value
648+ assertThat (actual .bar ).isEqualTo ("the-bar" ); // converted value
649+ }).verifyComplete ();
650+ }
651+
613652 record WithoutId (String name ) {
614653 }
615654
@@ -825,4 +864,30 @@ public io.r2dbc.spi.Parameter convert(Money source) {
825864 }
826865
827866 }
867+
868+ record MyRowToEntityType (int foo , String bar ) {
869+
870+ }
871+
872+ static class RowConverter implements Converter <Row , MyRowToEntityType > {
873+
874+ @ Override
875+ public MyRowToEntityType convert (Row source ) {
876+ return new MyRowToEntityType (1 , source .get ("bar" , String .class ));
877+ }
878+
879+ }
880+
881+ record MyRowDocumentToEntityType (int foo , String bar ) {
882+
883+ }
884+
885+ static class RowDocumentConverter implements Converter <RowDocument , MyRowDocumentToEntityType > {
886+
887+ @ Override
888+ public MyRowDocumentToEntityType convert (RowDocument source ) {
889+ return new MyRowDocumentToEntityType (1 , (String ) source .get ("bar" ));
890+ }
891+
892+ }
828893}
0 commit comments