Skip to content

RUBY-3117 investigate and fix test failures as a result of removing validating_keys #2620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/mongo/grid/file/chunk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ def initialize(document)
#
# @param [ BSON::ByteBuffer ] buffer The encoded BSON buffer to append to.
# @param [ true, false ] validating_keys Whether keys should be validated when serializing.
# This option is deprecated and will not be used. It will removed in version 3.0.
#
# @return [ String ] The raw BSON data.
#
# @since 2.0.0
def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = BSON::Config.validating_keys?)
def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = nil)
document.to_bson(buffer)
end

Expand Down
3 changes: 2 additions & 1 deletion lib/mongo/grid/file/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,12 @@ def update_md5(bytes)
#
# @param [ BSON::ByteBuffer ] buffer The encoded BSON buffer to append to.
# @param [ true, false ] validating_keys Whether keys should be validated when serializing.
# This option is deprecated and will not be used. It will removed in version 3.0.
#
# @return [ String ] The raw BSON data.
#
# @since 2.0.0
def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = BSON::Config.validating_keys?)
def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = nil)
if @client_md5 && !document[:md5]
document[:md5] = @client_md5.hexdigest
end
Expand Down
4 changes: 3 additions & 1 deletion lib/mongo/protocol/bit_vector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ def initialize(layout)
#
# @param buffer [ String ] Buffer to receive the serialized vector
# @param value [ Array<Symbol> ] Array of flags to encode
# @param [ true, false ] validating_keys Whether keys should be validated when serializing.
# This option is deprecated and will not be used. It will removed in version 3.0.
#
# @return [ String ] Buffer that received the serialized vector
def serialize(buffer, value, validating_keys = BSON::Config.validating_keys?)
def serialize(buffer, value, validating_keys = nil)
bits = 0
value.each { |flag| bits |= (@masks[flag] || 0) }
buffer.put_int32(bits)
Expand Down
23 changes: 3 additions & 20 deletions lib/mongo/protocol/caching_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,15 @@ def bson_type
#
# @param [ BSON::ByteBuffer ] buffer The encoded BSON buffer to append to.
# @param [ true, false ] validating_keys Whether keys should be validated when serializing.
# This option is deprecated and will not be used. It will removed in version 3.0.
#
# @return [ BSON::ByteBuffer ] The buffer with the encoded object.
def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = BSON::Config.validating_keys?)
def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = nil)
if !@bytes
@bytes = @hash.to_bson(BSON::ByteBuffer.new, validating_keys).to_s
elsif needs_validation?(validating_keys)
@validated = true
return @hash.to_bson(buffer, validating_keys)
@bytes = @hash.to_bson(BSON::ByteBuffer.new).to_s
end
@validated ||= validating_keys
buffer.put_bytes(@bytes)
end

private

# Checks the current value for validating keys, and whether or not this
# bson has been validated in the past, and decides if we need to recalculate
# the to_bson to check the validations.
#
# @param [ true, false ] validating_keys Whether keys should be validated when serializing.
#
# @return [ true, false ] Whether or not the bson needs to be recalculated
# with validation.
def needs_validation?(validating_keys)
!@validated && validating_keys
end
end
end
end
12 changes: 4 additions & 8 deletions lib/mongo/protocol/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,16 @@ def serialize_fields(buffer, max_bson_size = nil)
if field[:multi]
value.each do |item|
if field[:type].respond_to?(:size_limited?)
field[:type].serialize(buffer, item, max_bson_size, validating_keys?)
field[:type].serialize(buffer, item, max_bson_size)
else
field[:type].serialize(buffer, item, validating_keys?)
field[:type].serialize(buffer, item)
end
end
else
if field[:type].respond_to?(:size_limited?)
field[:type].serialize(buffer, value, max_bson_size, validating_keys?)
field[:type].serialize(buffer, value, max_bson_size)
else
field[:type].serialize(buffer, value, validating_keys?)
field[:type].serialize(buffer, value)
end
end
end
Expand Down Expand Up @@ -456,10 +456,6 @@ def self.deserialize_field(message, io, field, options = {})
field[:type].deserialize(io, options)
)
end

