Skip to content

Commit

Permalink
Merge pull request #22756 from geoand/#22624
Browse files Browse the repository at this point in the history
Fix Kotlin Serialization native support
  • Loading branch information
rsvoboda authored Jan 10, 2022
2 parents 3f00513 + 78bf6eb commit 6a36ecf
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,55 @@
import static io.quarkus.deployment.Feature.RESTEASY_REACTIVE_KOTLIN_SERIALIZATION;
import static io.quarkus.resteasy.reactive.common.deployment.ServerDefaultProducesHandlerBuildItem.json;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Priorities;
import javax.ws.rs.RuntimeType;
import javax.ws.rs.core.MediaType;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.DotName;

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.kotlin.serialization.KotlinSerializationMessageBodyReader;
import io.quarkus.kotlin.serialization.KotlinSerializationMessageBodyWriter;
import io.quarkus.resteasy.reactive.common.deployment.ServerDefaultProducesHandlerBuildItem;
import io.quarkus.resteasy.reactive.spi.MessageBodyReaderBuildItem;
import io.quarkus.resteasy.reactive.spi.MessageBodyWriterBuildItem;

public class KotlinSerializationProcessor {

private static final DotName SERIALIZABLE = DotName.createSimple("kotlinx.serialization.Serializable");
private static final String COMPANION_CLASS_SUFFIX = "$Companion";

// Kotlin Serialization generates classes at compile time which need to be available via reflection
// for serialization to work properly
@BuildStep
public void registerReflection(CombinedIndexBuildItem index, BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {
var serializableInstances = index.getIndex().getAnnotations(SERIALIZABLE);
if (serializableInstances.isEmpty()) {
return;
}

List<String> supportClassNames = new ArrayList<>(2 * serializableInstances.size());
for (AnnotationInstance instance : serializableInstances) {
if (instance.target().kind() != AnnotationTarget.Kind.CLASS) {
continue;
}
var targetClass = instance.target().asClass().name();
String companionClassName = targetClass.toString() + COMPANION_CLASS_SUFFIX;
supportClassNames.add(companionClassName);
}
reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, false, supportClassNames.toArray(new String[0])));
}

@BuildStep
public void additionalProviders(
BuildProducer<AdditionalBeanBuildItem> additionalBean,
Expand Down
6 changes: 0 additions & 6 deletions integration-tests/kotlin-serialization/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,6 @@
</pluginOptions>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.quarkus.it.kotser.model

import io.quarkus.runtime.annotations.RegisterForReflection
import kotlinx.serialization.Serializable

@Serializable
@RegisterForReflection
data class Person(var name: String, var defaulted: String = "hi there!") {
override fun toString(): String {
TODO("this shouldn't get called. a proper serialization should be invoked.")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.it.kotser

import io.quarkus.test.junit.QuarkusIntegrationTest

@QuarkusIntegrationTest
class ResourceIT : ResourceTest() {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.hamcrest.CoreMatchers.`is`
import org.junit.jupiter.api.Test

@QuarkusTest
class ResourceTest {
open class ResourceTest {
@Test
fun testGet() {
given()
Expand Down Expand Up @@ -37,4 +37,4 @@ class ResourceTest {
"defaulted": "hi there!"
}""".trimIndent()))
}
}
}

0 comments on commit 6a36ecf

Please sign in to comment.