nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: elixir-protobuf/protobuf
Pull request #385 (2024-12-05 18:37:30)

Decode integer-formatted JSON numbers as float (#364)
JSON has a single type for numbers. The same number can be written as `42` or `42.0`. Without additional context, this number should be valid in any of the Protobuf numeric types. For reference, the `protobuf` Python library is totally fine decoding `{ "foo": 42 }` with either a `float` or an `int32` Protobuf field.


Patch:
diff --git a/lib/protobuf/json/decode.ex b/lib/protobuf/json/decode.ex
index 1fbaf23..f12df6c 100644
--- a/lib/protobuf/json/decode.ex
+++ b/lib/protobuf/json/decode.ex
@@ -392,6 +392,7 @@ defmodule Protobuf.JSON.Decode do
   end
 
   defp decode_float(float) when is_float(float), do: {:ok, float}
+  defp decode_float(integer) when is_integer(integer), do: {:ok, integer / 1}
   defp decode_float(string) when is_binary(string), do: parse_float(string)
   defp decode_float(_bad), do: :error
 


Test patch:
diff --git a/test/protobuf/json/decode_test.exs b/test/protobuf/json/decode_test.exs
index 924e6cc..8684df0 100644
--- a/test/protobuf/json/decode_test.exs
+++ b/test/protobuf/json/decode_test.exs
@@ -238,6 +238,10 @@ defmodule Protobuf.JSON.DecodeTest do
       data = %{"float" => 1.23e3, "double" => 1.23e-2}
       decoded = %Scalars{float: 1230.0, double: 0.0123}
       assert decode(data, Scalars) == {:ok, decoded}
+
+      data = %{"float" => 5, "double" => 9}
+      decoded = %Scalars{float: 5.0, double: 9.0}
+      assert decode(data, Scalars) == {:ok, decoded}
     end
 
     test "constants are valid" do
@@ -279,8 +283,8 @@ defmodule Protobuf.JSON.DecodeTest do
     end
 
     test "other types are invalid" do
-      data = %{"float" => 5}
-      msg = "Field 'float' has an invalid floating point (5)"
+      data = %{"float" => <<1::15>>}
+      msg = "Field 'float' has an invalid floating point (<<0, 1::size(7)>>)"
       assert decode(data, Scalars) == error(msg)
 
       data = %{"double" => true}
Repository: elixir-protobuf/protobuf
Pull request #391 (2024-12-30 20:15:20)

Fix decoding of maps to match conformance tests
Different from the previous fixes in #390, I'm not 100% sure about this one. Feels like it could/should be solved in a different layer, but I didn't find a better way to do it without breaking anything else.


Patch:
diff --git a/lib/protobuf/decoder.ex b/lib/protobuf/decoder.ex
index 9da9f54..b8609c7 100644
--- a/lib/protobuf/decoder.ex
+++ b/lib/protobuf/decoder.ex
@@ -233,8 +233,17 @@ defmodule Protobuf.Decoder do
     %FieldProps{type: type, map?: map?, oneof: oneof, name_atom: name_atom, repeated?: repeated?} =
       prop
 
-    embedded_msg = decode(bin, type)
-    val = if map?, do: %{embedded_msg.key => embedded_msg.value}, else: embedded_msg
+    embed_msg = decode(bin, type)
+
+    val =
+      if map? do
+        key = if is_nil(embed_msg.key), do: map_default(prop, :key), else: embed_msg.key
+        value = if is_nil(embed_msg.value), do: map_default(prop, :value), else: embed_msg.value
+        %{key => value}
+      else
+        embed_msg
+      end
+
     val = if oneof, do: {name_atom, val}, else: val
 
     cond do
@@ -255,6 +264,15 @@ defmodule Protobuf.Decoder do
     end
   end
 
+  defp map_default(prop, key_or_value) do
+    prop.type.__message_props__().field_props
+    |> Enum.find(fn {_key, field_props} -> field_props.name_atom == key_or_value end)
+    |> then(fn {_key, field_props} ->
+      # Conformance only works when we use proto3 defaults here, even for proto2...
+      Protobuf.DSL.field_default(:proto3, field_props)
+    end)
+  end
+
   defp deep_merge(_oneof1 = {tag1, val1}, oneof2 = {tag2, val2}, props) do
     if tag1 == tag2 do
       # If the field is a oneof, we merge its value and keep the tag.


Test patch:
diff --git a/test/protobuf/conformance_regressions_test.exs b/test/protobuf/conformance_regressions_test.exs
index 441f377..dc5e992 100644
--- a/test/protobuf/conformance_regressions_test.exs
+++ b/test/protobuf/conformance_regressions_test.exs
@@ -83,6 +83,12 @@ defmodule Protobuf.ConformanceRegressionsTest do
 
     @describetag message_type: "protobuf_test_messages.proto3.TestAllTypesProto3"
 
+    test "Required.Proto2.ProtobufInput.ValidDataMap.INT32.INT32.MissingDefault.JsonOutput" do
+      mod = ProtobufTestMessages.Proto2.TestAllTypesProto2
+      problematic_payload = <<194, 3, 0>>
+      assert %{map_int32_int32: %{0 => 0}} = mod.decode(problematic_payload)
+    end
+    
     test "Required.Proto3.JsonInput.Int32FieldQuotedExponentialValue.JsonOutput" do
       mod = ProtobufTestMessages.Proto3.TestAllTypesProto3
       problematic_payload = ~S({"optionalInt32": "1e5"})
Repository: elixirmoney/money
Pull request #220 (2024-05-14 18:28:28)

#219 Support Decimal result in Ecto load.
This addresses #219 to support cases where Ecto may return a non-integer type.  This can arise when using the bigint type in PostgreSQL.

Nitrino: @aglassman Thanks!

Patch:
diff --git a/lib/money/ecto/amount_type.ex b/lib/money/ecto/amount_type.ex
index 8d6e432..1ca412f 100644
--- a/lib/money/ecto/amount_type.ex
+++ b/lib/money/ecto/amount_type.ex
@@ -67,6 +67,7 @@ if Code.ensure_loaded?(Ecto.Type) do
 
     @spec load(integer()) :: {:ok, Money.t()}
     def load(int) when is_integer(int), do: {:ok, Money.new(int)}
+    def load(%Decimal{} = decimal), do: {:ok, decimal |> Decimal.to_integer() |> Money.new()}
 
     @spec dump(integer() | Money.t()) :: {:ok, integer()}
     def dump(int) when is_integer(int), do: {:ok, int}


Test patch:
diff --git a/test/money/ecto/amount_type_test.exs b/test/money/ecto/amount_type_test.exs
index a62375e..1d576f9 100644
--- a/test/money/ecto/amount_type_test.exs
+++ b/test/money/ecto/amount_type_test.exs
@@ -56,6 +56,10 @@ defmodule Money.Ecto.Amount.TypeTest do
     assert Type.load(1000) == {:ok, Money.new(1000, :GBP)}
   end
 
+  test "load/1 decimal" do
+    assert Type.load(Decimal.new("1000")) == {:ok, Money.new(1000, :GBP)}
+  end
+
   test "dump/1 integer" do
     assert Type.dump(1000) == {:ok, 1000}
   end