def validating_keys?
@options[:validating_keys] if @options
end
end
end
end
1 change: 1 addition & 0 deletions lib/mongo/protocol/msg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Msg < Message
# @option options [ true, false ] validating_keys Whether keys should be
# validated for being valid document keys (i.e. not begin with $ and
# not contain dots).
# This option is deprecated and will not be used. It will removed in version 3.0.
#
# @api private
#
Expand Down
41 changes: 24 additions & 17 deletions lib/mongo/protocol/serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ module Header
#
# @param buffer [ String ] Buffer to receive the serialized value.
# @param value [ String ] Header value to be serialized.
# @param [ true, false ] validating_keys Whether keys should be validated when serializing.
# This option is deprecated and will not be used. It will removed in version 3.0.
#
# @return [ String ] Buffer with serialized value.
def self.serialize(buffer, value, validating_keys = BSON::Config.validating_keys?)
def self.serialize(buffer, value, validating_keys = nil)
buffer.put_bytes(value.pack(HEADER_PACK))
end

Expand All @@ -80,7 +82,7 @@ module CString
# @param value [ String ] The string to be serialized.
#
# @return [ String ] Buffer with serialized value.
def self.serialize(buffer, value, validating_keys = BSON::Config.validating_keys?)
def self.serialize(buffer, value, validating_keys = nil)
buffer.put_cstring(value)
end
end
Expand All @@ -96,7 +98,7 @@ module Zero
# @param value [ Fixnum ] Ignored value.
#
# @return [ String ] Buffer with serialized value.
def self.serialize(buffer, value, validating_keys = BSON::Config.validating_keys?)
def self.serialize(buffer, value, validating_keys = nil)
buffer.put_int32(ZERO)
end
end
Expand All @@ -112,7 +114,7 @@ module Int32
# @param value [ Integer | BSON::Int32 ] 32-bit integer to be serialized.
#
# @return [String] Buffer with serialized value.
def self.serialize(buffer, value, validating_keys = BSON::Config.validating_keys?)
def self.serialize(buffer, value, validating_keys = nil)
if value.is_a?(BSON::Int32)
if value.respond_to?(:value)
# bson-ruby >= 4.6.0
Expand Down Expand Up @@ -146,7 +148,7 @@ module Int64
# @param value [ Integer | BSON::Int64 ] 64-bit integer to be serialized.
#
# @return [ String ] Buffer with serialized value.
def self.serialize(buffer, value, validating_keys = BSON::Config.validating_keys?)
def self.serialize(buffer, value, validating_keys = nil)
if value.is_a?(BSON::Int64)
if value.respond_to?(:value)
# bson-ruby >= 4.6.0
Expand Down Expand Up @@ -182,19 +184,20 @@ module Sections
# @param [ Array<Hash, BSON::Document> ] value The sections to be serialized.
# @param [ Fixnum ] max_bson_size The max bson size of documents in the sections.
# @param [ true, false ] validating_keys Whether to validate document keys.
# This option is deprecated and will not be used. It will removed in version 3.0.
#
# @return [ BSON::ByteBuffer ] Buffer with serialized value.
#
# @since 2.5.0
def self.serialize(buffer, value, max_bson_size = nil, validating_keys = BSON::Config.validating_keys?)
def self.serialize(buffer, value, max_bson_size = nil, validating_keys = nil)
value.each do |section|
case section[:type]
when PayloadZero::TYPE
PayloadZero.serialize(buffer, section[:payload], max_bson_size, false)
PayloadZero.serialize(buffer, section[:payload], max_bson_size)
when nil
PayloadZero.serialize(buffer, section[:payload], max_bson_size, false)
PayloadZero.serialize(buffer, section[:payload], max_bson_size)
when PayloadOne::TYPE
PayloadOne.serialize(buffer, section[:payload], max_bson_size, validating_keys)
PayloadOne.serialize(buffer, section[:payload], max_bson_size)
else
raise Error::UnknownPayloadType.new(section[:type])
end
Expand Down Expand Up @@ -259,13 +262,14 @@ module PayloadZero
# @param [ BSON::Document, Hash ] value The object to serialize.
# @param [ Fixnum ] max_bson_size The max bson size of documents in the section.
# @param [ true, false ] validating_keys Whether to validate document keys.
# This option is deprecated and will not be used. It will removed in version 3.0.
#
# @return [ BSON::ByteBuffer ] Buffer with serialized value.
#
# @since 2.5.0
def self.serialize(buffer, value, max_bson_size = nil, validating_keys = BSON::Config.validating_keys?)
def self.serialize(buffer, value, max_bson_size = nil, validating_keys = nil)
buffer.put_byte(TYPE_BYTE)
Serializers::Document.serialize(buffer, value, max_bson_size, validating_keys)
Serializers::Document.serialize(buffer, value, max_bson_size)
end

