bson 4.0.4-java → 4.1.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2acb70e11a8426afd8b5b1269f34f4bd0fe73b2e
4
- data.tar.gz: d62bbc6fb021e6a275e6a3853ad7bbee770dff06
3
+ metadata.gz: 5cbded2c27d698be26bb72f36921c70e1e8193d2
4
+ data.tar.gz: 4173ded97b83024341bf8f8922acd60d70d12b6b
5
5
  SHA512:
6
- metadata.gz: 315abfefcae86c1c3924c04d89aef67a07bc043a53c5d2fb2bd1e30b0e02d3be6d1edb12430b6790ff6eceea117332688e4a6d7b67fdf8db70e263598f624ee6
7
- data.tar.gz: f556ff6be0e50c33f6525f9ce5992f50a188aedcb76fcd38c4a2584b8eb37bad9f185c306e21af99a1320f9549f59786baa9829f1ec0052a09a35c5e5d14deaa
6
+ metadata.gz: 054ee98826bd504e44d6c1871e77deb4355f2eabf1907bc23762b95d50445bb64b72c943fa91dcf00e5e15652fdd36860569812f6e3d6446de52eb766bab6211
7
+ data.tar.gz: 6b4c01788ca478e953d2d127779f010a3d3cfc6ccb13803165d9525e83067f1102d90a69a4d39719d5fa8d016da7ae8c046bb9256753c6b9b3fb9b6aa0a92e80
Binary file
data.tar.gz.sig CHANGED
Binary file
Binary file
@@ -56,6 +56,7 @@ module BSON
56
56
  UTF8 = "UTF-8".freeze
57
57
  end
58
58
 
59
+ require "bson/config"
59
60
  require "bson/registry"
60
61
  require "bson/specialized"
61
62
  require "bson/json"
@@ -0,0 +1,51 @@
1
+ # Copyright (C) 2016 MongoDB Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module BSON
16
+
17
+ # Provides configuration options for the BSON library.
18
+ #
19
+ # @since 4.1.0
20
+ module Config
21
+ extend self
22
+
23
+ # Set the configuration option for BSON to validate keys or not.
24
+ #
25
+ # @example Set the config option.
26
+ # BSON::Config.validating_keys = true
27
+ #
28
+ # @param [ true, false ] value The value to set.
29
+ #
30
+ # @return [ true, false ] The value.
31
+ #
32
+ # @since 4.1.0
33
+ def validating_keys=(value)
34
+ @validating_keys = value
35
+ end
36
+
37
+ # Returns true if BSON will validate the document keys on serialization to
38
+ # determine if they contain invalid MongoDB values. Invalid keys start with
39
+ # '$' or contain a '.' in them.
40
+ #
41
+ # @example Is BSON validating keys?
42
+ # BSON::Config.validating_keys?
43
+ #
44
+ # @return [ true, false ] If BSON is validating keys?
45
+ #
46
+ # @since 4.1.0
47
+ def validating_keys?
48
+ !!@validating_keys
49
+ end
50
+ end
51
+ end
@@ -37,12 +37,12 @@ module BSON
37
37
  # @see http://bsonspec.org/#/specification
38
38
  #
39
39
  # @since 2.0.0
40
- def to_bson(buffer = ByteBuffer.new)
40
+ def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
41
41
  position = buffer.length
42
42
  buffer.put_int32(0)
43
43
  each do |field, value|
44
44
  buffer.put_byte(value.bson_type)
45
- buffer.put_cstring(field.to_bson_key)
45
+ buffer.put_cstring(field.to_bson_key(validating_keys))
46
46
  value.to_bson(buffer)
47
47
  end
48
48
  buffer.put_byte(NULL_BYTE)
@@ -145,8 +145,18 @@ module BSON
145
145
  encoded << ((self >> 56) & 255)
146
146
  end
147
147
 
148
- def to_bson_key
149
- to_s
148
+ # Convert the integer to a BSON string key.
149
+ #
150
+ # @example Convert the integer to a BSON key string.
151
+ # 1.to_bson_key
152
+ #
153
+ # @param [ true, false ] validating_keys If BSON should validate the key.
154
+ #
155
+ # @return [ String ] The string key.
156
+ #
157
+ # @since 2.0.0
158
+ def to_bson_key(validating_keys = Config.validating_keys?)
159
+ to_s.to_bson_key(validating_keys)
150
160
  end
