Skip to content

Commit 2dfa1b5

Browse files
authored
SWIFT-973 Fix crash when encoding top level arrays to BSON (#35)
1 parent 9fb49e3 commit 2dfa1b5

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

Sources/BSON/BSONEncoder.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ public class BSONEncoder {
217217
if let mutableDict = boxedValue as? MutableDictionary {
218218
return .document(try mutableDict.toDocument())
219219
}
220+
if let mutableArray = boxedValue as? MutableArray {
221+
return .array(try mutableArray.toBSONArray())
222+
}
220223
return boxedValue.bson
221224
} catch let error as BSONErrorProtocol {
222225
throw EncodingError.invalidValue(

Tests/BSONTests/CodecTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,4 +830,11 @@ final class CodecTests: BSONTestCase {
830830
expect(try encoder.encode(IncorrectTopLevelEncode(.null))).to(throwError(CodecTests.invalidValueErr))
831831
expect(try encoder.encode(CorrectTopLevelEncode(.null))).to(equal(["x": [:]]))
832832
}
833+
834+
func testTopLevelArray() {
835+
let encoder = BSONEncoder()
836+
let decoder = BSONDecoder()
837+
expect(try encoder.encodeFragment([1, 2, 3])).to(equal([1, 2, 3]))
838+
expect(try decoder.decode([Int].self, fromBSON: [1, 2, 3])).to(equal([1, 2, 3]))
839+
}
833840
}

0 commit comments

Comments
 (0)