Skip to content
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

Fix string escaping problem #942

Merged
merged 1 commit into from
Jun 30, 2021
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
2 changes: 1 addition & 1 deletion client/src/main/scala/caliban/client/__Value.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object __Value {
override def toString: String = value
}
case class __StringValue(value: String) extends __Value {
override def toString: String = s""""${value.replace("\"", "\\\"")}""""
override def toString: String = Json.fromString(value).toString
}
case class __BooleanValue(value: Boolean) extends __Value {
override def toString: String = value.toString
Expand Down
25 changes: 25 additions & 0 deletions client/src/test/scala/caliban/client/ArgEncoderSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package caliban.client

import zio.test.Assertion.equalTo
import zio.test._
import zio.test.environment.TestEnvironment

object ArgEncoderSpec extends DefaultRunnableSpec {
override def spec: ZSpec[TestEnvironment, Any] =
suite("ArgEncoderSpec")(
suite("__StringValue")(
test("regular string") {
assert(ArgEncoder.string.encode("abcde who am i?").toString)(equalTo(""""abcde who am i?""""))
},
test("string with quotes") {
assert(ArgEncoder.string.encode("abcde \"who am i?\"").toString)(equalTo(""""abcde \"who am i?\"""""))
},
test("string with new line") {
assert(ArgEncoder.string.encode("abcde\n who\n am\n i\n").toString)(equalTo(""""abcde\n who\n am\n i\n""""))
},
test("string with null characters") {
assert(ArgEncoder.string.encode("abcde who am i\u0000").toString)(equalTo("\"abcde who am i\\u0000\""))
}
)
)
}