Skip to content

Commit

Permalink
Multipart writer detects custom Jackson encoder
Browse files Browse the repository at this point in the history
Closes gh-27017
  • Loading branch information
rstoyanchev committed Jun 8, 2021
1 parent cc2b980 commit a42540e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public void jackson2JsonDecoder(Decoder<?> decoder) {
public void jackson2JsonEncoder(Encoder<?> encoder) {
this.jackson2JsonEncoder = encoder;
initObjectWriters();
initTypedWriters();
}

@Override
Expand All @@ -233,6 +234,7 @@ public void jackson2SmileDecoder(Decoder<?> decoder) {
public void jackson2SmileEncoder(Encoder<?> encoder) {
this.jackson2SmileEncoder = encoder;
initObjectWriters();
initTypedWriters();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,28 @@ public void defaultWriters() {
}

@Test
public void jackson2CodecCustomizations() {
public void jackson2CodecCustomization() {
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
this.configurer.defaultCodecs().jackson2JsonDecoder(decoder);
this.configurer.defaultCodecs().jackson2JsonEncoder(encoder);

List<HttpMessageReader<?>> readers = this.configurer.getReaders();
Jackson2JsonDecoder actualDecoder = findCodec(readers, Jackson2JsonDecoder.class);
assertThat(actualDecoder).isSameAs(decoder);
assertThat(findCodec(readers, ServerSentEventHttpMessageReader.class).getDecoder()).isSameAs(decoder);

List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
Jackson2JsonEncoder actualEncoder = findCodec(writers, Jackson2JsonEncoder.class);
assertThat(actualEncoder).isSameAs(encoder);

MultipartHttpMessageWriter multipartWriter = findCodec(writers, MultipartHttpMessageWriter.class);
actualEncoder = findCodec(multipartWriter.getPartWriters(), Jackson2JsonEncoder.class);
assertThat(actualEncoder).isSameAs(encoder);
}

@Test
public void objectMapperCustomization() {
ObjectMapper objectMapper = new ObjectMapper();
this.configurer.defaultCodecs().configureDefaultCodec(codec -> {
if (codec instanceof Jackson2CodecSupport) {
Expand All @@ -138,13 +154,14 @@ public void jackson2CodecCustomizations() {

List<HttpMessageReader<?>> readers = this.configurer.getReaders();
Jackson2JsonDecoder actualDecoder = findCodec(readers, Jackson2JsonDecoder.class);
assertThat(actualDecoder).isSameAs(decoder);
assertThat(actualDecoder.getObjectMapper()).isSameAs(objectMapper);
assertThat(findCodec(readers, ServerSentEventHttpMessageReader.class).getDecoder()).isSameAs(decoder);

List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
Jackson2JsonEncoder actualEncoder = findCodec(writers, Jackson2JsonEncoder.class);
assertThat(actualEncoder).isSameAs(encoder);
assertThat(actualEncoder.getObjectMapper()).isSameAs(objectMapper);

MultipartHttpMessageWriter multipartWriter = findCodec(writers, MultipartHttpMessageWriter.class);
actualEncoder = findCodec(multipartWriter.getPartWriters(), Jackson2JsonEncoder.class);
assertThat(actualEncoder.getObjectMapper()).isSameAs(objectMapper);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -125,15 +125,19 @@ public void defaultWriters() {

@Test
public void jackson2EncoderOverride() {
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
this.configurer.defaultCodecs().jackson2JsonDecoder(decoder);
this.configurer.defaultCodecs().jackson2JsonEncoder(encoder);

assertThat(this.configurer.getWriters().stream()
.filter(writer -> ServerSentEventHttpMessageWriter.class.equals(writer.getClass()))
.map(writer -> (ServerSentEventHttpMessageWriter) writer)
.findFirst()
.map(ServerSentEventHttpMessageWriter::getEncoder)
.filter(e -> e == encoder).orElse(null)).isSameAs(encoder);
List<HttpMessageReader<?>> readers = this.configurer.getReaders();
Jackson2JsonDecoder actualDecoder = findCodec(readers, Jackson2JsonDecoder.class);
assertThat(actualDecoder).isSameAs(decoder);

List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
Jackson2JsonEncoder actualEncoder = findCodec(writers, Jackson2JsonEncoder.class);
assertThat(actualEncoder).isSameAs(encoder);
assertThat(findCodec(writers, ServerSentEventHttpMessageWriter.class).getEncoder()).isSameAs(encoder);
}

@Test
Expand Down Expand Up @@ -261,7 +265,19 @@ private Encoder<?> getNextEncoder(List<HttpMessageWriter<?>> writers) {

@SuppressWarnings("unchecked")
private <T> T findCodec(List<?> codecs, Class<T> type) {
return (T) codecs.stream().filter(type::isInstance).findFirst().get();
return (T) codecs.stream()
.map(c -> {
if (c instanceof EncoderHttpMessageWriter) {
return ((EncoderHttpMessageWriter<?>) c).getEncoder();
}
else if (c instanceof DecoderHttpMessageReader) {
return ((DecoderHttpMessageReader<?>) c).getDecoder();
}
else {
return c;
}
})
.filter(type::isInstance).findFirst().get();
}

@SuppressWarnings("unchecked")
Expand Down

0 comments on commit a42540e

Please sign in to comment.