Skip to content

fix: correct self reference serialization #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import com.google.gson.*
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import spp.probe.services.common.serialize.RuntimeClassNameTypeAdapterFactory
import spp.probe.services.common.serialize.CappedTypeAdapterFactory
import spp.probe.services.common.serialize.RuntimeClassNameTypeAdapterFactory
import java.io.IOException
import java.io.OutputStream
import java.util.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package spp.probe.services.common.serialize

import com.google.gson.*
import com.google.gson.internal.Streams
import com.google.gson.internal.bind.JsogRegistry
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import org.apache.skywalking.apm.agent.core.logging.api.LogManager
import java.io.IOException

/**
Expand Down Expand Up @@ -113,6 +115,9 @@ import java.io.IOException
*/
class RuntimeClassNameTypeAdapterFactory<T> private constructor(baseType: Class<*>?, typeFieldName: String?) :
TypeAdapterFactory {

private val log = LogManager.getLogger(RuntimeClassNameTypeAdapterFactory::class.java)

private val baseType: Class<*>
private val typeFieldName: String
private val labelToSubtype: MutableMap<String, Class<*>> = LinkedHashMap()
Expand Down Expand Up @@ -224,6 +229,23 @@ class RuntimeClassNameTypeAdapterFactory<T> private constructor(baseType: Class<
Streams.write(jsonObject, out)
} else {
val clone = JsonObject()

//search for self-references
try {
srcType.declaredFields.forEach {
it.isAccessible = true
val fieldValue = it.get(value)
if (fieldValue === value) {
val selfRef = JsonObject()
selfRef.addProperty("@ref", JsogRegistry.get().geId(value))
selfRef.addProperty("@class", value.javaClass.name)
clone.add(it.name, selfRef)
}
}
} catch (e: Throwable) {
log.error("Error while serializing self-references", e)
}

clone.add(typeFieldName, JsonPrimitive(label))
for ((key, value1) in jsonObject.entrySet()) {
clone.add(key, value1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Source++, the open-source live coding platform.
* Copyright (C) 2022 CodeBrig, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package spp.probe.services.common.serialize

import io.vertx.core.json.JsonObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import spp.probe.services.common.ModelSerializer

class JsogTest {

class RootSelfRef {
var self: RootSelfRef? = null
}

@Test
fun testRootSelfRef() {
val rootSelfRef = RootSelfRef()
rootSelfRef.self = rootSelfRef
val json = ModelSerializer.INSTANCE.toExtendedJson(rootSelfRef)
val jsonObject = JsonObject(json)

assertEquals(3, jsonObject.size())
assertTrue(jsonObject.containsKey("@class"))
assertTrue(jsonObject.containsKey("@id"))
assertTrue(jsonObject.containsKey("self"))

val self = jsonObject.getJsonObject("self")
assertEquals(2, self.size())
assertTrue(self.containsKey("@class"))
assertTrue(self.containsKey("@ref"))
assertEquals(self.getString("@ref"), jsonObject.getString("@id"))
}

class InnerSelfRef {
class InnerSelfRef2 {
var selfRef: InnerSelfRef? = null
}

var self2: InnerSelfRef2? = null
}

@Test
fun testInnerSelfRef() {
val innerSelfRef = InnerSelfRef()
innerSelfRef.self2 = InnerSelfRef.InnerSelfRef2().apply { this.selfRef = innerSelfRef }
val json = ModelSerializer.INSTANCE.toExtendedJson(innerSelfRef)
val jsonObject = JsonObject(json)

assertEquals(3, jsonObject.size())
assertTrue(jsonObject.containsKey("@class"))
assertTrue(jsonObject.containsKey("@id"))
assertTrue(jsonObject.containsKey("self2"))

val self2 = jsonObject.getJsonObject("self2")
assertEquals(3, self2.size())
assertTrue(self2.containsKey("@class"))
assertTrue(self2.containsKey("@id"))
assertTrue(self2.containsKey("selfRef"))

val selfRef = self2.getJsonObject("selfRef")
assertEquals(2, selfRef.size())
assertTrue(selfRef.containsKey("@class"))
assertTrue(selfRef.containsKey("@ref"))
assertEquals(selfRef.getString("@ref"), jsonObject.getString("@id"))
}
}