Skip to content

Commit fa7890b

Browse files
Fix: Fix Object ID decoding logic for uppercase HEX string (#274)
Co-authored-by: Kirill Davydov <Kirill Davydov>
1 parent 908acef commit fa7890b

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/bson/types.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ defmodule BSON.ObjectId do
124124
defp e(unquote(int)), do: unquote(char)
125125
end
126126

127-
for {char, int} <- Enum.with_index(?A..?F) do
127+
for {char, int} <- Enum.with_index(?A..?F, 10) do
128128
defp d(unquote(char)), do: unquote(int)
129129
end
130130

test/bson/types_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ defmodule BSON.TypesTest do
1111

1212
@objectid %BSON.ObjectId{value: <<29, 32, 69, 244, 101, 119, 228, 28, 61, 24, 21, 215>>}
1313
@string "1d2045f46577e41c3d1815d7"
14+
@string_uppercase "1D2045F46577E41C3D1815D7"
1415
@timestamp DateTime.from_unix!(488_654_324)
1516

1617
test "inspect BSON.ObjectId" do
@@ -39,6 +40,14 @@ defmodule BSON.TypesTest do
3940
end
4041
end
4142

43+
test "BSON.ObjectId.decode!/1 for uppercase HEX" do
44+
assert BSON.ObjectId.decode!(@string_uppercase) == @objectid
45+
46+
assert_raise FunctionClauseError, fn ->
47+
BSON.ObjectId.decode!("")
48+
end
49+
end
50+
4251
test "BSON.ObjectId.encode/1" do
4352
assert BSON.ObjectId.encode(@objectid) == {:ok, @string}
4453
assert BSON.ObjectId.encode("") == :error

0 commit comments

Comments
 (0)