29
29
30
30
import org .springframework .core .convert .converter .Converter ;
31
31
import org .springframework .data .convert .WritingConverter ;
32
+ import org .springframework .data .r2dbc .convert .EnumWriteSupport ;
32
33
import org .springframework .data .r2dbc .dialect .PostgresDialect ;
33
34
import org .springframework .data .r2dbc .mapping .OutboundRow ;
34
35
import org .springframework .data .relational .core .sql .SqlIdentifier ;
@@ -126,38 +127,90 @@ void shouldApplyCustomConversionForNull() {
126
127
assertThat (value .getType ()).isEqualTo (String .class );
127
128
}
128
129
129
- @ Test // gh-252
130
- void shouldConvertSetOfEnumToString () {
130
+ @ Test // gh-252, gh-593
131
+ void shouldConvertCollectionOfEnumToString () {
131
132
132
- DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy (PostgresDialect .INSTANCE ,
133
- Collections .singletonList (MyObjectsToStringConverter .INSTANCE ));
133
+ DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy (PostgresDialect .INSTANCE );
134
134
135
135
WithEnumCollections withEnums = new WithEnumCollections ();
136
136
withEnums .enumSet = EnumSet .of (MyEnum .ONE , MyEnum .TWO );
137
+ withEnums .enumList = Arrays .asList (MyEnum .ONE , MyEnum .TWO );
138
+ withEnums .enumArray = new MyEnum [] { MyEnum .ONE , MyEnum .TWO };
137
139
138
140
OutboundRow outboundRow = strategy .getOutboundRow (withEnums );
139
141
140
142
assertThat (outboundRow ).containsKey (SqlIdentifier .unquoted ("enum_set" ));
143
+ assertThat (outboundRow .get (SqlIdentifier .unquoted ("enum_set" )).getValue ()).isEqualTo (new String [] { "ONE" , "TWO" });
144
+
145
+ assertThat (outboundRow ).containsKey (SqlIdentifier .unquoted ("enum_array" ));
146
+ assertThat (outboundRow .get (SqlIdentifier .unquoted ("enum_array" )).getValue ())
147
+ .isEqualTo (new String [] { "ONE" , "TWO" });
148
+
149
+ assertThat (outboundRow ).containsKey (SqlIdentifier .unquoted ("enum_list" ));
150
+ assertThat (outboundRow .get (SqlIdentifier .unquoted ("enum_list" )).getValue ())
151
+ .isEqualTo (new String [] { "ONE" , "TWO" });
152
+ }
153
+
154
+ @ Test // gh-593
155
+ void shouldCorrectlyWriteConvertedEnumNullValues () {
156
+
157
+ DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy (PostgresDialect .INSTANCE );
158
+
159
+ WithEnumCollections withEnums = new WithEnumCollections ();
160
+
161
+ OutboundRow outboundRow = strategy .getOutboundRow (withEnums );
162
+
163
+ assertThat (outboundRow ).containsKey (SqlIdentifier .unquoted ("enum_set" ));
164
+ assertThat (outboundRow .get (SqlIdentifier .unquoted ("enum_set" )).getType ()).isEqualTo (String [].class );
165
+
166
+ assertThat (outboundRow ).containsKey (SqlIdentifier .unquoted ("enum_array" ));
167
+ assertThat (outboundRow .get (SqlIdentifier .unquoted ("enum_array" )).getType ()).isEqualTo (String [].class );
141
168
142
- Parameter value = outboundRow . get (SqlIdentifier .unquoted ("enum_set " ));
143
- assertThat (value . getValue ( )).isEqualTo (new String [] { "ONE" , "TWO" } );
169
+ assertThat ( outboundRow ). containsKey (SqlIdentifier .unquoted ("enum_list " ));
170
+ assertThat (outboundRow . get ( SqlIdentifier . unquoted ( "enum_list" )).getType ()). isEqualTo (String []. class );
144
171
}
145
172
146
- @ Test // gh-252
147
- void shouldConvertArrayOfEnumToString () {
173
+ @ Test // gh-593
174
+ void shouldConvertCollectionOfEnumNatively () {
148
175
149
176
DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy (PostgresDialect .INSTANCE ,
150
- Collections .singletonList (MyObjectsToStringConverter . INSTANCE ));
177
+ Collections .singletonList (new MyEnumSupport () ));
151
178
152
179
WithEnumCollections withEnums = new WithEnumCollections ();
180
+ withEnums .enumSet = EnumSet .of (MyEnum .ONE , MyEnum .TWO );
181
+ withEnums .enumList = Arrays .asList (MyEnum .ONE , MyEnum .TWO );
153
182
withEnums .enumArray = new MyEnum [] { MyEnum .ONE , MyEnum .TWO };
154
183
155
184
OutboundRow outboundRow = strategy .getOutboundRow (withEnums );
156
185
186
+ assertThat (outboundRow ).containsKey (SqlIdentifier .unquoted ("enum_set" ));
187
+ assertThat (outboundRow .get (SqlIdentifier .unquoted ("enum_set" )).getValue ()).isInstanceOf (MyEnum [].class );
188
+
189
+ assertThat (outboundRow ).containsKey (SqlIdentifier .unquoted ("enum_array" ));
190
+ assertThat (outboundRow .get (SqlIdentifier .unquoted ("enum_array" )).getValue ()).isInstanceOf (MyEnum [].class );
191
+
192
+ assertThat (outboundRow ).containsKey (SqlIdentifier .unquoted ("enum_list" ));
193
+ assertThat (outboundRow .get (SqlIdentifier .unquoted ("enum_list" )).getValue ()).isInstanceOf (MyEnum [].class );
194
+ }
195
+
196
+ @ Test // gh-593
197
+ void shouldCorrectlyWriteNativeEnumNullValues () {
198
+
199
+ DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy (PostgresDialect .INSTANCE ,
200
+ Collections .singletonList (new MyEnumSupport ()));
201
+
202
+ WithEnumCollections withEnums = new WithEnumCollections ();
203
+
204
+ OutboundRow outboundRow = strategy .getOutboundRow (withEnums );
205
+
206
+ assertThat (outboundRow ).containsKey (SqlIdentifier .unquoted ("enum_set" ));
207
+ assertThat (outboundRow .get (SqlIdentifier .unquoted ("enum_set" )).getType ()).isEqualTo (MyEnum [].class );
208
+
157
209
assertThat (outboundRow ).containsKey (SqlIdentifier .unquoted ("enum_array" ));
210
+ assertThat (outboundRow .get (SqlIdentifier .unquoted ("enum_array" )).getType ()).isEqualTo (MyEnum [].class );
158
211
159
- Parameter value = outboundRow . get (SqlIdentifier .unquoted ("enum_array " ));
160
- assertThat (value . getValue ( )).isEqualTo (new String [] { "ONE" , "TWO" } );
212
+ assertThat ( outboundRow ). containsKey (SqlIdentifier .unquoted ("enum_list " ));
213
+ assertThat (outboundRow . get ( SqlIdentifier . unquoted ( "enum_list" )).getType ()). isEqualTo (MyEnum []. class );
161
214
}
162
215
163
216
@ RequiredArgsConstructor
@@ -182,6 +235,7 @@ static class WithEnumCollections {
182
235
183
236
MyEnum [] enumArray ;
184
237
Set <MyEnum > enumSet ;
238
+ List <MyEnum > enumList ;
185
239
}
186
240
187
241
static class WithConversion {
@@ -216,4 +270,6 @@ public String convert(List<MyObject> myObjects) {
216
270
return myObjects .toString ();
217
271
}
218
272
}
273
+
274
+ private static class MyEnumSupport extends EnumWriteSupport <MyEnum > {}
219
275
}
0 commit comments