Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Stringify symbols before membrane validation

Change-Id: I13720ec1cbc5548e0b5f645c79ab443dff0ce8e7
  • Loading branch information...
commit 344c5e43eab435cef46038726c07a67b9cd3e4f0 1 parent 1821440
@dsabeti dsabeti authored
View
78 Gemfile.dea.lock
@@ -0,0 +1,78 @@
+GIT
+ remote: https://github.com/cloudfoundry/vcap-common.git
+ revision: 45cd194dce44d590512d50d344f66cbe84b46732
+ specs:
+ vcap_common (2.0.10)
+ em-http-request (~> 1.0.0.beta3, < 1.0.0.beta4)
+ eventmachine
+ httpclient
+ membrane (~> 0.0.2)
+ mime-types
+ multipart-post
+ nats (~> 0.4.24)
+ posix-spawn (~> 0.3.6)
+ thin
+ yajl-ruby
+
+PATH
+ remote: .
+ specs:
+ schemata-dea (0.0.1)
+ membrane
+ vcap_common
+ yajl-ruby
+
+GEM
+ remote: https://rubygems.org/
+ specs:
+ addressable (2.3.2)
+ builder (3.1.4)
+ ci_reporter (1.8.3)
+ builder (>= 2.1.2)
+ daemons (1.1.9)
+ diff-lcs (1.1.3)
+ em-http-request (1.0.0.beta.3)
+ addressable (>= 2.2.3)
+ em-socksify
+ eventmachine
+ http_parser.rb (>= 0.5.1)
+ em-socksify (0.1.0)
+ eventmachine
+ eventmachine (0.12.10)
+ http_parser.rb (0.5.3)
+ httpclient (2.3.0.1)
+ json_pure (1.7.5)
+ membrane (0.0.2)
+ mime-types (1.19)
+ multipart-post (1.1.5)
+ nats (0.4.28)
+ daemons (>= 1.1.5)
+ eventmachine (= 0.12.10)
+ json_pure (>= 1.7.3)
+ thin (>= 1.4.1)
+ posix-spawn (0.3.6)
+ rack (1.4.1)
+ rake (10.0.3)
+ rspec (2.12.0)
+ rspec-core (~> 2.12.0)
+ rspec-expectations (~> 2.12.0)
+ rspec-mocks (~> 2.12.0)
+ rspec-core (2.12.2)
+ rspec-expectations (2.12.0)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.12.0)
+ thin (1.5.0)
+ daemons (>= 1.0.9)
+ eventmachine (>= 0.12.6)
+ rack (>= 1.0.0)
+ yajl-ruby (1.1.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ ci_reporter
+ rake
+ rspec
+ schemata-dea!
+ vcap_common!
View
78 Gemfile.router.lock
@@ -0,0 +1,78 @@
+GIT
+ remote: https://github.com/cloudfoundry/vcap-common.git
+ revision: 45cd194dce44d590512d50d344f66cbe84b46732
+ specs:
+ vcap_common (2.0.10)
+ em-http-request (~> 1.0.0.beta3, < 1.0.0.beta4)
+ eventmachine
+ httpclient
+ membrane (~> 0.0.2)
+ mime-types
+ multipart-post
+ nats (~> 0.4.24)
+ posix-spawn (~> 0.3.6)
+ thin
+ yajl-ruby
+
+PATH
+ remote: .
+ specs:
+ schemata-router (0.0.1)
+ membrane
+ vcap_common
+ yajl-ruby
+
+GEM
+ remote: https://rubygems.org/
+ specs:
+ addressable (2.3.2)
+ builder (3.1.4)
+ ci_reporter (1.8.3)
+ builder (>= 2.1.2)
+ daemons (1.1.9)
+ diff-lcs (1.1.3)
+ em-http-request (1.0.0.beta.3)
+ addressable (>= 2.2.3)
+ em-socksify
+ eventmachine
+ http_parser.rb (>= 0.5.1)
+ em-socksify (0.1.0)
+ eventmachine
+ eventmachine (0.12.10)
+ http_parser.rb (0.5.3)
+ httpclient (2.3.0.1)
+ json_pure (1.7.5)
+ membrane (0.0.2)
+ mime-types (1.19)
+ multipart-post (1.1.5)
+ nats (0.4.28)
+ daemons (>= 1.1.5)
+ eventmachine (= 0.12.10)
+ json_pure (>= 1.7.3)
+ thin (>= 1.4.1)
+ posix-spawn (0.3.6)
+ rack (1.4.1)
+ rake (10.0.3)
+ rspec (2.12.0)
+ rspec-core (~> 2.12.0)
+ rspec-expectations (~> 2.12.0)
+ rspec-mocks (~> 2.12.0)
+ rspec-core (2.12.2)
+ rspec-expectations (2.12.1)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.12.0)
+ thin (1.5.0)
+ daemons (>= 1.0.9)
+ eventmachine (>= 0.12.6)
+ rack (>= 1.0.0)
+ yajl-ruby (1.1.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ ci_reporter
+ rake
+ rspec
+ schemata-router!
+ vcap_common!
View
77 Gemfile.staging.lock
@@ -0,0 +1,77 @@
+GIT
+ remote: https://github.com/cloudfoundry/vcap-common.git
+ revision: 45cd194dce44d590512d50d344f66cbe84b46732
+ specs:
+ vcap_common (2.0.10)
+ em-http-request (~> 1.0.0.beta3, < 1.0.0.beta4)
+ eventmachine
+ httpclient
+ membrane (~> 0.0.2)
+ mime-types
+ multipart-post
+ nats (~> 0.4.24)
+ posix-spawn (~> 0.3.6)
+ thin
+ yajl-ruby
+
+PATH
+ remote: .
+ specs:
+ schemata-staging (0.0.2)
+ membrane
+ yajl-ruby
+
+GEM
+ remote: https://rubygems.org/
+ specs:
+ addressable (2.3.2)
+ builder (3.1.4)
+ ci_reporter (1.8.3)
+ builder (>= 2.1.2)
+ daemons (1.1.9)
+ diff-lcs (1.1.3)
+ em-http-request (1.0.0.beta.3)
+ addressable (>= 2.2.3)
+ em-socksify
+ eventmachine
+ http_parser.rb (>= 0.5.1)
+ em-socksify (0.1.0)
+ eventmachine
+ eventmachine (0.12.10)
+ http_parser.rb (0.5.3)
+ httpclient (2.3.0.1)
+ json_pure (1.7.5)
+ membrane (0.0.2)
+ mime-types (1.19)
+ multipart-post (1.1.5)
+ nats (0.4.28)
+ daemons (>= 1.1.5)
+ eventmachine (= 0.12.10)
+ json_pure (>= 1.7.3)
+ thin (>= 1.4.1)
+ posix-spawn (0.3.6)
+ rack (1.4.1)
+ rake (10.0.3)
+ rspec (2.12.0)
+ rspec-core (~> 2.12.0)
+ rspec-expectations (~> 2.12.0)
+ rspec-mocks (~> 2.12.0)
+ rspec-core (2.12.2)
+ rspec-expectations (2.12.0)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.12.0)
+ thin (1.5.0)
+ daemons (>= 1.0.9)
+ eventmachine (>= 0.12.6)
+ rack (>= 1.0.0)
+ yajl-ruby (1.1.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ ci_reporter
+ rake
+ rspec
+ schemata-staging!
+ vcap_common!
View
14 lib/schemata/common/msgbase.rb
@@ -14,6 +14,17 @@ def initialize(data = {})
field_schema = @schema.schemas[key]
next unless field_schema
+ # TODO This call to stringify should be removed when cc/dea stop using
+ # Symbols.
+ #
+ # Currently, some fields (for example, 'states' in requests sent
+ # on dea.find.droplet), are are symbols, During Yajl decoding, however,
+ # they become strings. Thus, on the encoding side, Schemata should expect
+ # symbols, but on the decoding side, it should expect strings. To allow
+ # for this in the schema definition, Schemata stringifies all symbols during
+ # construction of Schemata objects.
+ field_value = Schemata::HashCopyHelpers.stringify(field_value)
+
begin
field_schema.validate(field_value)
rescue Membrane::SchemaValidationError => e
@@ -35,7 +46,10 @@ def self.define(schema)
nil
end
+ # TODO This call to stringify should be removed when cc/dea stops using
+ # symbols. See comment above for a better description.
vc_klass.send(:define_method, "#{key}=") do |field_value|
+ field_value = Schemata::HashCopyHelpers.stringify(field_value)
begin
field_schema.validate(field_value)
rescue Membrane::SchemaValidationError => e
View
26 lib/schemata/helpers/stringify.rb
@@ -0,0 +1,26 @@
+module Schemata
+ module HashCopyHelpers
+
+ def self.stringify(node)
+ case node
+ when String
+ return node
+ when Numeric, TrueClass, FalseClass
+ return node
+ when Hash
+ copy = {}
+ node.each { |k, v| copy[k.to_s] = stringify(v) }
+ return copy
+ when Array
+ return node.map { |v| stringify(v) }
+ when NilClass
+ return nil
+ when Symbol
+ return node.to_s
+ else
+ raise CopyError.new("Unexpected class: #{node.class}")
+ end
+ end
+
+ end
+end
View
28 spec/common/helpers_spec.rb
@@ -1,4 +1,5 @@
require 'schemata/helpers/hash_copy'
+require 'schemata/helpers/stringify'
describe Schemata::HashCopyHelpers do
describe "#deep_copy" do
@@ -29,7 +30,7 @@
copy.should be_an_instance_of Fixnum
# set original to be max fixnum + 1
- original = 2**(0.size * 8 -2)
+ original = 2**(0.size * 8 - 2)
copy = Schemata::HashCopyHelpers.deep_copy(original)
copy.should == original
copy.should be_an_instance_of Bignum
@@ -86,4 +87,29 @@
end.to raise_error(described_class::CopyError, /Unexpected class: /)
end
end
+
+ describe "#stringify" do
+ it "should stringify nil" do
+ str = Schemata::HashCopyHelpers.stringify(nil)
+ str.should == nil
+ end
+
+ it "should stringify a string" do
+ original = "foo"
+ str = Schemata::HashCopyHelpers.stringify(original)
+ str.should == "foo"
+ end
+
+ it "should stringify a symbol" do
+ original = :foo
+ str = Schemata::HashCopyHelpers.stringify(original)
+ str.should == "foo"
+ end
+
+ it "should stringify a hash" do
+ original = { "foo" => :foo }
+ str = Schemata::HashCopyHelpers.stringify(original)
+ str.should == { "foo" => "foo" }
+ end
+ end
end
View
17 spec/dea/find_droplet_request_spec.rb
@@ -7,4 +7,21 @@
describe Schemata::DEA::FindDropletRequest::V1 do
it_behaves_like "a message"
+
+ it "should stringify the 'states' field when it is given to the constructor as a symbol" do
+ hash = {
+ "droplet" => "deadbeef",
+ "states" => [:RUNNING],
+ "version" => "0.1.0",
+ }
+
+ msg_obj = Schemata::DEA::FindDropletRequest::V1.new(hash)
+ msg_obj.states.should =~ ["RUNNING"]
+ end
+
+ it "should stringify the 'states' field when a symbold is passed ot the attr writer" do
+ msg_obj = Schemata::DEA.mock_find_droplet_request
+ msg_obj.states = [:RUNNING]
+ msg_obj.states.should =~ ["RUNNING"]
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.