Add kotlinx.serialization JSON support to Spring WebFlux
Flow decoding is not supported yet since it depends on kotlin/kotlinx.serialization#1073, but it will be enabled when this issue will be fixed. Closes gh-25771
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2002-2020 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.http.codec.json
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.core.ResolvableType
|
||||
import org.springframework.core.io.buffer.DataBuffer
|
||||
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.nio.charset.Charset
|
||||
import java.nio.charset.StandardCharsets
|
||||
|
||||
/**
|
||||
* Tests for the JSON decoding using kotlinx.serialization.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
class KotlinSerializationJsonDecoderTests : AbstractDecoderTests<KotlinSerializationJsonDecoder>(KotlinSerializationJsonDecoder()) {
|
||||
|
||||
@Suppress("UsePropertyAccessSyntax", "DEPRECATION")
|
||||
@Test
|
||||
override fun canDecode() {
|
||||
val jsonSubtype = MediaType("application", "vnd.test-micro-type+json")
|
||||
Assertions.assertThat(decoder.canDecode(ResolvableType.forClass(Pojo::class.java), MediaType.APPLICATION_JSON)).isTrue()
|
||||
Assertions.assertThat(decoder.canDecode(ResolvableType.forClass(Pojo::class.java), jsonSubtype)).isTrue()
|
||||
Assertions.assertThat(decoder.canDecode(ResolvableType.forClass(Pojo::class.java), null)).isTrue()
|
||||
Assertions.assertThat(decoder.canDecode(ResolvableType.forClass(String::class.java), null)).isFalse()
|
||||
Assertions.assertThat(decoder.canDecode(ResolvableType.forClass(Pojo::class.java), MediaType.APPLICATION_XML)).isFalse()
|
||||
Assertions.assertThat(decoder.canDecode(ResolvableType.forClass(Pojo::class.java),
|
||||
MediaType("application", "json", StandardCharsets.UTF_8))).isTrue()
|
||||
Assertions.assertThat(decoder.canDecode(ResolvableType.forClass(Pojo::class.java),
|
||||
MediaType("application", "json", StandardCharsets.US_ASCII))).isTrue()
|
||||
Assertions.assertThat(decoder.canDecode(ResolvableType.forClass(Pojo::class.java),
|
||||
MediaType("application", "json", StandardCharsets.ISO_8859_1))).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
override fun decode() {
|
||||
val output = decoder.decode(Mono.empty(), ResolvableType.forClass(Pojo::class.java), null, emptyMap())
|
||||
StepVerifier
|
||||
.create(output)
|
||||
.expectError(UnsupportedOperationException::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
override fun decodeToMono() {
|
||||
val input = Flux.concat(
|
||||
stringBuffer("[{\"bar\":\"b1\",\"foo\":\"f1\"},"),
|
||||
stringBuffer("{\"bar\":\"b2\",\"foo\":\"f2\"}]"))
|
||||
|
||||
val elementType = ResolvableType.forClassWithGenerics(List::class.java, Pojo::class.java)
|
||||
|
||||
testDecodeToMonoAll(input, elementType, { step: FirstStep<Any> ->
|
||||
step
|
||||
.expectNext(listOf(Pojo("f1", "b1"), Pojo("f2", "b2")))
|
||||
.expectComplete()
|
||||
.verify()
|
||||
}, null, null)
|
||||
}
|
||||
|
||||
private fun stringBuffer(value: String): Mono<DataBuffer> {
|
||||
return stringBuffer(value, StandardCharsets.UTF_8)
|
||||
}
|
||||
|
||||
private fun stringBuffer(value: String, charset: Charset): Mono<DataBuffer> {
|
||||
return Mono.defer {
|
||||
val bytes = value.toByteArray(charset)
|
||||
val buffer = bufferFactory.allocateBuffer(bytes.size)
|
||||
buffer.write(bytes)
|
||||
Mono.just(buffer)
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class Pojo(val foo: String, val bar: String)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright 2002-2020 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.http.codec.json
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.core.ResolvableType
|
||||
import org.springframework.core.io.buffer.DataBuffer
|
||||
import org.springframework.core.io.buffer.DataBufferUtils
|
||||
import org.springframework.core.testfixture.codec.AbstractEncoderTests
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.http.codec.ServerSentEvent
|
||||
import reactor.core.publisher.Flux
|
||||
import reactor.core.publisher.Mono
|
||||
import reactor.test.StepVerifier.FirstStep
|
||||
import java.nio.charset.StandardCharsets
|
||||
|
||||
/**
|
||||
* Tests for the JSON encoding using kotlinx.serialization.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
@Suppress("UsePropertyAccessSyntax")
|
||||
class KotlinSerializationJsonEncoderTests : AbstractEncoderTests<KotlinSerializationJsonEncoder>(KotlinSerializationJsonEncoder()) {
|
||||
|
||||
@Test
|
||||
override fun canEncode() {
|
||||
val pojoType = ResolvableType.forClass(Pojo::class.java)
|
||||
val jsonSubtype = MediaType("application", "vnd.test-micro-type+json")
|
||||
Assertions.assertThat(encoder.canEncode(pojoType, MediaType.APPLICATION_JSON)).isTrue()
|
||||
Assertions.assertThat(encoder.canEncode(pojoType, jsonSubtype)).isTrue()
|
||||
Assertions.assertThat(encoder.canEncode(pojoType, null)).isTrue()
|
||||
Assertions.assertThat(encoder.canEncode(ResolvableType.forClass(Pojo::class.java),
|
||||
MediaType("application", "json", StandardCharsets.UTF_8))).isTrue()
|
||||
Assertions.assertThat(encoder.canEncode(ResolvableType.forClass(Pojo::class.java),
|
||||
MediaType("application", "json", StandardCharsets.US_ASCII))).isTrue()
|
||||
Assertions.assertThat(encoder.canEncode(ResolvableType.NONE, null)).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
override fun encode() {
|
||||
val input = Flux.just(
|
||||
Pojo("foo", "bar"),
|
||||
Pojo("foofoo", "barbar"),
|
||||
Pojo("foofoofoo", "barbarbar")
|
||||
)
|
||||
testEncode(input, Pojo::class.java, { step: FirstStep<DataBuffer?> ->
|
||||
step
|
||||
.consumeNextWith(expectString("[" +
|
||||
"{\"foo\":\"foo\",\"bar\":\"bar\"}," +
|
||||
"{\"foo\":\"foofoo\",\"bar\":\"barbar\"}," +
|
||||
"{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}]")
|
||||
.andThen { dataBuffer: DataBuffer? -> DataBufferUtils.release(dataBuffer) })
|
||||
.verifyComplete()
|
||||
})
|
||||
}
|
||||
|
||||
@Test
|
||||
fun encodeMono() {
|
||||
val input = Mono.just(Pojo("foo", "bar"))
|
||||
testEncode(input, Pojo::class.java, { step: FirstStep<DataBuffer?> ->
|
||||
step
|
||||
.consumeNextWith(expectString("{\"foo\":\"foo\",\"bar\":\"bar\"}")
|
||||
.andThen { dataBuffer: DataBuffer? -> DataBufferUtils.release(dataBuffer) })
|
||||
.verifyComplete()
|
||||
})
|
||||
}
|
||||
|
||||
@Test
|
||||
fun canNotEncode() {
|
||||
Assertions.assertThat(encoder.canEncode(ResolvableType.forClass(String::class.java), null)).isFalse()
|
||||
Assertions.assertThat(encoder.canEncode(ResolvableType.forClass(Pojo::class.java), MediaType.APPLICATION_XML)).isFalse()
|
||||
val sseType = ResolvableType.forClass(ServerSentEvent::class.java)
|
||||
Assertions.assertThat(encoder.canEncode(sseType, MediaType.APPLICATION_JSON)).isFalse()
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class Pojo(val foo: String, val bar: String)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user