151
161
 
152
162
  private
@@ -31,7 +31,7 @@ module BSON
31
31
  # @see http://bsonspec.org/#/specification
32
32
  #
33
33
  # @since 2.2.4
34
- def to_bson_key(buffer = ByteBuffer.new)
34
+ def to_bson_key(validating_keys = Config.validating_keys?)
35
35
  raise InvalidKey.new(self)
36
36
  end
37
37
 
@@ -28,6 +28,11 @@ module BSON
28
28
  # @since 2.0.0
29
29
  BSON_TYPE = 2.chr.force_encoding(BINARY).freeze
30
30
 
31
+ # Regex for matching illegal BSON keys.
32
+ #
33
+ # @since 4.1.0
34
+ ILLEGAL_KEY = /(\A[$])|(\.)/.freeze
35
+
31
36
  # Get the string as encoded BSON.
32
37
  #
33
38
  # @example Get the string as encoded BSON.
@@ -51,12 +56,18 @@ module BSON
51
56
  #
52
57
  # @raise [ EncodingError ] If the string is not UTF-8.
53
58
  #
59
+ # @raise [ IllegalKey ] If validating keys and it contains a '.' or starts
60
+ # with '$'.
61
+ #
54
62
  # @return [ String ] The encoded string.
55
63
  #
56
64
  # @see http://bsonspec.org/#/specification
57
65
  #
58
66
  # @since 2.0.0
59
- def to_bson_key
67
+ def to_bson_key(validating_keys = Config.validating_keys?)
68
+ if validating_keys
69
+ raise IllegalKey.new(self) if ILLEGAL_KEY =~ self
70
+ end
60
71
  self
61
72
  end
62
73
 
@@ -89,6 +100,24 @@ module BSON
89
100
  unpack("H*")[0]
90
101
  end
91
102
 
103
+ # Raised when validating keys and a key is illegal in MongoDB
104
+ #
105
+ # @since 4.1.0
106
+ class IllegalKey < RuntimeError
107
+
108
+ # Instantiate the exception.
109
+ #
110
+ # @example Instantiate the exception.
111
+ # BSON::Object::IllegalKey.new(string)
112
+ #
113
+ # @param [ String ] string The illegal string.
114
+ #
115
+ # @since 4.1.0
116
+ def initialize(string)
117
+ super("'#{string}' is an illegal key in MongoDB. Keys may not start with '$' or contain a '.'.")
118
+ end
119
+ end
120
+
92
121
  module ClassMethods
93
122
 
94
123
  # Deserialize a string from BSON.
@@ -70,8 +70,8 @@ module BSON
70
70
  # @see http://bsonspec.org/#/specification
71
71
  #
72
72
  # @since 2.0.0
73
- def to_bson_key
74
- to_s.to_bson_key
73
+ def to_bson_key(validating_keys = Config.validating_keys?)
74
+ to_s.to_bson_key(validating_keys)
75
75
  end
76
76
 
77
77
  # Converts the symbol to a normalized key in a BSON document.
@@ -13,5 +13,5 @@
13
13
  # limitations under the License.
14
14
 
15
15
  module BSON
16
- VERSION = "4.0.4".freeze
16
+ VERSION = "4.1.0".freeze
17
17
  end
@@ -0,0 +1,40 @@
1
+ require "spec_helper"
2
+
3
+ describe BSON::Config do
4
+
5
+ describe "#validating_keys?" do
6
+
7
+ context "when the default is used" do
8
+
9
+ it "returns false" do
10
+ expect(described_class).to_not be_validating_keys
11
+ end
12
+ end
13
+
14
+ context "when configuring to false" do
15
+
16
+ before do
17
+ BSON::Config.validating_keys = false
18
+ end
19
+
20
+ it "returns false" do
21
+ expect(described_class).to_not be_validating_keys
22
+ end
23
+ end
24
+
25
+ context "when configuring to true" do
26
+
27
+ before do
28
+ BSON::Config.validating_keys = true
29
+ end
30
+
31
+ after do
32
+ BSON::Config.validating_keys = false
33
+ end
34
+
35
+ it "returns true" do
36
+ expect(described_class).to be_validating_keys
37
+ end
38
+ end
39
+ end
40
+ end
@@ -52,6 +52,54 @@ describe Hash do
52
52
  end
