Skip to content

Commit

Permalink
Merge pull request #271 from basho/features/lrb/gh-266
Browse files Browse the repository at this point in the history
READY: Add checks to ensure Riak key is a String
  • Loading branch information
lukebakken authored Jun 10, 2016
2 parents a932e54 + be746b1 commit 42a8ccd
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
8 changes: 4 additions & 4 deletions lib/riak/bucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class Bucket
# @param [Client] client the {Riak::Client} for this bucket
# @param [String] name the name of the bucket
def initialize(client, name)
raise ArgumentError, t("client_type", :client => client.inspect) unless Client === client
raise ArgumentError, t("string_type", :string => name.inspect) unless String === name
raise ArgumentError, t('zero_length_bucket') if name == ''
fail ArgumentError, t('client_type', :client => client.inspect) unless Client === client
fail ArgumentError, t('string_type', :string => name.inspect) unless String === name
fail ArgumentError, t('zero_length_bucket') if name == ''
@client, @name = client, name
end

Expand Down Expand Up @@ -71,7 +71,7 @@ def keys(options = {}, &block)
# @raise [FailedRequest] if the new properties were not accepted by the Riakserver
# @see #n_value, #allow_mult, #r, #w, #dw, #rw
def props=(properties)
raise ArgumentError, t("hash_type", :hash => properties.inspect) unless Hash === properties
fail ArgumentError, t("hash_type", :hash => properties.inspect) unless Hash === properties
props.merge!(properties)
@client.set_bucket_props(self, properties)
props
Expand Down
3 changes: 2 additions & 1 deletion lib/riak/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ def get_many(pairs)

# Get an object. See Bucket#get
def get_object(bucket, key, options = {})
raise ArgumentError, t("zero_length_key") if key == ''
fail ArgumentError, t('zero_length_key') if key == ''
fail ArgumentError, t('string_type', :string => key) unless String === key
backend do |b|
b.fetch_object(bucket, key, options)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/riak/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Counter
# @param [Bucket] bucket the {Riak::Bucket} for this counter
# @param [String] key the name of the counter
def initialize(bucket, key)
raise ArgumentError, t("bucket_type", bucket: bucket.inspect) unless bucket.is_a? Bucket
raise ArgumentError, t("string_type", string: key.inspect) unless key.is_a? String
fail ArgumentError, t('bucket_type', bucket: bucket.inspect) unless Bucket === bucket
fail ArgumentError, t('string_type', string: key.inspect) unless String === key
@bucket, @key = bucket, key
@client = bucket.client

Expand Down Expand Up @@ -79,11 +79,11 @@ def decrement(amount = 1, options = {})

private
def validate_bucket
raise ArgumentError, t("counter.bucket_needs_allow_mult") unless bucket.allow_mult
fail ArgumentError, t("counter.bucket_needs_allow_mult") unless bucket.allow_mult
end

def validate_amount(amount)
raise ArgumentError, t("counter.increment_by_integer") unless amount.is_a? Integer
fail ArgumentError, t("counter.increment_by_integer") unless amount.is_a? Integer
end

def backend(&blk)
Expand Down
2 changes: 2 additions & 0 deletions lib/riak/robject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ def store(options = {})
fail Conflict, self if conflict?
fail ArgumentError, t('content_type_undefined') unless content_type.present?
fail ArgumentError, t('zero_length_key') if key == ''
# NB: key can be nil to indicate that Riak should generate one
fail ArgumentError, t('string_type', :string => key) unless key.nil? or String === key
@bucket.client.store_object(self, default(options))
self
end
Expand Down
6 changes: 6 additions & 0 deletions spec/integration/riak/encodings/kv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,11 @@

expect(binary_bucket.keys).to include random_binary_string
end

it 'throws an exception when non-string used as key' do
expect do
binary_bucket.get(1234)
end.to raise_error ArgumentError, /is not a String/
end
end
end
3 changes: 2 additions & 1 deletion spec/riak/robject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

describe "initialization" do
it "sets the bucket" do
@object = Riak::RObject.new(@bucket)
@object = Riak::RObject.new(@bucket, "bar")
expect(@object.bucket).to eq(@bucket)
end

Expand Down Expand Up @@ -293,6 +293,7 @@
end

it "passes quorum parameters and returnbody to the backend" do
@object.key = 'foo'
expect(@backend).to receive(:store_object).
with(@object,
returnbody: false,
Expand Down

0 comments on commit 42a8ccd

Please sign in to comment.