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

[0.17] Fix ClassCastException in document encoding #1161

Merged
merged 1 commit into from
Aug 21, 2023
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
18 changes: 12 additions & 6 deletions modules/json/src/smithy4s/http/json/SchemaVisitorJCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,18 @@ private[smithy4s] class SchemaVisitorJCodec(
case a: DArray =>
out.writeArrayStart()
a.value match {
case x: ArraySeq[Document] =>
val xs = x.unsafeArray.asInstanceOf[Array[Document]]
var i = 0
while (i < xs.length) {
encodeValue(xs(i), out)
i += 1
// short-circuiting on empty arrays to avoid the downcast to array of documents
// which has proven to be dangerous in Scala 3:
// https://github.com/disneystreaming/smithy4s/issues/1158
case x: ArraySeq[_] =>
if (x.isEmpty) ()
else {
val xs = x.unsafeArray.asInstanceOf[Array[Document]]
var i = 0
while (i < xs.length) {
encodeValue(xs(i), out)
i += 1
}
}
case xs =>
xs.foreach(encodeValue(_, out))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,18 @@ class SchemaVisitorJCodecTests() extends FunSuite {
expect.same(decoded, doc)
}

test("empty document arrays can be encoded (#1158)") {
val doc: Document = Document.array()
val documentJson = writeToString(doc)
val expected =
"""[]"""

val decoded = readFromString[Document](documentJson)

expect.same(documentJson, expected)
expect.same(decoded, doc)
}

test("Range checks are performed correctly") {
val json = """{"qty":0}"""
val result = util.Try(readFromString[RangeCheck](json))
Expand Down