53
53
  end
54
54
 
55
+ context "when the hash has invalid keys" do
56
+
57
+ let(:obj) do
58
+ { "$testing" => "value" }
59
+ end
60
+
61
+ context "when validating keys" do
62
+
63
+ context "when validating globally" do
64
+
65
+ before do
66
+ BSON::Config.validating_keys = true
67
+ end
68
+
69
+ after do
70
+ BSON::Config.validating_keys = false
71
+ end
72
+
73
+ it "raises an error" do
74
+ expect {
75
+ obj.to_bson
76
+ }.to raise_error(BSON::String::IllegalKey)
77
+ end
78
+ end
79
+
80
+ context "when validating locally" do
81
+
82
+ it "raises an error" do
83
+ expect {
84
+ obj.to_bson(BSON::ByteBuffer.new, true)
85
+ }.to raise_error(BSON::String::IllegalKey)
86
+ end
87
+ end
88
+ end
89
+
90
+ context "when not validating keys" do
91
+
92
+ let(:bson) do
93
+ "#{25.to_bson.to_s}#{String::BSON_TYPE}$testing#{BSON::NULL_BYTE}" +
94
+ "#{6.to_bson.to_s}value#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}"
95
+ end
96
+
97
+ it "serializes the hash" do
98
+ expect(obj.to_bson.to_s).to eq(bson)
99
+ end
100
+ end
101
+ end
102
+
55
103
  context "when the hash is embedded" do
56
104
 
57
105
  let(:obj) do
@@ -87,4 +87,47 @@ describe String do
87
87
  expect(string.to_hex_string).to eq("74657374696e67313233")
88
88
  end
89
89
  end
90
+
91
+ describe "#to_bson_key" do
92
+
93
+ context "when validating keys" do
94
+
95
+ context "when validating globally" do
96
+
97
+ before do
98
+ BSON::Config.validating_keys = true
99
+ end
100
+
101
+ after do
102
+ BSON::Config.validating_keys = false
103
+ end
104
+
105
+ let(:validated) do
106
+ string.to_bson_key
107
+ end
108
+
109
+ it_behaves_like "a validated BSON key"
110
+ end
111
+
112
+ context "when validating locally" do
113
+
114
+ let(:validated) do
115
+ string.to_bson_key(true)
116
+ end
117
+
118
+ it_behaves_like "a validated BSON key"
119
+ end
120
+ end
121
+
122
+ context "when allowing invalid keys" do
123
+
124
+ let(:string) do
125
+ "$testing.testing"
126
+ end
127
+
128
+ it "allows invalid keys" do
129
+ expect(string.to_bson_key).to eq(string)
130
+ end
131
+ end
132
+ end
90
133
  end
@@ -43,4 +43,31 @@ describe Symbol do
43
43
  expect(symbol.to_bson_key).to eq(encoded)
44
44
  end
45
45
  end
46
+
47
+ describe "#to_bson_key" do
48
+
49
+ context "when validating keys" do
50
+
51
+ let(:symbol) do
52
+ :'$testing.testing'
53
+ end
54
+
55
+ it "raises an exception" do
56
+ expect {
57
+ symbol.to_bson_key(true)
58
+ }.to raise_error(BSON::String::IllegalKey)
59
+ end
60
+ end
61
+
62
+ context "when not validating keys" do
63
+
64
+ let(:symbol) do
65
+ :'$testing.testing'
66
+ end
67
+
68
+ it "allows invalid keys" do
69
+ expect(symbol.to_bson_key).to eq(symbol.to_s)
70
+ end
71
+ end
72
+ end
46
73
  end
@@ -96,3 +96,60 @@ shared_examples_for "a class which converts to Time" do
96
96
  expect(described_class.new.bson_type).to eq(Time::BSON_TYPE)
97
97
  end
98
98
  end
