Skip to content

Commit fce972e

Browse files
dlsrb6342sbrannen
authored andcommitted
Fix BaseCodecConfigurer clone bug
Prior to this commit, ExchangeStrategies custom codec's reader and writer were not registered due to a bug in BaseCodecConfigurer. This commit fixes this by correcting the implementation of the DefaultCustomCodecs constructor used within BaseCodecConfigurer. Closes gh-25149
1 parent a92f425 commit fce972e

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

spring-web/src/main/java/org/springframework/http/codec/support/BaseCodecConfigurer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ protected static final class DefaultCustomCodecs implements CustomCodecs {
142142
* @since 5.1.12
143143
*/
144144
DefaultCustomCodecs(DefaultCustomCodecs other) {
145-
other.typedReaders.putAll(this.typedReaders);
146-
other.typedWriters.putAll(this.typedWriters);
147-
other.objectReaders.putAll(this.objectReaders);
148-
other.objectWriters.putAll(this.objectWriters);
145+
this.typedReaders.putAll(other.typedReaders);
146+
this.typedWriters.putAll(other.typedWriters);
147+
this.objectReaders.putAll(other.objectReaders);
148+
this.objectWriters.putAll(other.objectWriters);
149149
}
150150

151151
@Override

spring-web/src/test/java/org/springframework/http/codec/support/CodecConfigurerTests.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public void encoderDecoderOverrides() {
279279
}
280280

281281
@Test
282-
public void cloneCustomCodecs() {
282+
public void cloneEmptyCustomCodecs() {
283283
this.configurer.registerDefaults(false);
284284
CodecConfigurer clone = this.configurer.clone();
285285

@@ -294,6 +294,22 @@ public void cloneCustomCodecs() {
294294
assertThat(clone.getWriters().size()).isEqualTo(2);
295295
}
296296

297+
@Test
298+
public void cloneCustomCodecs() {
299+
CodecConfigurer from = new TestCodecConfigurer();
300+
from.registerDefaults(false);
301+
from.customCodecs().register(new Jackson2JsonEncoder());
302+
from.customCodecs().register(new Jackson2JsonDecoder());
303+
from.customCodecs().register(new ServerSentEventHttpMessageReader());
304+
from.customCodecs().register(new ServerSentEventHttpMessageWriter());
305+
306+
CodecConfigurer clone = from.clone();
307+
assertThat(from.getReaders().size()).isEqualTo(2);
308+
assertThat(from.getWriters().size()).isEqualTo(2);
309+
assertThat(clone.getReaders().size()).isEqualTo(2);
310+
assertThat(clone.getWriters().size()).isEqualTo(2);
311+
}
312+
297313
@Test
298314
public void cloneDefaultCodecs() {
299315
CodecConfigurer clone = this.configurer.clone();

0 commit comments

Comments
 (0)