Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Commit

Permalink
Dependency updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchallen committed Dec 10, 2021
1 parent 7252ca4 commit 2570829
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 208 deletions.
1 change: 1 addition & 0 deletions .java-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
openjdk64-16
46 changes: 0 additions & 46 deletions build.gradle

This file was deleted.

34 changes: 34 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.6.0" apply false
id("org.jmailen.kotlinter") version "3.7.0" apply false
id("com.github.ben-manes.versions") version "0.39.0"
}
subprojects {
group = "com.github.cs125-illinois"
version = "2021.12.0"
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_16.toString()
}
}
tasks.withType<Test> {
enableAssertions = true
useJUnitPlatform()
jvmArgs("-ea", "-Xmx1G", "-Xss256k", "--illegal-access=permit")
}
}
allprojects {
repositories {
mavenCentral()
}
}
tasks.dependencyUpdates {
fun String.isNonStable() = !(
listOf("RELEASE", "FINAL", "GA", "JRE").any { toUpperCase().contains(it) }
|| "^[0-9,.v-]+(-r)?$".toRegex().matches(this)
)
rejectVersionIf { candidate.version.isNonStable() }
gradleReleaseChannel = "current"
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
17 changes: 0 additions & 17 deletions library/build.gradle

This file was deleted.

47 changes: 47 additions & 0 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
plugins {
kotlin("jvm")
kotlin("kapt")
`maven-publish`
id("org.jmailen.kotlinter")
}
dependencies {
kaptTest("com.squareup.moshi:moshi-kotlin-codegen:1.13.0")

implementation("io.ktor:ktor-server-core:1.6.7")
implementation("com.squareup.moshi:moshi-kotlin:1.13.0")
implementation("com.squareup.okio:okio:3.0.0")

testImplementation(kotlin("test"))
testImplementation("com.google.truth:truth:1.1.3")
testImplementation("io.ktor:ktor-server-test-host:1.6.7")
}
tasks {
val sourcesJar by creating(Jar::class) {
archiveClassifier.set("sources")
from(sourceSets["main"].allSource)
}
artifacts {
add("archives", sourcesJar)
}
}
java {
withSourcesJar()
}
publishing {
publications {
create<MavenPublication>("mave") {
artifactId = "ktor-moshi"
from(components["java"])
}
}
}
kapt {
useBuildCache = true
includeCompileClasspath = false
javacOptions {
option("--illegal-access", "permit")
}
}
kotlin {
kotlinDaemonJvmArgs = listOf("-Dfile.encoding=UTF-8", "--illegal-access=permit")
}
37 changes: 20 additions & 17 deletions library/src/main/kotlin/com/ryanharter/ktor/moshi/MoshiConverter.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("unused")

package com.ryanharter.ktor.moshi

import com.squareup.moshi.Moshi
Expand All @@ -17,39 +19,40 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okio.buffer
import okio.source
import kotlin.reflect.jvm.jvmErasure

class MoshiConverter(private val moshi: Moshi = Moshi.Builder().build()) : ContentConverter {
override suspend fun convertForReceive(context: PipelineContext<ApplicationReceiveRequest, ApplicationCall>): Any? {
val request = context.subject
val channel = request.value as? ByteReadChannel ?: return null
val source = channel.toInputStream().source().buffer()
val type = request.type
return withContext(Dispatchers.IO) {
moshi.adapter(type.javaObjectType).fromJson(source)
override suspend fun convertForReceive(context: PipelineContext<ApplicationReceiveRequest, ApplicationCall>): Any? {
val request = context.subject
val channel = request.value as? ByteReadChannel ?: return null
val source = channel.toInputStream().source().buffer()
val type = request.typeInfo.jvmErasure
return withContext(Dispatchers.IO) {
moshi.adapter(type.javaObjectType).fromJson(source)
}
}
}

override suspend fun convertForSend(context: PipelineContext<Any, ApplicationCall>, contentType: ContentType, value: Any): Any? {
return TextContent(moshi.adapter(value.javaClass).toJson(value), contentType.withCharset(context.call.suitableCharset()))
}
override suspend fun convertForSend(context: PipelineContext<Any, ApplicationCall>, contentType: ContentType, value: Any): Any {
return TextContent(moshi.adapter(value.javaClass).toJson(value), contentType.withCharset(context.call.suitableCharset()))
}
}

/**
* Registers the supplied Moshi instance as a content converter for `application/json`
* data.
*/
fun ContentNegotiation.Configuration.moshi(moshi: Moshi = Moshi.Builder().build()) {
val converter = MoshiConverter(moshi)
register(ContentType.Application.Json, converter)
val converter = MoshiConverter(moshi)
register(ContentType.Application.Json, converter)
}

/**
* Creates a new Moshi instance and registers it as a content converter for
* `application/json` data. The supplied block is used to configure the builder.
*/
fun ContentNegotiation.Configuration.moshi(block: Moshi.Builder.() -> Unit) {
val builder = Moshi.Builder()
builder.apply(block)
val converter = MoshiConverter(builder.build())
register(ContentType.Application.Json, converter)
val builder = Moshi.Builder()
builder.apply(block)
val converter = MoshiConverter(builder.build())
register(ContentType.Application.Json, converter)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("SpellCheckingInspection")

package com.ryanharter.ktor.moshi

import com.google.common.truth.Truth.assertThat
Expand All @@ -19,90 +21,88 @@ import io.ktor.server.testing.contentType
import io.ktor.server.testing.handleRequest
import io.ktor.server.testing.setBody
import io.ktor.server.testing.withTestApplication
import org.junit.Test
import kotlin.test.fail
import kotlin.test.Test

class MoshiConverterTest {

@Test fun reflection() = withTestApplication {
application.install(ContentNegotiation) {
moshi {
this.add(KotlinJsonAdapterFactory())
}
}
application.routing {
val foo = Foo(id = 42, name = "Foosius")
get("/") {
call.respond(foo)
}
post("/") {
val request = call.receive<Foo>()
val text = request.toString()
call.respond(text)
}
}
@Test fun reflection() = withTestApplication {
application.install(ContentNegotiation) {
moshi {
this.add(KotlinJsonAdapterFactory())
}
}
application.routing {
val foo = Foo(id = 42, name = "Foosius")
get("/") {
call.respond(foo)
}
post("/") {
val request = call.receive<Foo>()
val text = request.toString()
call.respond(text)
}
}

handleRequest(HttpMethod.Get, "/") {
addHeader("Accept", "application/json")
}.response.let { response ->
assertThat(response.status()).isEqualTo(HttpStatusCode.OK)
assertThat(response.content).isNotNull()
assertThat(response.content).isEqualTo("""{"id":42,"name":"Foosius"}""")
assertThat(response.contentType()).isEqualTo(ContentType.Application.Json.withCharset(Charsets.UTF_8))
}
handleRequest(HttpMethod.Get, "/") {
addHeader("Accept", "application/json")
}.response.let { response ->
assertThat(response.status()).isEqualTo(HttpStatusCode.OK)
assertThat(response.content).isNotNull()
assertThat(response.content).isEqualTo("""{"id":42,"name":"Foosius"}""")
assertThat(response.contentType()).isEqualTo(ContentType.Application.Json.withCharset(Charsets.UTF_8))
}

handleRequest(HttpMethod.Post, "/") {
addHeader("Accept", "application/json")
addHeader("Content-Type", "application/json")
setBody("""{"id":43,"name":"Finnius"}""")
}.response.let { response ->
assertThat(response.status()).isEqualTo(HttpStatusCode.OK)
assertThat(response.content).isNotNull()
assertThat(response.content).isEqualTo("Foo(id=43, name=Finnius)")
assertThat(response.contentType()).isEqualTo(ContentType.Text.Plain.withCharset(Charsets.UTF_8))
handleRequest(HttpMethod.Post, "/") {
addHeader("Accept", "application/json")
addHeader("Content-Type", "application/json")
setBody("""{"id":43,"name":"Finnius"}""")
}.response.let { response ->
assertThat(response.status()).isEqualTo(HttpStatusCode.OK)
assertThat(response.content).isNotNull()
assertThat(response.content).isEqualTo("Foo(id=43, name=Finnius)")
assertThat(response.contentType()).isEqualTo(ContentType.Text.Plain.withCharset(Charsets.UTF_8))
}
}
}

@Test fun codegen() = withTestApplication {
application.install(ContentNegotiation) {
moshi { }
}
application.routing {
val bar = Bar(id = "bar-123", count = 50)
get("/") {
call.respond(bar)
}
post("/") {
val request = call.receive<Bar>()
val text = request.toString()
call.respond(text)
}
}
@Test fun codegen() = withTestApplication {
application.install(ContentNegotiation) {
moshi { }
}
application.routing {
val bar = Bar(id = "bar-123", count = 50)
get("/") {
call.respond(bar)
}
post("/") {
val request = call.receive<Bar>()
val text = request.toString()
call.respond(text)
}
}

handleRequest(HttpMethod.Get, "/") {
addHeader("Accept", "application/json")
}.response.let { response ->
assertThat(response.status()).isEqualTo(HttpStatusCode.OK)
assertThat(response.content).isNotNull()
assertThat(response.content).isEqualTo("""{"id":"bar-123","count":50}""")
assertThat(response.contentType()).isEqualTo(ContentType.Application.Json.withCharset(Charsets.UTF_8))
}
handleRequest(HttpMethod.Get, "/") {
addHeader("Accept", "application/json")
}.response.let { response ->
assertThat(response.status()).isEqualTo(HttpStatusCode.OK)
assertThat(response.content).isNotNull()
assertThat(response.content).isEqualTo("""{"id":"bar-123","count":50}""")
assertThat(response.contentType()).isEqualTo(ContentType.Application.Json.withCharset(Charsets.UTF_8))
}

handleRequest(HttpMethod.Post, "/") {
addHeader("Accept", "application/json")
addHeader("Content-Type", "application/json")
setBody("""{"id":"bar-543","count":-1}""")
}.response.let { response ->
assertThat(response.status()).isEqualTo(HttpStatusCode.OK)
assertThat(response.content).isNotNull()
assertThat(response.content).isEqualTo("Bar(id=bar-543, count=-1)")
assertThat(response.contentType()).isEqualTo(ContentType.Text.Plain.withCharset(Charsets.UTF_8))
handleRequest(HttpMethod.Post, "/") {
addHeader("Accept", "application/json")
addHeader("Content-Type", "application/json")
setBody("""{"id":"bar-543","count":-1}""")
}.response.let { response ->
assertThat(response.status()).isEqualTo(HttpStatusCode.OK)
assertThat(response.content).isNotNull()
assertThat(response.content).isEqualTo("Bar(id=bar-543, count=-1)")
assertThat(response.contentType()).isEqualTo(ContentType.Text.Plain.withCharset(Charsets.UTF_8))
}
}
}

}

data class Foo(val id: Int, val name: String)

@JsonClass(generateAdapter = true)
data class Bar(val id: String, val count: Int)
data class Bar(val id: String, val count: Int)
Loading

0 comments on commit 2570829

Please sign in to comment.