diff --git a/build.gradle.kts b/build.gradle.kts index 2ee2e280..7f212a07 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -20,6 +20,10 @@ application { applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment") } +tasks.withType { + useJUnitPlatform() +} + repositories { mavenCentral() } @@ -54,6 +58,7 @@ dependencies { implementation("io.ktor:ktor-client-core:$ktor_version") implementation("io.ktor:ktor-client-cio:$ktor_version") implementation("io.ktor:ktor-client-content-negotiation:$ktor_version") + testImplementation("org.junit.jupiter:junit-jupiter:5.8.1") } jib { diff --git a/src/main/kotlin/dev/usbharu/hideout/ap/Follow.kt b/src/main/kotlin/dev/usbharu/hideout/ap/Follow.kt index ef668610..b2ab16c6 100644 --- a/src/main/kotlin/dev/usbharu/hideout/ap/Follow.kt +++ b/src/main/kotlin/dev/usbharu/hideout/ap/Follow.kt @@ -13,4 +13,6 @@ open class Follow : Object{ this.`object` = `object` this.actor = actor } + + } diff --git a/src/main/kotlin/dev/usbharu/hideout/ap/JsonLd.kt b/src/main/kotlin/dev/usbharu/hideout/ap/JsonLd.kt index 2cda262c..b4749c7b 100644 --- a/src/main/kotlin/dev/usbharu/hideout/ap/JsonLd.kt +++ b/src/main/kotlin/dev/usbharu/hideout/ap/JsonLd.kt @@ -5,12 +5,13 @@ import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.core.TreeNode import com.fasterxml.jackson.databind.JsonDeserializer +import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.annotation.JsonDeserialize @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) open class JsonLd { @JsonProperty("@context") - @JsonDeserialize(using = ContextDeserializer::class) + @JsonDeserialize(contentUsing = ContextDeserializer::class) var context:List = emptyList() @JsonCreator @@ -21,8 +22,12 @@ open class JsonLd { protected constructor() } -public class ContextDeserializer : JsonDeserializer>() { - override fun deserialize(p0: com.fasterxml.jackson.core.JsonParser?, p1: com.fasterxml.jackson.databind.DeserializationContext?): List { - return emptyList() +public class ContextDeserializer : JsonDeserializer() { + override fun deserialize(p0: com.fasterxml.jackson.core.JsonParser?, p1: com.fasterxml.jackson.databind.DeserializationContext?): String { + val readTree : JsonNode = p0?.codec?.readTree(p0) ?: return "" + if (readTree.isObject) { + return "" + } + return readTree.asText() } } diff --git a/src/test/kotlin/dev/usbharu/hideout/ap/ContextDeserializerTest.kt b/src/test/kotlin/dev/usbharu/hideout/ap/ContextDeserializerTest.kt new file mode 100644 index 00000000..a9b933c5 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/ap/ContextDeserializerTest.kt @@ -0,0 +1,51 @@ +package dev.usbharu.hideout.ap + +import com.fasterxml.jackson.databind.DeserializationFeature +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import org.junit.jupiter.api.Assertions.* + +class ContextDeserializerTest { + + @org.junit.jupiter.api.Test + fun deserialize() { + //language=JSON + val s = """ + { + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", + "sensitive": "as:sensitive", + "Hashtag": "as:Hashtag", + "quoteUrl": "as:quoteUrl", + "toot": "http://joinmastodon.org/ns#", + "Emoji": "toot:Emoji", + "featured": "toot:featured", + "discoverable": "toot:discoverable", + "schema": "http://schema.org#", + "PropertyValue": "schema:PropertyValue", + "value": "schema:value", + "misskey": "https://misskey-hub.net/ns#", + "_misskey_content": "misskey:_misskey_content", + "_misskey_quote": "misskey:_misskey_quote", + "_misskey_reaction": "misskey:_misskey_reaction", + "_misskey_votes": "misskey:_misskey_votes", + "_misskey_talk": "misskey:_misskey_talk", + "isCat": "misskey:isCat", + "vcard": "http://www.w3.org/2006/vcard/ns#" + } + ], + "id": "https://test-misskey-v12.usbharu.dev/follows/9bg1zu54y7/9cydqvpjcn", + "type": "Follow", + "actor": "https://test-misskey-v12.usbharu.dev/users/9bg1zu54y7", + "object": "https://test-hideout.usbharu.dev/users/test3" +} + +""" + val readValue = jacksonObjectMapper().enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false).readValue(s) + println(readValue) + println(readValue.actor) + } +}