json_schema_tools 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MzRiOTM3ZmEzMGQzMThiMmE0NjJiZThmY2JiNmM2OTBjMmVhYzU3OA==
4
+ ZDg0NTQ4YzQwN2QzYjhhYTMzMDM2NmJlMDgzOGM2NDU0MWExOTdlMQ==
5
5
  data.tar.gz: !binary |-
6
- ZTNjNGQ5ZThkMGQ1ODc0NjFlNTE5OWRiYWRiY2Q2NmYwZjJkOTAzZA==
6
+ ZmQ5MTNhYTUwNWY2NWQ5Zjc3ODk1MTQzMjcwNDk0Yzg3NmM0NzZkMw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MDIwMmFlNGMzMWMyNTE3NmM5MjJmOWYzY2Q4MjA4NGM1NWJlNmEzNWFiM2Uw
10
- MjIwZTk3ZDdkMmJlODMxOGM2ZmFkMTA0ZWU5ZTAwZDk0OTQ0OTVlMmE2MDI2
11
- MzdkY2UxMTE0MGEyZjdjNjAyZTc3ZjFlNTdiNDgzMWNiMjUwYjU=
9
+ YzhlNWJhNTJmYTQyMjc5YzQwZWQxMzkzMDQ4MThhNGI0OTNhYmNiNTI0NjQz
10
+ ZDU0MjQ3YTExMDlmMGYzZmQzZGNiMmQwNzUzZDlhYjg3MDE5NmI1NmQ1Yjg4
11
+ NDY3M2NhNjQ1MjMzNjM3NmRkMmMwMzUwN2VlNzI0MmQxYjE2YWI=
12
12
  data.tar.gz: !binary |-
13
- ZDlhNGY2ZGM1MDM2ZjliMjk5YjY3NzVjNDEzNjYxZGY0M2EzNzFmMTQ4Yjkx
14
- NmVkNjVmOWNmN2QwYjc3ZmFmNjAwNzVhN2UzYzM4NjY4NWYwZTA2NzVjZGEx
15
- YjFhN2QxZmU1MzA0Y2VjODFhZDM0MGI2NmY4ZDU0ZjljNDRhNjk=
13
+ OGQ4MTBhYTMzZWI0YTRjYzI5ZWRlOWRiYzBlMWM5ZjYwYzVlM2UyNTVjZTlj
14
+ ZjM1YTZmN2I3MjA3OTY0MDcxM2YwNDMyZTVkNzI4MWM4NGU0MDM0N2QwNzYw
15
+ NjQ1MDgwYjkzM2UxYjczNTU5Mzk3MWI2Njk0NTYzNzIxNjM4MjY=
data/CHANGELOG.md CHANGED
@@ -2,7 +2,10 @@
2
2
 
3
3
  ##2015-04
4
4
 
5
- * use schema definition in nested items array Thanks @VidurMalik
5
+ Thanks @VidurMalik
6
+ * use schema definition in nested items array
7
+ * exclude keys for nil(blank) nested objects
8
+ * allow arbitrary key/values for a nested object without properties definition in the schema
6
9
 
7
10
  ##2015-03
8
11
 
@@ -83,7 +83,7 @@ module SchemaTools
83
83
  data[field] = parse_list(obj, field, prop, opts)
84
84
  elsif prop['type'] == 'object' # a singular related object
85
85
  opts.delete(:class_name)
86
- data[field] = parse_object(obj, field, prop, opts)
86
+ data[field] = parse_object(obj, field, prop, opts) if parse_object?(obj, field)
87
87
  else # a simple field is only added if the object knows it
88
88
  next unless obj.respond_to?(field)
89
89
  raw_val = obj.send(field)
@@ -176,11 +176,8 @@ module SchemaTools
176
176
  # @param [Hash] opts to_schema options
177
177
  # @return [Array<Hash{String=>String}>]
178
178
  def parse_object(obj, field, prop, opts)
179
- return if !obj.respond_to?( field )
180
179
  rel_obj = obj.send( field )
