Skip to content

Fix: Fix Object ID decoding logic for uppercase HEX string #274

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

Merged
merged 1 commit into from
Apr 17, 2025
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 lib/bson/types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ defmodule BSON.ObjectId do
defp e(unquote(int)), do: unquote(char)
end

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

Expand Down
9 changes: 9 additions & 0 deletions test/bson/types_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule BSON.TypesTest do

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

test "inspect BSON.ObjectId" do
Expand Down Expand Up @@ -39,6 +40,14 @@ defmodule BSON.TypesTest do
end
end

test "BSON.ObjectId.decode!/1 for uppercase HEX" do
assert BSON.ObjectId.decode!(@string_uppercase) == @objectid

assert_raise FunctionClauseError, fn ->
BSON.ObjectId.decode!("")
end
end

test "BSON.ObjectId.encode/1" do
assert BSON.ObjectId.encode(@objectid) == {:ok, @string}
assert BSON.ObjectId.encode("") == :error
Expand Down