# Deserializes a section of payload type 0 of an OP_MSG from the IO stream.
Expand Down Expand Up @@ -307,17 +311,18 @@ module PayloadOne
# @param [ BSON::Document, Hash ] value The object to serialize.
# @param [ Fixnum ] max_bson_size The max bson size of documents in the section.
# @param [ true, false ] validating_keys Whether to validate document keys.
# This option is deprecated and will not be used. It will removed in version 3.0.
#
# @return [ BSON::ByteBuffer ] Buffer with serialized value.
#
# @since 2.5.0
def self.serialize(buffer, value, max_bson_size = nil, validating_keys = BSON::Config.validating_keys?)
def self.serialize(buffer, value, max_bson_size = nil, validating_keys = nil)
buffer.put_byte(TYPE_BYTE)
start = buffer.length
buffer.put_int32(0) # hold for size
buffer.put_cstring(value[:identifier])
value[:sequence].each do |document|
Document.serialize(buffer, document, max_bson_size, validating_keys)
Document.serialize(buffer, document, max_bson_size)
end
buffer.replace_int32(start, buffer.length - start)
end
Expand Down Expand Up @@ -356,9 +361,9 @@ module Document
# @param value [ Hash ] Document to serialize as BSON.
#
# @return [ String ] Buffer with serialized value.
def self.serialize(buffer, value, max_bson_size = nil, validating_keys = BSON::Config.validating_keys?)
def self.serialize(buffer, value, max_bson_size = nil, validating_keys = nil)
start_size = buffer.length
value.to_bson(buffer, validating_keys)
value.to_bson(buffer)
serialized_size = buffer.length - start_size
if max_bson_size && serialized_size > max_bson_size
raise Error::MaxBSONSize,
Expand Down Expand Up @@ -401,11 +406,12 @@ module Byte
# @param [ BSON::ByteBuffer ] buffer Buffer to receive the single byte.
# @param [ String ] value The byte to write to the buffer.
# @param [ true, false ] validating_keys Whether to validate keys.
# This option is deprecated and will not be used. It will removed in version 3.0.
#
# @return [ BSON::ByteBuffer ] Buffer with serialized value.
#
# @since 2.5.0
def self.serialize(buffer, value, validating_keys = BSON::Config.validating_keys?)
def self.serialize(buffer, value, validating_keys = nil)
buffer.put_byte(value)
end

Expand All @@ -432,11 +438,12 @@ module Bytes
# @param [ BSON::ByteBuffer ] buffer Buffer to receive the bytes.
# @param [ String ] value The bytes to write to the buffer.
# @param [ true, false ] validating_keys Whether to validate keys.
# This option is deprecated and will not be used. It will removed in version 3.0.
#
# @return [ BSON::ByteBuffer ] Buffer with serialized value.
#
# @since 2.5.0
def self.serialize(buffer, value, validating_keys = BSON::Config.validating_keys?)
def self.serialize(buffer, value, validating_keys = nil)
buffer.put_bytes(value)
end

Expand Down
46 changes: 0 additions & 46 deletions spec/mongo/protocol/caching_hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,50 +34,4 @@
end
end
end

describe "#needs_validation?" do
max_bson_version "4.99.99"

before do
hash.to_bson(BSON::ByteBuffer.new, validation)
end

let(:needs_validation?) do
hash.send(:needs_validation?, validating_keys)
end

context "when calling to_bson without validation" do
let(:validation) { false }

context "validating_keys is true" do
let(:validating_keys) { true }

it "is true" do
expect(needs_validation?).to be true
end
end

context "validating_keys is false" do
let(:validating_keys) { false }

it "is false" do
expect(needs_validation?).to be false
end
end
end

context "when calling to_bson with validation" do
let(:validation) { true }

[true, false].each do |b|
context "validating_keys is #{b}" do
let(:validating_keys) { b }

it "is false" do
expect(needs_validation?).to be false
end
end
end
end
end
end
6 changes: 2 additions & 4 deletions spec/mongo/protocol/msg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,8 @@
{ validating_keys: true }
end

it 'checks the sequence document keys' do
expect {
message.serialize
}.to raise_exception(BSON::String::IllegalKey)
it 'does not check the sequence document keys' do
expect(message.serialize).to be_a(BSON::ByteBuffer)
end
end

Expand Down