Skip to content

Commit

Permalink
Merge branch '6.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Jul 9, 2024
2 parents 9d45a8d + 4f38079 commit e2ce811
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 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 @@ -109,7 +109,15 @@ public Mono<Object> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableTy
}
return this.byteArrayDecoder
.decodeToMono(inputStream, elementType, mimeType, hints)
.map(byteArray -> format().decodeFromByteArray(serializer, byteArray));
.handle((byteArray, sink) -> {
try {
sink.next(format().decodeFromByteArray(serializer, byteArray));
sink.complete();
}
catch (IllegalArgumentException ex) {
sink.error(new DecodingException("Decoding error: " + ex.getMessage(), ex));
}
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import reactor.core.publisher.Mono;

import org.springframework.core.ResolvableType;
import org.springframework.core.codec.CodecException;
import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.DecodingException;
import org.springframework.core.codec.StringDecoder;
Expand Down Expand Up @@ -101,7 +102,14 @@ public Flux<Object> decode(Publisher<DataBuffer> inputStream, ResolvableType ele
}
return this.stringDecoder
.decode(inputStream, elementType, mimeType, hints)
.map(string -> format().decodeFromString(serializer, string));
.handle((string, sink) -> {
try {
sink.next(format().decodeFromString(serializer, string));
}
catch (IllegalArgumentException ex) {
sink.error(processException(ex));
}
});
});
}

Expand All @@ -115,8 +123,20 @@ public Mono<Object> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableTy
}
return this.stringDecoder
.decodeToMono(inputStream, elementType, mimeType, hints)
.map(string -> format().decodeFromString(serializer, string));
.handle((string, sink) -> {
try {
sink.next(format().decodeFromString(serializer, string));
sink.complete();
}
catch (IllegalArgumentException ex) {
sink.error(processException(ex));
}
});
});
}

private CodecException processException(IllegalArgumentException ex) {
return new DecodingException("Decoding error: " + ex.getMessage(), ex);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ import org.junit.jupiter.api.Test
import org.springframework.core.MethodParameter
import org.springframework.core.Ordered
import org.springframework.core.ResolvableType
import org.springframework.core.codec.DecodingException
import org.springframework.core.io.buffer.DataBuffer
import org.springframework.core.io.buffer.DataBufferUtils
import org.springframework.core.testfixture.codec.AbstractDecoderTests
import org.springframework.http.MediaType
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import reactor.test.StepVerifier
import reactor.test.StepVerifier.FirstStep
import java.lang.UnsupportedOperationException
import java.math.BigDecimal
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
Expand Down Expand Up @@ -85,6 +84,29 @@ class KotlinSerializationJsonDecoderTests : AbstractDecoderTests<KotlinSerializa
}, null, null)
}

@Test
fun decodeWithUnexpectedFormat() {
val input = Flux.concat(
stringBuffer("{\"ba\":\"b1\",\"fo\":\"f1\"}\n"),
)

testDecode(input, ResolvableType.forClass(Pojo::class.java), { step: FirstStep<Pojo> ->
step
.expectError(DecodingException::class.java)
.verify() }, null, null)
}

@Test
fun decodeToMonoWithUnexpectedFormat() {
val input = Flux.concat(
stringBuffer("{\"ba\":\"b1\",\"fo\":\"f1\"}\n"),
)

testDecodeToMono(input, ResolvableType.forClass(Pojo::class.java), { step: FirstStep<Pojo> ->
step.expectError(DecodingException::class.java)
.verify() }, null, null)
}

@Test
fun decodeStreamWithSingleBuffer() {
val input = Flux.concat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.core.Ordered
import org.springframework.core.ResolvableType
import org.springframework.core.codec.DecodingException
import org.springframework.core.io.buffer.DataBuffer
import org.springframework.core.testfixture.codec.AbstractDecoderTests
import org.springframework.http.MediaType
Expand Down Expand Up @@ -86,6 +87,21 @@ class KotlinSerializationProtobufDecoderTests : AbstractDecoderTests<KotlinSeria
}, null, null)
}

@Test
fun decodeToMonoWithUnexpectedFormat() {
val input = Mono.just(
bufferFactory.allocateBuffer(0),
)

val elementType = ResolvableType.forClass(Pojo::class.java)

testDecodeToMono(input, elementType, { step: FirstStep<Any> ->
step
.expectError(DecodingException::class.java)
.verify()
}, null, null)
}

private fun byteBuffer(value: Any): Mono<DataBuffer> {
return Mono.defer {
val bytes = ProtoBuf.Default.encodeToByteArray(serializer(Pojo::class.java), value)
Expand Down

0 comments on commit e2ce811

Please sign in to comment.