Skip to content

Commit 11f6c4e

Browse files
committed
Matching update for copyHeadersIfAbsent
The change to copyHeaders from d46091 also applied to copyHeadersIfPresent. Closes gh-26155
1 parent d460915 commit 11f6c4e

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,21 @@ public void copyHeaders(@Nullable Map<String, ?> headersToCopy) {
129129
super.copyHeaders(headersToCopy);
130130
}
131131

132+
@Override
133+
public void copyHeadersIfAbsent(@Nullable Map<String, ?> headersToCopy) {
134+
if (headersToCopy == null) {
135+
return;
136+
}
137+
138+
@SuppressWarnings("unchecked")
139+
Map<String, List<String>> map = (Map<String, List<String>>) headersToCopy.get(NATIVE_HEADERS);
140+
if (map != null && getNativeHeaders() == null) {
141+
map.forEach(this::setNativeHeaderValues);
142+
}
143+
144+
super.copyHeadersIfAbsent(headersToCopy);
145+
}
146+
132147
/**
133148
* Whether the native header map contains the give header name.
134149
* @param headerName the name of the header

spring-messaging/src/test/java/org/springframework/messaging/support/NativeMessageHeaderAccessorTests.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,33 @@ public void setImmutableIdempotent() {
226226

227227
@Test // gh-25821
228228
void copyImmutableToMutable() {
229-
NativeMessageHeaderAccessor source = new NativeMessageHeaderAccessor();
230-
source.addNativeHeader("foo", "bar");
231-
Message<String> message = MessageBuilder.createMessage("payload", source.getMessageHeaders());
229+
NativeMessageHeaderAccessor sourceAccessor = new NativeMessageHeaderAccessor();
230+
sourceAccessor.addNativeHeader("foo", "bar");
231+
Message<String> source = MessageBuilder.createMessage("payload", sourceAccessor.getMessageHeaders());
232232

233-
NativeMessageHeaderAccessor target = new NativeMessageHeaderAccessor();
234-
target.copyHeaders(message.getHeaders());
235-
target.setLeaveMutable(true);
236-
message = MessageBuilder.createMessage(message.getPayload(), target.getMessageHeaders());
233+
NativeMessageHeaderAccessor targetAccessor = new NativeMessageHeaderAccessor();
234+
targetAccessor.copyHeaders(source.getHeaders());
235+
targetAccessor.setLeaveMutable(true);
236+
Message<?> target = MessageBuilder.createMessage(source.getPayload(), targetAccessor.getMessageHeaders());
237237

238-
MessageHeaderAccessor accessor = MessageHeaderAccessor.getMutableAccessor(message);
238+
MessageHeaderAccessor accessor = MessageHeaderAccessor.getMutableAccessor(target);
239+
assertThat(accessor.isMutable());
240+
((NativeMessageHeaderAccessor) accessor).addNativeHeader("foo", "baz");
241+
assertThat(((NativeMessageHeaderAccessor) accessor).getNativeHeader("foo")).containsExactly("bar", "baz");
242+
}
243+
244+
@Test // gh-25821
245+
void copyIfAbsentImmutableToMutable() {
246+
NativeMessageHeaderAccessor sourceAccessor = new NativeMessageHeaderAccessor();
247+
sourceAccessor.addNativeHeader("foo", "bar");
248+
Message<String> source = MessageBuilder.createMessage("payload", sourceAccessor.getMessageHeaders());
249+
250+
MessageHeaderAccessor targetAccessor = new NativeMessageHeaderAccessor();
251+
targetAccessor.copyHeadersIfAbsent(source.getHeaders());
252+
targetAccessor.setLeaveMutable(true);
253+
Message<?> target = MessageBuilder.createMessage(source.getPayload(), targetAccessor.getMessageHeaders());
254+
255+
MessageHeaderAccessor accessor = MessageHeaderAccessor.getMutableAccessor(target);
239256
assertThat(accessor.isMutable());
240257
((NativeMessageHeaderAccessor) accessor).addNativeHeader("foo", "baz");
241258
assertThat(((NativeMessageHeaderAccessor) accessor).getNativeHeader("foo")).containsExactly("bar", "baz");

0 commit comments

Comments
 (0)