@@ -409,10 +409,15 @@ public void write(@Nullable Object source, RedisData sink) {
409409if (collection instanceof List ) {
410410collectionTargetType = List .class ;
411411} else if (collection instanceof Set ) {
412- collectionTargetType = collection instanceof EnumSet ? EnumSet .class : Set .class ;
412+ if (collection instanceof EnumSet <?>) {
413+ collectionTargetType = EnumSet .class ;
414+ } else {
415+ collectionTargetType = Set .class ;
416+ }
413417}
418+
414419typeMapper .writeType (collectionTargetType , sink .getBucket ().getPath ());
415- writeCollection (sink .getKeyspace (), "" , collection , TypeInformation .of ( Object . class ) , sink );
420+ writeCollection (sink .getKeyspace (), "" , collection , TypeInformation .OBJECT , sink );
416421return ;
417422}
418423
@@ -829,7 +834,13 @@ private void writeToBucket(String path, @Nullable Object value, RedisData sink,
829834
830835boolean isArray = collectionType .isArray ();
831836Class <?> collectionTypeToUse = isArray ? ArrayList .class : collectionType ;
832- Collection <Object > target = CollectionFactory .createCollection (collectionTypeToUse , valueType , keys .size ());
837+ Class <?> valueTypeToUse = valueType ;
838+
839+ if (collectionTypeToUse == EnumSet .class ) {
840+ valueTypeToUse = findFirstElementType (bucket , keys , valueType );
841+ }
842+
843+ Collection <Object > target = CollectionFactory .createCollection (collectionTypeToUse , valueTypeToUse , keys .size ());
833844
834845for (String key : keys ) {
835846
@@ -977,6 +988,17 @@ private void writeMap(@Nullable String keyspace, String path, Class<?> mapValueT
977988return conversionService .convert (toBytes (mapKey ), targetType );
978989}
979990
991+ private Class <?> findFirstElementType (Bucket bucket , List <String > keys , Class <?> fallbackType ) {
992+
993+ Optional <String > firstElement = keys .stream ().filter (typeMapper ::isTypeKey )
994+ .map (it -> it .substring (0 , it .indexOf (']' ) + 1 )).findFirst ();
995+ if (firstElement .isEmpty ()) {
996+ return fallbackType ;
997+ }
998+
999+ return getTypeHint (firstElement .get (), bucket , fallbackType );
1000+ }
1001+
9801002private Class <?> getTypeHint (String path , Bucket bucket , Class <?> fallback ) {
9811003
9821004TypeInformation <?> typeInformation = typeMapper .readType (bucket .getPropertyPath (path ),
0 commit comments