Fixed issues with recursive structures and external references.
authorBob Aman <bobaman@google.com>
Wed, 5 Oct 2011 11:13:39 +0000 (14:13 +0300)
committerBob Aman <bobaman@google.com>
Wed, 5 Oct 2011 11:13:39 +0000 (14:13 +0300)
lib/google/api_client/discovery/schema.rb
spec/google/api_client/discovery_spec.rb
tasks/gem.rake

index 22551396dbab862823a16056b20e8b6817e858dc..02edd331478e1410bbadea99add2812ab75151aa 100644 (file)
@@ -31,6 +31,43 @@ module Google
         # unavoidable dependence on closures and execution context.
         schema_name = schema_data['id']
 
         # unavoidable dependence on closures and execution context.
         schema_name = schema_data['id']
 
+        # Due to an oversight, schema IDs may not be URI references.
+        # TODO(bobaman): Remove this code once this has been resolved.
+        schema_uri = (
+          api.document_base +
+          (schema_name[0..0] != '#' ? '#' + schema_name : schema_name)
+        )
+        # puts schema_uri
+
+        # Due to an oversight, schema IDs may not be URI references.
+        # TODO(bobaman): Remove this whole lambda once this has been resolved.
+        reformat_references = lambda do |data|
+          # This code is not particularly efficient due to recursive traversal
+          # and excess object creation, but this hopefully shouldn't be an
+          # issue since it should only be called only once per schema per
+          # process.
+          if data.kind_of?(Hash) && data['$ref']
+            reference = data['$ref']
+            reference = '#' + reference if reference[0..0] != '#'
+            data.merge({
+              '$ref' => reference
+            })
+          elsif data.kind_of?(Hash)
+            data.inject({}) do |accu, (key, value)|
+              if value.kind_of?(Hash)
+                accu[key] = reformat_references.call(value)
+              else
+                accu[key] = value
+              end
+              accu
+            end
+          else
+            data
+          end
+        end
+        schema_data = reformat_references.call(schema_data)
+        # puts schema_data.inspect
+
         if schema_name
           api_name_string =
             Google::INFLECTOR.camelize(api.name)
         if schema_name
           api_name_string =
             Google::INFLECTOR.camelize(api.name)
@@ -57,7 +94,7 @@ module Google
         # redefine it. This means that reloading a schema which has already
         # been loaded into memory is not possible.
         unless schema_class
         # redefine it. This means that reloading a schema which has already
         # been loaded into memory is not possible.
         unless schema_class
-          schema_class = AutoParse.generate(schema_data)
+          schema_class = AutoParse.generate(schema_data, schema_uri)
           if schema_name
             api_version.const_set(schema_name, schema_class)
           end
           if schema_name
             api_version.const_set(schema_name, schema_class)
           end
index 7714e25c6052e9784e9803511a4a07a6249f51ba..bcfc66b7048ff682706991e9ba2549d38c842d87 100644 (file)
@@ -379,6 +379,18 @@ describe Google::APIClient do
       status.should == 200
     end
 
       status.should == 200
     end
 
+    it 'should correctly handle nested schema objects' do
+      result = @client.execute(
+        @buzz.activities.list,
+        {'alt' => 'json', 'userId' => 'hikingfan', 'scope' => '@public'},
+        '',
+        [],
+        :authenticated => false
+      )
+      feed = result.data
+      puts feed.inspect
+    end
+
     it 'should not be able to execute requests without authorization' do
       result = @client.execute(
         @buzz.activities.list,
     it 'should not be able to execute requests without authorization' do
       result = @client.execute(
         @buzz.activities.list,
index c10cd632ed743fb15a1f3f64d6a5831d30d62bb6..09130b902abbcf5d1cb3b923ac0a74dbd3630176 100644 (file)
@@ -25,7 +25,7 @@ namespace :gem do
     s.add_runtime_dependency('signet', '~> 0.2.2')
     s.add_runtime_dependency('addressable', '~> 2.2.2')
     s.add_runtime_dependency('httpadapter', '~> 1.0.0')
     s.add_runtime_dependency('signet', '~> 0.2.2')
     s.add_runtime_dependency('addressable', '~> 2.2.2')
     s.add_runtime_dependency('httpadapter', '~> 1.0.0')
-    s.add_runtime_dependency('autoparse', '~> 0.1.0')
+    s.add_runtime_dependency('autoparse', '~> 0.2.0')
     s.add_runtime_dependency('json', '>= 1.4.6')
     s.add_runtime_dependency('extlib', '>= 0.9.15')
 
     s.add_runtime_dependency('json', '>= 1.4.6')
     s.add_runtime_dependency('extlib', '>= 0.9.15')