181
- return if !rel_obj
182
-
183
- res = if prop['properties']
180
+ res = if prop['properties'].present?
184
181
  opts[:schema] = prop
185
182
  from_schema(rel_obj, opts)
186
183
  elsif prop['oneOf']
@@ -188,10 +185,22 @@ module SchemaTools
188
185
  # Simpler than detecting the object type or $ref to use inside the
189
186
  # oneOf array
190
187
  from_schema(rel_obj, opts)
188
+ elsif prop['properties'].blank?
189
+ rel_obj
191
190
  end
192
191
  res
193
192
  end
194
193
 
194
+ private
195
+
196
+ def parse_object?(obj, field)
197
+ if obj.respond_to?( field )
198
+ rel_obj = obj.send( field )
199
+ rel_obj.present?
200
+ else
201
+ false
202
+ end
203
+ end
195
204
  end
196
205
  end
197
206
  end
@@ -1,3 +1,3 @@
1
1
  module SchemaTools
2
- VERSION = '0.6.2'
2
+ VERSION = '0.6.3'
3
3
  end
@@ -0,0 +1,7 @@
1
+ {
2
+ "type" : "object",
3
+ "properties" : {
4
+ "id" : { "type" : "string" },
5
+ "user_data" : { "type" : "object" }
6
+ }
7
+ }
@@ -33,6 +33,10 @@ class AnotherAddress
33
33
  has_schema_attrs 'address'
34
34
  end
35
35
 
36
+ class NestedObjectNoProperties
37
+ include SchemaTools::Modules::Attributes
38
+ has_schema_attrs 'nested_object_no_properties'
39
+ end
36
40
 
37
41
  describe SchemaTools::Hash do
38
42
 
@@ -113,9 +117,9 @@ describe SchemaTools::Hash do
113
117
  hash['addresses'].should == []
114
118
  end
115
119
 
116
- it 'has nil if nested object is missing' do
120
+ it 'does not have the key if nested object is missing' do
117
121
  hash = SchemaTools::Hash.from_schema(client)
118
- hash['work_address'].should be_nil
122
+ hash.has_key?('work_address').should == false
119
123
  end
120
124
 
121
125
  it 'has nested array values' do
@@ -170,9 +174,9 @@ describe SchemaTools::Hash do
170
174
  hash['addresses'].should == []
171
175
  end
172
176
 
173
- it 'has nil if nested object is missing' do
177
+ it 'does not have the key if nested object is missing' do
174
178
  hash = SchemaTools::Hash.from_schema(client)
175
- hash['work_address'].should be_nil
179
+ hash.has_key?('work_address').should == false
176
180
  end
177
181
 
178
182
  it 'has nested array values' do
@@ -266,7 +270,16 @@ describe SchemaTools::Hash do
266
270
  hash = SchemaTools::Hash.from_schema(client, base_url: 'http://json-hell.com', links: true)
267
271
  hash['_links'].last['href'].should == 'http://json-hell.com/clients/123/Peter'
268
272
  end
273
+ end
269
274
 
275
+ context 'with a nested object that does not have properties' do
276
+ let(:input_hash) { { foo: 'bar', baz: 'boom' } }
277
+ let(:nested_object_no_properties) { NestedObjectNoProperties.from_hash(user_data: input_hash) }
278
+
279
+ it 'should output the input hash' do
280
+ hash = SchemaTools::Hash.from_schema(nested_object_no_properties)
281
+ hash['user_data'].should == input_hash
282
+ end
270
283
  end
271
284
  end
272
285
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_schema_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Georg Leciejewski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-01 00:00:00.000000000 Z
11
+ date: 2015-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -106,6 +106,7 @@ files:
106
106
  - spec/fixtures/schemata/includes_basic_definitions.json
107
107
  - spec/fixtures/schemata/includes_deep_nested_refs.json
108
108
  - spec/fixtures/schemata/lead.json
109
+ - spec/fixtures/schemata/nested_object_no_properties.json
109
110
  - spec/fixtures/schemata/nested_schemas/person.json
110
111
  - spec/fixtures/schemata/one_of_definition.json
111
112
  - spec/fixtures/schemata/page.json