99
+
100
+ shared_examples_for "a validated BSON key" do
101
+
102
+ context "when the string is valid" do
103
+
104
+ context "when the string has no invalid characters" do
105
+
106
+ let(:string) do
107
+ "testing"
108
+ end
109
+
110
+ it "returns the key" do
111
+ expect(validated).to eq(string)
112
+ end
113
+ end
114
+
115
+ context "when the string contains a $" do
116
+
117
+ let(:string) do
118
+ "te$ting"
119
+ end
120
+
121
+ it "returns the key" do
122
+ expect(validated).to eq(string)
123
+ end
124
+ end
125
+ end
126
+
127
+ context "when the string is invalid" do
128
+
129
+ context "when the string starts with $" do
130
+
131
+ let(:string) do
132
+ "$testing"
133
+ end
134
+
135
+ it "raises an exception" do
136
+ expect {
137
+ validated
138
+ }.to raise_error(BSON::String::IllegalKey)
139
+ end
140
+ end
141
+
142
+ context "when the string contains a ." do
143
+
144
+ let(:string) do
145
+ "testing.testing"
146
+ end
147
+
148
+ it "raises an exception" do
149
+ expect {
150
+ validated
151
+ }.to raise_error(BSON::String::IllegalKey)
152
+ end
153
+ end
154
+ end
155
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bson
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.4
4
+ version: 4.1.0
5
5
  platform: java
6
6
  authors:
7
7
  - Tyler Brock
@@ -16,7 +16,7 @@ cert_chain:
16
16
  -----BEGIN CERTIFICATE-----
17
17
  MIIDfDCCAmSgAwIBAgIBATANBgkqhkiG9w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5M=
30
+ KoZIhvcNAQEFBQADggEBAKe478140EL5v2vJMjRSGCTtv9tD7jH7595aW267n9Tg
31
+ nuFFwHVC3cTz7xbWi8s7Dat3uC1qWhg7qPFjrF5QZymP7R3vC887XiXwJCnkilx4
32
+ tqIhdDCk/PU1G8RvS2GtCjxcDZLzr2sLNlqC1OCplJrL8YAY6vF8i46WKudH/189
33
+ HPLRJ8tx0aFZY7c6MO1NLqCtBs+wdB9sip7CvCA/w4Ly1MkLzVPXvzjkDJPwkoF1
34
+ yVPbgNJA2D/PnLe8ZFMhgJf+VIA1V4oybP/o+9aqlghTtNfYHJpRCAluUiaywwXl
35
+ KzD4mmIBpxtbWrWB3hx7tsVJmWx5zgO9aDAfvWnXfoo=
36
36
  -----END CERTIFICATE-----
37
- date: 2016-03-06 00:00:00.000000000 Z
37
+ date: 2016-04-14 00:00:00.000000000 Z
38
38
  dependencies: []
39
39
  description: A full featured BSON specification implementation, in Ruby
40
40
  email:
@@ -56,6 +56,7 @@ files:
56
56
  - lib/bson/boolean.rb
57
57
  - lib/bson/code.rb
58
58
  - lib/bson/code_with_scope.rb
59
+ - lib/bson/config.rb
59
60
  - lib/bson/date.rb
60
61
  - lib/bson/date_time.rb
61
62
  - lib/bson/document.rb
@@ -88,6 +89,7 @@ files:
88
89
  - spec/bson/byte_buffer_spec.rb
89
90
  - spec/bson/code_spec.rb
90
91
  - spec/bson/code_with_scope_spec.rb
92
+ - spec/bson/config_spec.rb
91
93
  - spec/bson/date_spec.rb
92
94
  - spec/bson/date_time_spec.rb
93
95
  - spec/bson/document_spec.rb
@@ -147,6 +149,7 @@ test_files:
147
149
  - spec/bson/byte_buffer_spec.rb
148
150
  - spec/bson/code_spec.rb
149
151
  - spec/bson/code_with_scope_spec.rb
152
+ - spec/bson/config_spec.rb
150
153
  - spec/bson/date_spec.rb
151
154
  - spec/bson/date_time_spec.rb
152
155
  - spec/bson/document_spec.rb
metadata.gz.sig CHANGED
Binary file