4343import org .springframework .core .convert .converter .Converter ;
4444import org .springframework .core .serializer .support .SerializingConverter ;
4545import org .springframework .data .annotation .Id ;
46- import org .springframework .data .annotation .Transient ;
4746import org .springframework .data .convert .ReadingConverter ;
4847import org .springframework .data .convert .WritingConverter ;
4948import org .springframework .data .domain .Sort ;
6362import org .springframework .data .mongodb .core .query .Criteria ;
6463import org .springframework .data .mongodb .core .query .Query ;
6564import org .springframework .data .mongodb .core .query .Update ;
65+ import org .springframework .data .util .TypeInformation ;
6666import org .springframework .integration .history .MessageHistory ;
6767import org .springframework .integration .message .AdviceMessage ;
6868import org .springframework .integration .store .AbstractMessageGroupStore ;
@@ -495,9 +495,9 @@ private long getNextId() {
495495 Query query = Query .query (Criteria .where ("_id" ).is (SEQUENCE_NAME ));
496496 query .fields ().include (SEQUENCE );
497497 return ((Number ) this .template .findAndModify (query ,
498- new Update ().inc (SEQUENCE , 1L ),
499- FindAndModifyOptions .options ().returnNew (true ).upsert (true ),
500- Map .class , this .collectionName )
498+ new Update ().inc (SEQUENCE , 1L ),
499+ FindAndModifyOptions .options ().returnNew (true ).upsert (true ),
500+ Map .class , this .collectionName )
501501 .get (SEQUENCE )) // NOSONAR - never returns null
502502 .longValue ();
503503 }
@@ -589,9 +589,14 @@ public <S> S read(Class<S> clazz, Bson source) {
589589 if (!MessageWrapper .class .equals (clazz )) {
590590 return super .read (clazz , source );
591591 }
592+
593+ return (S ) readAsMessageWrapper (source );
594+ }
595+
596+ private MessageWrapper readAsMessageWrapper (Bson source ) {
592597 if (source != null ) {
593598 Map <String , Object > sourceMap = asMap (source );
594- Message <?> message = null ;
599+ Message <?> message ;
595600 Object messageType = sourceMap .get ("_messageType" );
596601 if (messageType == null ) {
597602 messageType = GenericMessage .class .getName ();
@@ -629,21 +634,30 @@ public <S> S read(Class<S> clazz, Bson source) {
629634 }
630635 wrapper .setCondition ((String ) sourceMap .get ("_condition" ));
631636
632- return ( S ) wrapper ;
637+ return wrapper ;
633638 }
634639 return null ;
635640 }
636641
642+ @ Override
643+ @ SuppressWarnings ({ UNCHECKED })
644+ protected <S > S read (TypeInformation <S > type , Bson source ) {
645+ if (!MessageWrapper .class .equals (type .getType ())) {
646+ return super .read (type , source );
647+ }
648+
649+ return (S ) readAsMessageWrapper (source );
650+ }
651+
637652 private Map <String , Object > normalizeHeaders (Map <String , Object > headers ) {
638653 Map <String , Object > normalizedHeaders = new HashMap <>();
639654 for (Entry <String , Object > entry : headers .entrySet ()) {
640655 String headerName = entry .getKey ();
641656 Object headerValue = entry .getValue ();
642- if (headerValue instanceof Bson ) {
643- Bson source = (Bson ) headerValue ;
657+ if (headerValue instanceof Bson source ) {
644658 Map <String , Object > document = asMap (source );
645659 try {
646- Class <?> typeClass = null ;
660+ Class <?> typeClass ;
647661 if (document .containsKey (CLASS )) {
648662 Object type = document .get (CLASS );
649663 typeClass = ClassUtils .forName (type .toString (), MongoDbMessageStore .this .classLoader );
@@ -670,8 +684,7 @@ else if (source instanceof BasicDBList) {
670684 private Object extractPayload (Bson source ) {
671685 Object payload = asMap (source ).get ("payload" );
672686
673- if (payload instanceof Bson ) {
674- Bson payloadObject = (Bson ) payload ;
687+ if (payload instanceof Bson payloadObject ) {
675688 Object payloadType = asMap (payloadObject ).get (CLASS );
676689 try {
677690 Class <?> payloadClass =
@@ -845,7 +858,7 @@ private static final class MessageWrapper {
845858
846859 private volatile Object _groupId ; // NOSONAR name
847860
848- @ Transient
861+ // @Transient
849862 private final Message <?> message ; // NOSONAR name
850863
851864 @ SuppressWarnings (UNUSED )
0 commit comments