Skip to content

Commit

Permalink
Accept integers in scientific notation in JSON decoder (#392)
Browse files Browse the repository at this point in the history
Required for compliance tests.
  • Loading branch information
v0idpwn authored Jan 2, 2025
1 parent 509460a commit db54215
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/protobuf/json/decode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,18 @@ defmodule Protobuf.JSON.Decode do

defp parse_int(string) do
case Integer.parse(string) do
{int, ""} -> {:ok, int}
_ -> :error
{int, ""} ->
{:ok, int}

_ ->
# We accept integers in scientific notation as well:
case Float.parse(string) do
{float, ""} ->
parse_float_as_int(float)

_ ->
:error
end
end
end

Expand Down
6 changes: 6 additions & 0 deletions test/protobuf/conformance_regressions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ defmodule Protobuf.ConformanceRegressionsTest do

@describetag message_type: "protobuf_test_messages.proto3.TestAllTypesProto3"

test "Required.Proto3.JsonInput.Int32FieldQuotedExponentialValue.JsonOutput" do
mod = ProtobufTestMessages.Proto3.TestAllTypesProto3
problematic_payload = ~S({"optionalInt32": "1e5"})
assert %{optional_int32: 100_000} = Protobuf.JSON.decode!(problematic_payload, mod)
end

test "Recommended.Proto3.JsonInput.NullValueInOtherOneofNewFormat.Validator",
%{message_mod: message_mod} do
json = "{\"oneofNullValue\": null}"
Expand Down

0 comments on commit db54215

Please sign in to comment.