Fixed some YARD documentation issues and changed how the user-agent is built.
[arvados.git] / lib / google / api_client / discovery / schema.rb
index 22551396dbab862823a16056b20e8b6817e858dc..9421be1e1ca43b99b3cfa970a0c11e85b4b2c41c 100644 (file)
@@ -23,6 +23,7 @@ require 'addressable/template'
 require 'google/inflection'
 require 'google/api_client/errors'
 
+
 module Google
   class APIClient
     module Schema
@@ -31,6 +32,43 @@ module Google
         # 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)
@@ -57,7 +95,7 @@ module Google
         # 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, :uri => schema_uri)
           if schema_name
             api_version.const_set(schema_name, schema_class)
           end