Skip to content

Commit db54215

Browse files
authored
Accept integers in scientific notation in JSON decoder (#392)
Required for compliance tests.
1 parent 509460a commit db54215

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/protobuf/json/decode.ex

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,18 @@ defmodule Protobuf.JSON.Decode do
376376

377377
defp parse_int(string) do
378378
case Integer.parse(string) do
379-
{int, ""} -> {:ok, int}
380-
_ -> :error
379+
{int, ""} ->
380+
{:ok, int}
381+
382+
_ ->
383+
# We accept integers in scientific notation as well:
384+
case Float.parse(string) do
385+
{float, ""} ->
386+
parse_float_as_int(float)
387+
388+
_ ->
389+
:error
390+
end
381391
end
382392
end
383393

test/protobuf/conformance_regressions_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ defmodule Protobuf.ConformanceRegressionsTest do
8383

8484
@describetag message_type: "protobuf_test_messages.proto3.TestAllTypesProto3"
8585

86+
test "Required.Proto3.JsonInput.Int32FieldQuotedExponentialValue.JsonOutput" do
87+
mod = ProtobufTestMessages.Proto3.TestAllTypesProto3
88+
problematic_payload = ~S({"optionalInt32": "1e5"})
89+
assert %{optional_int32: 100_000} = Protobuf.JSON.decode!(problematic_payload, mod)
90+
end
91+
8692
test "Recommended.Proto3.JsonInput.NullValueInOtherOneofNewFormat.Validator",
8793
%{message_mod: message_mod} do
8894
json = "{\"oneofNullValue\": null}"

0 commit comments

Comments
 (0)