Skip to content

Commit

Permalink
fix basic object comparison collision (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
mensfeld authored Jun 18, 2024
1 parent e760963 commit dbf7bfc
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Karafka core changelog

## 2.4.3 (2024-06-18)
- [Fix] Use `Object` instead of `BasicObject` for rule result comparison because of Time mismatch with BasicObject.

## 2.4.2 (2024-06-17)
- [Enhancement] Allow `karafka-rdkafka` `0.16.x` to be used since API compatible.

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
karafka-core (2.4.2)
karafka-core (2.4.3)
karafka-rdkafka (>= 0.15.0, < 0.17.0)

GEM
Expand Down
1 change: 1 addition & 0 deletions config/locales/errors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ en:
config:
missing: needs to be present
id_format: needs to be a String
test_format: needs to be nil
8 changes: 5 additions & 3 deletions lib/karafka/core/contractable/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Contract
# Constant representing a miss during dig
# We use it as a result value not to return an array with found object and a state to
# prevent additional array allocation
DIG_MISS = BasicObject.new
DIG_MISS = Object.new

private_constant :DIG_MISS

Expand Down Expand Up @@ -120,7 +120,9 @@ def validate!(data, error_class)
def validate_required(data, rule, errors)
for_checking = dig(data, rule.path)

if for_checking == DIG_MISS
# We need to compare `DIG_MISS` against stuff because of the ownership of the `#==`
# method
if DIG_MISS == for_checking
errors << [rule.path, :missing]
else
result = rule.validator.call(for_checking, data, errors, self)
Expand All @@ -140,7 +142,7 @@ def validate_required(data, rule, errors)
def validate_optional(data, rule, errors)
for_checking = dig(data, rule.path)

return if for_checking == DIG_MISS
return if DIG_MISS == for_checking

result = rule.validator.call(for_checking, data, errors, self)

Expand Down
2 changes: 1 addition & 1 deletion lib/karafka/core/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ module Karafka
module Core
# Current Karafka::Core version
# We follow the versioning schema of given Karafka version
VERSION = '2.4.2'
VERSION = '2.4.3'
end
end
14 changes: 14 additions & 0 deletions spec/lib/karafka/core/contractable/contract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
end

required(:id) { |id| id.is_a?(String) }

optional(:test) { |test| test == 5 }
end
end

Expand All @@ -29,6 +31,18 @@

it { expect { validation }.to raise_error(ArgumentError) }
end

context 'when optional data is not valid' do
let(:data) { { id: 1, test: Time.now } }

it { expect { validation }.to raise_error(ArgumentError) }
end

context 'when optional data is valid' do
let(:data) { { id: 1, test: nil } }

it { expect { validation }.to raise_error(ArgumentError) }
end
end

context 'when there are nested values in a contract' do
Expand Down

0 comments on commit dbf7bfc

Please sign in to comment.