Skip to content

Commit b88aad6

Browse files
committed
Avoid payload conversion if unnecessary
Closes gh-22386
1 parent baa7e2e commit b88aad6

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -221,6 +221,9 @@ protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @
221221
return this.objectMapper.readValue((byte[]) payload, javaType);
222222
}
223223
}
224+
else if (targetClass.isInstance(payload)) {
225+
return payload;
226+
}
224227
else {
225228
if (view != null) {
226229
return this.objectMapper.readerWithView(view).forType(javaType).readValue(payload.toString());

spring-messaging/src/test/java/org/springframework/messaging/converter/MappingJackson2MessageConverterTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -110,6 +110,14 @@ public void fromMessageUntyped() {
110110
assertEquals("AQI=", actual.get("bytes"));
111111
}
112112

113+
@Test // gh-22386
114+
public void fromMessageMatchingInstance() {
115+
MyBean myBean = new MyBean();
116+
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
117+
Message<?> message = MessageBuilder.withPayload(myBean).build();
118+
assertSame(myBean, converter.fromMessage(message, MyBean.class));
119+
}
120+
113121
@Test(expected = MessageConversionException.class)
114122
public void fromMessageInvalidJson() {
115123
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();

0 commit comments

Comments
 (0)