Skip to content

Commit

Permalink
In order to use Wirespec in kotlin multiplatform no JVM dependencies (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel authored Jun 27, 2024
1 parent 78846d7 commit 06ea1a9
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 159 deletions.
32 changes: 0 additions & 32 deletions examples/spring-boot-openapi-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,38 +107,6 @@
<shared>false</shared>
</configuration>
</execution>
<execution>
<id>java-v2</id>
<goals>
<goal>convert</goal>
</goals>
<configuration>
<input>${project.basedir}/src/main/openapi/petstorev2.json</input>
<output>${project.build.directory}/generated-sources</output>
<packageName>community.flock.wirespec.generated.java.v2</packageName>
<format>OpenApiV2</format>
<languages>
<language>Java</language>
</languages>
<shared>false</shared>
</configuration>
</execution>
<execution>
<id>java-v3</id>
<goals>
<goal>convert</goal>
</goals>
<configuration>
<input>${project.basedir}/src/main/openapi/petstorev3.json</input>
<output>${project.build.directory}/generated-sources</output>
<packageName>community.flock.wirespec.generated.java.v3</packageName>
<format>OpenApiV3</format>
<languages>
<language>Java</language>
</languages>
<shared>false</shared>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package community.flock.wirespec.examples.open_api_app.kotlin
package community.flock.wirespec.examples.open_api_app

import community.flock.wirespec.Wirespec
import community.flock.wirespec.generated.kotlin.v3.AddPetEndpoint
Expand All @@ -8,6 +8,7 @@ import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpMethod
import org.springframework.web.client.RestTemplate
import java.net.URI
import kotlin.reflect.typeOf

interface KotlinPetstoreClient : AddPetEndpoint, FindPetsByStatusEndpoint

Expand All @@ -28,7 +29,7 @@ class KotlinPetClientConfiguration {
HttpMethod.valueOf(request.method.name),
{ req ->
request.content
?.let { contentMapper.write(it) }
?.let { contentMapper.write(it, typeOf<Any>()) }
?.let { req.body.write(it.body) }
},
{ res ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package community.flock.wirespec.examples.open_api_app.kotlin
package community.flock.wirespec.examples.open_api_app

import community.flock.wirespec.generated.kotlin.v3.AddPetEndpoint
import community.flock.wirespec.generated.kotlin.v3.FindPetsByStatusEndpoint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
@file:OptIn(ExperimentalStdlibApi::class)

package community.flock.wirespec.examples.open_api_app

import com.fasterxml.jackson.databind.ObjectMapper
import community.flock.wirespec.Wirespec
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.lang.reflect.Type
import kotlin.reflect.KType
import kotlin.reflect.javaType

@Configuration
class WirespecConfiguration {
Expand All @@ -14,15 +17,16 @@ class WirespecConfiguration {
object : Wirespec.ContentMapper<ByteArray> {
override fun <T> read(
content: Wirespec.Content<ByteArray>,
valueType: Type,
valueType: KType,
): Wirespec.Content<T> = content.let {
val type = objectMapper.constructType(valueType)
val type = objectMapper.constructType(valueType.javaType)
val obj: T = objectMapper.readValue(content.body, type)
Wirespec.Content(it.type, obj)
}

override fun <T> write(
content: Wirespec.Content<T>,
valueType: KType,
): Wirespec.Content<ByteArray> = content.let {
val bytes = objectMapper.writeValueAsBytes(content.body)
Wirespec.Content(it.type, bytes)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ open class KotlinEmitter(
val import = """
|
|import community.flock.wirespec.Wirespec
|import kotlin.reflect.typeOf
|
""".trimMargin()

Expand Down Expand Up @@ -184,7 +185,7 @@ open class KotlinEmitter(
else
"""
|request.content?.type == "${content.type}" -> contentMapper
| .read<${content.reference.emitWrap()}>(request.content!!, Wirespec.getType(${content.reference.emit()}::class.java, ${isIterable}))
| .read<${content.reference.emitWrap()}>(request.content!!, typeOf<${content.reference.emitWrap()}>())
| .let { ${responseReference.emitWrap()}(request.path, request.method, request.query, request.headers, it) }
""".trimMargin()

Expand All @@ -205,7 +206,7 @@ open class KotlinEmitter(
else
"""
|${if (statusCode.isInt()) "response.status == $statusCode && " else ""}response.content?.type == "${content.type}" -> contentMapper
| .read<${content.reference.emitWrap()}>(response.content!!, Wirespec.getType(${content.reference.emit()}::class.java, false))
| .read<${content.reference.emitWrap()}>(response.content!!, typeOf<${content.reference.emitWrap()}>())
| .let { ${responseReference.emitWrap()}(response.status, response.headers, it) }
""".trimMargin()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,17 @@ data object KotlinShared : Shared {
override val source = """
|package community.flock.wirespec
|
|import java.lang.reflect.Type
|import java.lang.reflect.ParameterizedType
|import kotlin.reflect.KType
|
|object Wirespec {
|${SPACER}interface Enum
|${SPACER}interface Endpoint
|${SPACER}interface Refined { val value: String }
|${SPACER}enum class Method { GET, PUT, POST, DELETE, OPTIONS, HEAD, PATCH, TRACE }
|$SPACER@JvmRecord data class Content<T> (val type:String, val body:T )
|${SPACER}data class Content<T> (val type:String, val body:T )
|${SPACER}interface Request<T> { val path:String; val method: Method; val query: Map<String, List<Any?>>; val headers: Map<String, List<Any?>>; val content:Content<T>? }
|${SPACER}interface Response<T> { val status:Int; val headers: Map<String, List<Any?>>; val content:Content<T>? }
|${SPACER}interface ContentMapper<B> { fun <T> read(content: Content<B>, valueType: Type): Content<T> fun <T> write(content: Content<T>): Content<B> }
|$SPACER$SPACER@JvmStatic fun getType(type: Class<*>, isIterable: Boolean): Type {
|$SPACER${SPACER}return if (isIterable) {
|$SPACER$SPACER${SPACER}object : ParameterizedType {
|$SPACER$SPACER$SPACER${SPACER}override fun getRawType() = MutableList::class.java
|$SPACER$SPACER$SPACER${SPACER}override fun getActualTypeArguments() = arrayOf(type)
|$SPACER$SPACER$SPACER${SPACER}override fun getOwnerType() = null
|$SPACER$SPACER$SPACER}
|$SPACER$SPACER} else {
|$SPACER$SPACER${SPACER}type
|$SPACER$SPACER}
|$SPACER}
|${SPACER}interface ContentMapper<B> { fun <T> read(content: Content<B>, valueType: KType): Content<T> fun <T> write(content: Content<T>, valueType: KType): Content<B> }
|}
""".trimMargin()
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class CompileEndpointTest {
|package community.flock.wirespec.generated
|
|import community.flock.wirespec.Wirespec
|import kotlin.reflect.typeOf
|
|interface TodoEndpoint : Wirespec.Endpoint {
| sealed interface Request<T> : Wirespec.Request<T>
Expand Down Expand Up @@ -87,7 +88,7 @@ class CompileEndpointTest {
| fun <B> RESPONSE_MAPPER(contentMapper: Wirespec.ContentMapper<B>) = { response: Wirespec.Response<B> ->
| when {
| response.status == 200 && response.content?.type == "application/json" -> contentMapper
| .read<Todo>(response.content!!, Wirespec.getType(Todo::class.java, false))
| .read<Todo>(response.content!!, typeOf<Todo>())
| .let { Response200ApplicationJson(response.status, response.headers, it) }
| else -> error("Cannot map response with status ${'$'}{response.status}")
| }
Expand All @@ -112,6 +113,7 @@ class CompileEndpointTest {
|package community.flock.wirespec.generated
|
|import community.flock.wirespec.Wirespec
|import kotlin.reflect.typeOf
|
|interface TodoEndpoint : Wirespec.Endpoint {
| sealed interface Request<T> : Wirespec.Request<T>
Expand Down Expand Up @@ -147,15 +149,15 @@ class CompileEndpointTest {
| fun <B> REQUEST_MAPPER(contentMapper: Wirespec.ContentMapper<B>) = { request: Wirespec.Request<B> ->
| when {
| request.content?.type == "application/json" -> contentMapper
| .read<community.flock.wirespec.generated.Request>(request.content!!, Wirespec.getType(community.flock.wirespec.generated.Request::class.java, false))
| .read<community.flock.wirespec.generated.Request>(request.content!!, typeOf<community.flock.wirespec.generated.Request>())
| .let { RequestApplicationJson(request.path, request.method, request.query, request.headers, it) }
| else -> error("Cannot map request")
| }
| }
| fun <B> RESPONSE_MAPPER(contentMapper: Wirespec.ContentMapper<B>) = { response: Wirespec.Response<B> ->
| when {
| response.status == 200 && response.content?.type == "application/json" -> contentMapper
| .read<community.flock.wirespec.generated.Response>(response.content!!, Wirespec.getType(community.flock.wirespec.generated.Response::class.java, false))
| .read<community.flock.wirespec.generated.Response>(response.content!!, typeOf<community.flock.wirespec.generated.Response>())
| .let { Response200ApplicationJson(response.status, response.headers, it) }
| else -> error("Cannot map response with status ${'$'}{response.status}")
| }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CompileEnumTest {
|package community.flock.wirespec.generated
|
|import community.flock.wirespec.Wirespec
|import kotlin.reflect.typeOf
|
|enum class MyAwesomeEnum (val label: String): Wirespec.Enum {
| ONE("ONE"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CompileRefinedTest {
package community.flock.wirespec.generated
import community.flock.wirespec.Wirespec
import kotlin.reflect.typeOf
data class TodoId(override val value: String): Wirespec.Refined {
override fun toString() = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class CompilerTest {
package community.flock.wirespec.generated
import community.flock.wirespec.Wirespec
import kotlin.reflect.typeOf
data class Name(override val value: String): Wirespec.Refined {
override fun toString() = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class KotlinEmitterTest {
|package community.flock.wirespec.generated
|
|import community.flock.wirespec.Wirespec
|import kotlin.reflect.typeOf
|
|data class UUID(override val value: String): Wirespec.Refined {
| override fun toString() = value
Expand All @@ -54,6 +55,7 @@ class KotlinEmitterTest {
|package community.flock.wirespec.generated
|
|import community.flock.wirespec.Wirespec
|import kotlin.reflect.typeOf
|
|enum class TodoStatus (val label: String): Wirespec.Enum {
| OPEN("OPEN"),
Expand Down Expand Up @@ -153,24 +155,24 @@ class KotlinEmitterTest {
| fun <B> REQUEST_MAPPER(contentMapper: Wirespec.ContentMapper<B>) = { request: Wirespec.Request<B> ->
| when {
| request.content?.type == "application/json" -> contentMapper
| .read<Pet>(request.content!!, Wirespec.getType(Pet::class.java, false))
| .read<Pet>(request.content!!, typeOf<Pet>())
| .let { RequestApplicationJson(request.path, request.method, request.query, request.headers, it) }
| request.content?.type == "application/xml" -> contentMapper
| .read<Pet>(request.content!!, Wirespec.getType(Pet::class.java, false))
| .read<Pet>(request.content!!, typeOf<Pet>())
| .let { RequestApplicationXml(request.path, request.method, request.query, request.headers, it) }
| request.content?.type == "application/x-www-form-urlencoded" -> contentMapper
| .read<Pet>(request.content!!, Wirespec.getType(Pet::class.java, false))
| .read<Pet>(request.content!!, typeOf<Pet>())
| .let { RequestApplicationXWwwFormUrlencoded(request.path, request.method, request.query, request.headers, it) }
| else -> error("Cannot map request")
| }
| }
| fun <B> RESPONSE_MAPPER(contentMapper: Wirespec.ContentMapper<B>) = { response: Wirespec.Response<B> ->
| when {
| response.status == 200 && response.content?.type == "application/xml" -> contentMapper
| .read<Pet>(response.content!!, Wirespec.getType(Pet::class.java, false))
| .read<Pet>(response.content!!, typeOf<Pet>())
| .let { Response200ApplicationXml(response.status, response.headers, it) }
| response.status == 200 && response.content?.type == "application/json" -> contentMapper
| .read<Pet>(response.content!!, Wirespec.getType(Pet::class.java, false))
| .read<Pet>(response.content!!, typeOf<Pet>())
| .let { Response200ApplicationJson(response.status, response.headers, it) }
| response.status == 405 && response.content == null -> Response405Unit(response.status, response.headers, null)
| else -> error("Cannot map response with status ${'$'}{response.status}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package community.flock.wirespec.integration.jackson.kotlin.generated

import community.flock.wirespec.Wirespec
import kotlin.reflect.typeOf

data class TodoId(override val value: String): Wirespec.Refined {
override fun toString() = value
Expand Down

0 comments on commit 06ea1a9

Please sign in to comment.