From dc17e60da5b0cb7f109a5c27eee88b4ba6cfda58 Mon Sep 17 00:00:00 2001 From: "hyowoo.kim" Date: Tue, 16 Jan 2024 13:54:01 +0900 Subject: [PATCH 1/2] Handle escape string for strings containing quotes --- .../dev/yorkie/document/json/EscapeString.kt | 1 - .../yorkie/document/json/JsonStringifier.kt | 2 +- .../dev/yorkie/document/DocumentTest.kt | 38 +++++++++++++++++++ .../dev/yorkie/json/StringEscapingTest.kt | 2 +- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/yorkie/src/main/kotlin/dev/yorkie/document/json/EscapeString.kt b/yorkie/src/main/kotlin/dev/yorkie/document/json/EscapeString.kt index e317d699a..340827d6d 100644 --- a/yorkie/src/main/kotlin/dev/yorkie/document/json/EscapeString.kt +++ b/yorkie/src/main/kotlin/dev/yorkie/document/json/EscapeString.kt @@ -6,7 +6,6 @@ package dev.yorkie.document.json internal fun escapeString(str: String): String { return str.replace("\\", "\\\\") .replace("\"", "\\\"") - .replace("\'", "\\'") .replace("\n", "\\n") .replace("\r", "\\r") .replace("\b", "\\b") diff --git a/yorkie/src/main/kotlin/dev/yorkie/document/json/JsonStringifier.kt b/yorkie/src/main/kotlin/dev/yorkie/document/json/JsonStringifier.kt index 821a1f4d9..4d6a3d965 100644 --- a/yorkie/src/main/kotlin/dev/yorkie/document/json/JsonStringifier.kt +++ b/yorkie/src/main/kotlin/dev/yorkie/document/json/JsonStringifier.kt @@ -49,7 +49,7 @@ internal object JsonStringifier { is CrdtObject -> { buffer.append("{") forEach { (key, value) -> - buffer.append(""""$key":""") + buffer.append(""""${escapeString(key)}":""") value.toJsonStringInternal(buffer) if (key != last().first) { buffer.append(",") diff --git a/yorkie/src/test/kotlin/dev/yorkie/document/DocumentTest.kt b/yorkie/src/test/kotlin/dev/yorkie/document/DocumentTest.kt index a851ebe40..0f7fe47cc 100644 --- a/yorkie/src/test/kotlin/dev/yorkie/document/DocumentTest.kt +++ b/yorkie/src/test/kotlin/dev/yorkie/document/DocumentTest.kt @@ -1,5 +1,6 @@ package dev.yorkie.document +import com.google.gson.JsonParser import dev.yorkie.assertJsonContentEquals import dev.yorkie.document.json.JsonArray import dev.yorkie.document.json.JsonText @@ -265,4 +266,41 @@ class DocumentTest { assertEquals(0, target.garbageLength) } } + + @Test + fun `should handle escape string for strings containing single quotes`() { + runTest { + target.updateAsync { root, _ -> + root["str"] = "I'm Yorkie" + }.await() + assertEquals("""{"str":"I'm Yorkie"}""", target.toJson()) + assertEquals( + "I'm Yorkie", + JsonParser.parseString(target.toJson()).asJsonObject.get("str").asString, + ) + + target.updateAsync { root, _ -> + root["str"] = """I\\'m Yorkie""" + }.await() + assertEquals("""{"str":"I\\\\'m Yorkie"}""", target.toJson()) + assertEquals( + """I\\'m Yorkie""", + JsonParser.parseString(target.toJson()).asJsonObject.get("str").asString, + ) + } + } + + @Test + fun `should handle escape string for object keys`() { + runTest { + target.updateAsync { root, _ -> + root["""it"s"""] = "Yorkie" + }.await() + assertEquals("""{"it\"s":"Yorkie"}""", target.toJson()) + assertEquals( + "Yorkie", + JsonParser.parseString(target.toJson()).asJsonObject.get("""it"s""").asString, + ) + } + } } diff --git a/yorkie/src/test/kotlin/dev/yorkie/json/StringEscapingTest.kt b/yorkie/src/test/kotlin/dev/yorkie/json/StringEscapingTest.kt index c0866f453..5104726b6 100644 --- a/yorkie/src/test/kotlin/dev/yorkie/json/StringEscapingTest.kt +++ b/yorkie/src/test/kotlin/dev/yorkie/json/StringEscapingTest.kt @@ -9,7 +9,7 @@ class StringEscapingTest { @Test fun `should escape a string with case1`() { val target = "1\\2\"3'4\n5\r6\t7\b8\u000C9\u20280\u2029" - val escaped = "1\\\\2\\\"3\\'4\\n5\\r6\\t7\\b8\\f9\\u20280\\u2029" + val escaped = "1\\\\2\\\"3'4\\n5\\r6\\t7\\b8\\f9\\u20280\\u2029" assertEquals(escaped, escapeString(target)) } From 5f002c9f7fb81277f912aad1d0c71d36cf9d0ee7 Mon Sep 17 00:00:00 2001 From: "hyowoo.kim" Date: Tue, 16 Jan 2024 16:24:34 +0900 Subject: [PATCH 2/2] Handle escape string for attribute keys --- .idea/inspectionProfiles/Project_Default.xml | 1 + .../dev/yorkie/document/crdt/TextInfo.kt | 2 +- .../dev/yorkie/document/crdt/TextValueTest.kt | 29 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 yorkie/src/test/kotlin/dev/yorkie/document/crdt/TextValueTest.kt diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index bdb1b11bb..163aa4ffe 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -1,6 +1,7 @@