Skip to content

Commit

Permalink
Fix errors in Kotlin unit methods
Browse files Browse the repository at this point in the history
Kotlin API fails on methods that don't return anything
(which currently just includes `delete` methods) because
the generated code by default tries to serialize the methods'
return types in all cases, even if the return type is `Unit`.
I'm not exactly sure whether this is properly a bug in the openapi
generator or in moshi. In any case, I was unable to fix the error
by doing what initially seemed most sensible, which was to register
a moshi adapter for the `Unit` type.

The fix here is not terribly elegant but in practice seems to fix
the issue. I will also see about opening/adding to an existing issue
on the openapi generator's github page and/or submitting a PR to
fix this upstream.

Note also that I've removed the `Serializer.kt` template, which
is unused. We've already got an actual `Serializer` class
stored in the generated code folder, so there's no reason to
have the template.

Fixes #1117.
  • Loading branch information
jaymell committed Nov 16, 2023
1 parent 30776fc commit 89bf919
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 119 deletions.
1 change: 1 addition & 0 deletions kotlin/lib/src/test/com/svix/kotlin/BasicTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class BasicTest {
)
)
)
svix.application.delete(applicationOut.id)
}
}

Expand Down
116 changes: 0 additions & 116 deletions kotlin/templates/jvm-common/infrastructure/Serializer.kt.mustache

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,18 @@ import kotlin.random.Random
)
{{/jvm-okhttp3}}
{{#jvm-okhttp4}}
mediaType == JsonMediaType -> {{#moshi}}Serializer.moshi.adapter(T::class.java).toJson(content){{/moshi}}{{#gson}}Serializer.gson.toJson(content, T::class.java){{/gson}}{{#jackson}}Serializer.jacksonObjectMapper.writeValueAsString(content){{/jackson}}{{#kotlinx_serialization}}Serializer.jvmJson.encodeToString(content){{/kotlinx_serialization}}.toRequestBody(
mediaType.toMediaTypeOrNull()
)
mediaType == JsonMediaType -> {
// This is a bit of a hack to avoid moshi
// failing on trying to call `toJson` on
// a Unit type:
if (T::class.java == Unit::class.java) {
ByteArray(0).toRequestBody()
} else {
{{#moshi}}Serializer.moshi.adapter(T::class.java).toJson(content){{/moshi}}{{#gson}}Serializer.gson.toJson(content, T::class.java){{/gson}}{{#jackson}}Serializer.jacksonObjectMapper.writeValueAsString(content){{/jackson}}{{#kotlinx_serialization}}Serializer.jvmJson.encodeToString(content){{/kotlinx_serialization}}.toRequestBody(
mediaType.toMediaTypeOrNull()
)
}
}
{{/jvm-okhttp4}}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
// TODO: this should be extended with other serializers
Expand Down

0 comments on commit 89bf919

Please sign in to comment.