Skip to content

Commit 2d590d9

Browse files
MONGOID-5484 - Bugfix: Numeric strings should correctly evolve into BigDecimal (i.e. respecting map_big_decimal_to_decimal128) (#5468)
* Fix evolution of String --> BigDecimal * Update big_decimal_spec.rb * Fix string spec Co-authored-by: shields <[email protected]>
1 parent b9e65b8 commit 2d590d9

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

lib/mongoid/criteria/queryable/extensions/big_decimal.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def evolve(object)
3333
end
3434
# Always return on string for backwards compatibility with querying
3535
# string-backed BigDecimal fields.
36-
when BSON::Decimal128, String then obj
36+
when BSON::Decimal128 then obj
3737
else
3838
if obj.numeric?
3939
if Mongoid.map_big_decimal_to_decimal128

spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,14 @@
142142
end
143143

144144
context "when provided a numeric" do
145-
it "returns a string" do
145+
it "returns a BSON::Decimal128" do
146146
expect(described_class.evolve(1)).to eq(BSON::Decimal128.new('1'))
147147
end
148148
end
149149

150-
context "when provided a bogus value" do
151-
it "returns the value" do
152-
expect(described_class.evolve(:bogus)).to eq(:bogus)
150+
context "when provided a valid string" do
151+
it "returns a BSON::Decimal128" do
152+
expect(described_class.evolve('1')).to eq(BSON::Decimal128.new('1'))
153153
end
154154
end
155155

@@ -159,9 +159,9 @@
159159
end
160160
end
161161

162-
context "when provided a valid string" do
163-
it "returns the string" do
164-
expect(described_class.evolve("1")).to eq("1")
162+
context "when provided a bogus value" do
163+
it "returns the value" do
164+
expect(described_class.evolve(:bogus)).to eq(:bogus)
165165
end
166166
end
167167
end

spec/mongoid/criteria_spec.rb

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2756,13 +2756,33 @@ def self.ages; self; end
27562756
Band.create!(name: "Boards of Canada", sales: sales)
27572757
end
27582758

2759-
let(:from_db) do
2759+
it "cannot find values when querying using a BigDecimal value" do
27602760
Mongoid.map_big_decimal_to_decimal128 = true
2761-
Band.where(sales: sales.to_s).first
2761+
from_db = Band.where(sales: sales).first
2762+
expect(from_db).to eq(nil)
27622763
end
27632764

2764-
it "finds the document by the big decimal value" do
2765-
expect(from_db).to eq(band)
2765+
it "cannot find values when querying using a string value" do
2766+
Mongoid.map_big_decimal_to_decimal128 = true
2767+
from_db = Band.where(sales: sales.to_s).first
2768+
expect(from_db).to eq(nil)
2769+
end
2770+
2771+
context "after converting value" do
2772+
before do
2773+
Mongoid.map_big_decimal_to_decimal128 = true
2774+
band.set(sales: band.sales)
2775+
end
2776+
2777+
it "can find values when querying using a BigDecimal value" do
2778+
from_db = Band.where(sales: sales).first
2779+
expect(from_db).to eq(band)
2780+
end
2781+
2782+
it "can find values when querying using a string value" do
2783+
from_db = Band.where(sales: sales.to_s).first
2784+
expect(from_db).to eq(band)
2785+
end
27662786
end
27672787
end
27682788
end

0 commit comments

Comments
 (0)