Permalink
Browse files

Fix a couple bugs in the nullable support.

- Don't change `type: null` to `type: [null]`.
- Don't blow up when there is an actual property named `type`.
  • Loading branch information...
myronmarston committed Apr 28, 2013
1 parent 8ebc096 commit 1928a3668d1ad15eaf98f323c47a1fc87108f61d
Showing with 39 additions and 6 deletions.
  1. +3 −5 lib/interpol/endpoint.rb
  2. +36 −1 spec/unit/interpol/endpoint_spec.rb
View
@@ -247,7 +247,7 @@ def make_schema_strict!(raw_schema, modify_object=true)
end
def make_schema_hash_strict!(raw_schema, make_this_schema_strict=true)
- conditionally_make_nullable(raw_schema)
+ conditionally_make_nullable(raw_schema) if make_this_schema_strict
raw_schema.each do |key, value|
make_schema_strict!(value, key != 'properties')
@@ -272,11 +272,9 @@ def conditionally_make_nullable(raw_schema)
return unless should_be_nullable?(raw_schema)
types = Array(raw_schema['type'])
- return unless types.any?
+ return if types.none? || types.include?('null')
- types << "null" unless types.include?('null')
-
- raw_schema['type'] = types
+ raw_schema['type'] = (types << "null")
end
def should_be_nullable?(raw_schema)
@@ -395,7 +395,7 @@ def new_with(hash)
expect { subject.validate_data!('foo' => nil) }.not_to raise_error
end
- it 'does not add an extra `null` entry to an existing nullable type' do
+ it 'does not add an extra `null` entry to an existing nullable union type' do
schema['properties']['foo']['type'] = %w[ integer null ]
::JSON::Validator.should_receive(:fully_validate_schema) do |schema|
@@ -406,6 +406,17 @@ def new_with(hash)
subject.validate_data!('foo' => nil)
end
+ it 'does not add an extra `null` entry to an existing nullable scalar type' do
+ schema['properties']['foo']['type'] = 'null'
+
+ ::JSON::Validator.should_receive(:fully_validate_schema) do |schema|
+ expect(schema['properties']['foo']['type']).to eq('null')
+ [] # no errors
+ end
+
+ subject.validate_data!('foo' => nil)
+ end
+
it 'does not allow nulls when the property has `nullable: false`' do
schema['properties']['foo']['nullable'] = false
@@ -556,6 +567,30 @@ def new_with(hash)
subject.validate_data!('foo' => [{'name' => 3, 'bar' => 7}])
}.to raise_error(ValidationError)
end
+
+ context 'when scalars_nullable_by_default is set to true' do
+ before { config.scalars_nullable_by_default = true }
+
+ it 'allows nulls for nested sub properties' do
+ expect {
+ subject.validate_data!('foo' => [{ 'name' => nil }])
+ }.not_to raise_error
+ end
+
+ it 'works when there is actually a type property' do
+ schema['properties']['foo']['items']['properties']['type'] = {
+ 'type' => 'string'
+ }
+
+ expect {
+ subject.validate_data!('foo' => [{ 'name' => 3, 'type' => 'integer' }])
+ }.not_to raise_error
+
+ expect {
+ subject.validate_data!('foo' => [{ 'name' => 3, 'type' => nil }])
+ }.not_to raise_error
+ end
+ end
end
context 'a schema with an array of union types' do

0 comments on commit 1928a36

Please sign in to comment.