-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38064 from marko-bekhta/fix/i36558-kotlin-seriali…
…zation-validation-report Add custom Kotlin serializers for ValidationReport and Violation
- Loading branch information
Showing
12 changed files
with
398 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...quarkus/resteasy/reactive/kotlin/serialization/runtime/ValidationJsonBuilderCustomizer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.quarkus.resteasy.reactive.kotlin.serialization.runtime | ||
|
||
import io.quarkus.resteasy.reactive.kotlin.serialization.common.JsonBuilderCustomizer | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.json.JsonBuilder | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.contextual | ||
import kotlinx.serialization.modules.plus | ||
|
||
class ValidationJsonBuilderCustomizer : JsonBuilderCustomizer { | ||
@ExperimentalSerializationApi | ||
override fun customize(jsonBuilder: JsonBuilder) { | ||
jsonBuilder.serializersModule = | ||
jsonBuilder.serializersModule.plus( | ||
SerializersModule { | ||
contextual(ViolationReportSerializer) | ||
contextual(ViolationReportViolationSerializer) | ||
} | ||
) | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...in/io/quarkus/resteasy/reactive/kotlin/serialization/runtime/ViolationReportSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.quarkus.resteasy.reactive.kotlin.serialization.runtime | ||
|
||
import io.quarkus.hibernate.validator.runtime.jaxrs.ViolationReport | ||
import jakarta.ws.rs.core.Response | ||
import kotlinx.serialization.* | ||
import kotlinx.serialization.builtins.ListSerializer | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor | ||
import kotlinx.serialization.descriptors.listSerialDescriptor | ||
import kotlinx.serialization.descriptors.serialDescriptor | ||
import kotlinx.serialization.encoding.CompositeDecoder.Companion.DECODE_DONE | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.encoding.decodeStructure | ||
import kotlinx.serialization.encoding.encodeStructure | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
@Serializer(forClass = ViolationReport::class) | ||
object ViolationReportSerializer : KSerializer<ViolationReport> { | ||
override val descriptor: SerialDescriptor = | ||
buildClassSerialDescriptor("io.quarkus.hibernate.validator.runtime.jaxrs.ViolationReport") { | ||
element("title", serialDescriptor<String>()) | ||
element("status", serialDescriptor<Int>()) | ||
element( | ||
"violations", | ||
listSerialDescriptor(ListSerializer(ViolationReportViolationSerializer).descriptor) | ||
) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): ViolationReport { | ||
return decoder.decodeStructure(descriptor) { | ||
var title: String? = null | ||
var status: Int? = null | ||
var violations: List<ViolationReport.Violation> = emptyList() | ||
|
||
loop@ while (true) { | ||
when (val index = decodeElementIndex(descriptor)) { | ||
DECODE_DONE -> break@loop | ||
0 -> title = decodeStringElement(descriptor, 0) | ||
1 -> status = decodeIntElement(descriptor, 1) | ||
2 -> | ||
violations = | ||
decodeSerializableElement( | ||
descriptor, | ||
2, | ||
ListSerializer(ViolationReportViolationSerializer) | ||
) | ||
else -> throw SerializationException("Unexpected index $index") | ||
} | ||
} | ||
|
||
ViolationReport( | ||
requireNotNull(title), | ||
status?.let { Response.Status.fromStatusCode(it) }, | ||
violations | ||
) | ||
} | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: ViolationReport) { | ||
encoder.encodeStructure(descriptor) { | ||
encodeStringElement(descriptor, 0, value.title) | ||
encodeIntElement(descriptor, 1, value.status) | ||
encodeSerializableElement( | ||
descriptor, | ||
2, | ||
ListSerializer(ViolationReportViolationSerializer), | ||
value.violations | ||
) | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...rkus/resteasy/reactive/kotlin/serialization/runtime/ViolationReportViolationSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.quarkus.resteasy.reactive.kotlin.serialization.runtime | ||
|
||
import io.quarkus.hibernate.validator.runtime.jaxrs.ViolationReport | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.Serializer | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor | ||
import kotlinx.serialization.descriptors.serialDescriptor | ||
import kotlinx.serialization.encoding.* | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
@Serializer(forClass = ViolationReport.Violation::class) | ||
object ViolationReportViolationSerializer : KSerializer<ViolationReport.Violation> { | ||
override val descriptor: SerialDescriptor = | ||
buildClassSerialDescriptor( | ||
"io.quarkus.hibernate.validator.runtime.jaxrs.ViolationReport.Violation" | ||
) { | ||
element("field", serialDescriptor<String>()) | ||
element("message", serialDescriptor<String>()) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): ViolationReport.Violation { | ||
return decoder.decodeStructure(descriptor) { | ||
var field: String? = null | ||
var message: String? = null | ||
|
||
loop@ while (true) { | ||
when (val index = decodeElementIndex(descriptor)) { | ||
CompositeDecoder.DECODE_DONE -> break@loop | ||
0 -> field = decodeStringElement(descriptor, 0) | ||
1 -> message = decodeStringElement(descriptor, 1) | ||
else -> throw SerializationException("Unexpected index $index") | ||
} | ||
} | ||
|
||
ViolationReport.Violation( | ||
requireNotNull(field), | ||
requireNotNull(message), | ||
) | ||
} | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: ViolationReport.Violation) { | ||
encoder.encodeStructure(descriptor) { | ||
encodeStringElement(descriptor, 0, value.field) | ||
encodeStringElement(descriptor, 1, value.message) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.