You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Kobby supports generation of Jackson annotations for DTO classes to provide serialization / deserialization feature. But Jackson does not support Kotlin multiplatform serialization / deserialization. It makes impossible to use Kobby as multiplatform client. We have to support of Kotlinx Serialization for generated DSL client to use Kobby in multiplatform projects.
Ktor at least version 2.0.0 is required for default adapters.
Implicit setup
To enable Kotlinx Serialization support just add org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0 dependency and configure serialization plugin.
Gradle
plugins {
kotlin("jvm") version "1.8.20"
kotlin("plugin.serialization") version "1.8.20"
id("io.github.ermadmi78.kobby") version "3.0.0-beta.01"
}
dependencies {
// Add this dependency to enable Kotlinx Serialization
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
}
You can explicitly enable (or disable) Kotlinx Serialization support in the generated code, but you still need to add kotlinx-serialization-json dependency and configure serialization plugin. In addition to the "implicit setup" you can add:
Gradle
kobby {
kotlin {
dto {
serialization {
// Is Kotlinx Serialization enabled.// By default, "true" if "org.jetbrains.kotlinx:kotlinx-serialization-json" artifact// is in the project dependencies.
enabled =true// Name of the class descriptor property for polymorphic serialization.
classDiscriminator ="__typename"// Specifies whether encounters of unknown properties in the input JSON// should be ignored instead of throwing SerializationException.
ignoreUnknownKeys =true// Specifies whether default values of Kotlin properties should be encoded to JSON.
encodeDefaults =false// Specifies whether resulting JSON should be pretty-printed.
prettyPrint =false
}
}
}
}
Maven
<plugin>
<groupId>io.github.ermadmi78</groupId>
<artifactId>kobby-maven-plugin</artifactId>
<version>3.0.0-beta.01</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate-kotlin</goal>
</goals>
<configuration>
<kotlin>
<dto>
<serialization>
<!-- Is Kotlinx Serialization enabled. --><!-- By default, "true" if "org.jetbrains.kotlinx:kotlinx-serialization-json" --><!-- artifact is in the project dependencies. -->
<enabled>true</enabled>
<!-- Name of the class descriptor property for polymorphic serialization. -->
<classDiscriminator>__typename</classDiscriminator>
<!-- Specifies whether encounters of unknown properties in the input JSON --><!-- should be ignored instead of throwing SerializationException. -->
<ignoreUnknownKeys>true</ignoreUnknownKeys>
<!-- Specifies whether default values of Kotlin properties --><!-- should be encoded to JSON. -->
<encodeDefaults>false</encodeDefaults>
<!-- Specifies whether resulting JSON should be pretty-printed. -->
<prettyPrint>false</prettyPrint>
</serialization>
</dto>
</kotlin>
</configuration>
</execution>
</executions>
</plugin>
Kotlinx Serialization entry point
The Kotlinx Serialization entry point in the generated DSL is placed near the DSL context entry point (root file xxx.kt, where xxx is the name of the context). For example, for a context named cinema it would look like this:
You can configure a custom serializer to any type associated with a scalar. For example, let's define a Date scalar in our schema and associate it with a java.time.LocalDate.
scalarDatetypeQuery {
extract: Date!
}
First, we must write custom serializer for java.time.LocalDate:
During the development process, it turned out that the Kotlinx Serialization engine does not like type Any at all.
To get around this limitation, the plugin replaces type Any in the generated code according to the following rules:
Such a replacement leads to the fact that it is impossible to generate DTO classes that can be serialized using Jackson and Kotlinx Serialization at the same time. Therefore, you will need to choose one of the serialization engines and use only that.
The text was updated successfully, but these errors were encountered:
Kobby supports generation of Jackson annotations for DTO classes to provide serialization / deserialization feature. But Jackson does not support Kotlin multiplatform serialization / deserialization. It makes impossible to use Kobby as multiplatform client. We have to support of Kotlinx Serialization for generated DSL client to use Kobby in multiplatform projects.
Examples
Gradle example
Maven example
Requirements
Implicit setup
To enable Kotlinx Serialization support just add
org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0
dependency and configure serialization plugin.Gradle
Maven
Explicit setup
You can explicitly enable (or disable) Kotlinx Serialization support in the generated code, but you still need to add
kotlinx-serialization-json
dependency and configure serialization plugin. In addition to the "implicit setup" you can add:Gradle
Maven
Kotlinx Serialization entry point
The Kotlinx Serialization entry point in the generated DSL is placed near the DSL context entry point (root file
xxx.kt
, wherexxx
is the name of the context). For example, for a context namedcinema
it would look like this:cinema.kt
You must pass
cinemaJson
to the default adapters to ensure Kotlinx Serialization.Simple Adapter configuration
Composite Adapter configuration
You don't need to pass
cinemaJson
to the composite adapter as it is configured as the default value ofmapper
argument:Custom serializers
You can configure a custom serializer to any type associated with a scalar. For example, let's define a
Date
scalar in our schema and associate it with ajava.time.LocalDate
.First, we must write custom serializer for
java.time.LocalDate
:Second, we must bind
java.time.LocalDate
toDate
scalar and set upLocalDateSerializer
for it:Gradle (see scalar mapping)
Maven (see scalar mapping)
Mixing serialization engines
During the development process, it turned out that the Kotlinx Serialization engine does not like type
Any
at all.To get around this limitation, the plugin replaces type
Any
in the generated code according to the following rules:Map<String, Any?>
-> JsonObjectList<Any?>
-> JsonArrayAny?
-> JsonElementFor example, the GraphQL request DTO for Jackson serialization engine looks like this:
But the same DTO for Kotlinx Serialization engine looks like this:
Such a replacement leads to the fact that it is impossible to generate DTO classes that can be serialized using Jackson and Kotlinx Serialization at the same time. Therefore, you will need to choose one of the serialization engines and use only that.
The text was updated successfully, but these errors were encountered: