Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Kotlin Serialization native support #22756

Merged
merged 1 commit into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()))
}
}
}