diff --git a/google-apis-core/lib/google/apis/core/http_command.rb b/google-apis-core/lib/google/apis/core/http_command.rb index f753f2e2faa..70ce4249fe5 100644 --- a/google-apis-core/lib/google/apis/core/http_command.rb +++ b/google-apis-core/lib/google/apis/core/http_command.rb @@ -348,6 +348,12 @@ def allow_form_encoding? [:post, :put].include?(method) && body.nil? end + # Set the API version header for the service if not empty. + # @return [void] + def set_api_version_header api_version + self.header['X-Goog-Api-Version'] = api_version unless api_version.empty? + end + private UNSAFE_CLASS_NAMES = [ diff --git a/google-apis-core/spec/google/apis/core/http_command_spec.rb b/google-apis-core/spec/google/apis/core/http_command_spec.rb index 1b699b34aae..55ddaf4652a 100644 --- a/google-apis-core/spec/google/apis/core/http_command_spec.rb +++ b/google-apis-core/spec/google/apis/core/http_command_spec.rb @@ -528,6 +528,16 @@ class SecretPayload command.execute(client) end + it 'should set X-Goog-Api-Version headers when requested' do + command = Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals') + command.set_api_version_header "v1_20240502" + stub_request(:get, 'https://www.googleapis.com/zoo/animals').to_return(body: %(Api version)) + result = command.execute(client) + expect(a_request(:get, 'https://www.googleapis.com/zoo/animals') + .with { |req| req.headers['X-Goog-Api-Version'] == 'v1_20240502' }).to have_been_made + expect(result).to eql "Api version" + end + describe "#safe_pretty_representation" do let(:command) do Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals') diff --git a/google-apis-generator/lib/google/apis/generator.rb b/google-apis-generator/lib/google/apis/generator.rb index abca27ab681..daa1f277dca 100644 --- a/google-apis-generator/lib/google/apis/generator.rb +++ b/google-apis-generator/lib/google/apis/generator.rb @@ -18,6 +18,7 @@ require 'google/apis/generator/template' require 'google/apis/generator/updater' require 'google/apis/generator/version' +require 'google/apis/generator/patch' require 'active_support' require 'active_support/core_ext' require 'active_support/inflector' diff --git a/google-apis-generator/lib/google/apis/generator/patch.rb b/google-apis-generator/lib/google/apis/generator/patch.rb new file mode 100644 index 00000000000..9c3fe64476f --- /dev/null +++ b/google-apis-generator/lib/google/apis/generator/patch.rb @@ -0,0 +1,35 @@ +require 'google/apis/discovery_v1' + +# Extend the DiscoveryV1 API classes with additional fields for code generation, +# supporting features which are not present in the schema itself. +unless Google::Apis::DiscoveryV1::RestMethod.method_defined? :api_version + module Google + module Apis + module DiscoveryV1 + class RestMethod + # The `apiVersion` for this method, or empty if not present. + # @return [String] + attr_accessor :api_version + + # @private + # The original DiscoveryV1::RestMethod `update!` method to be called + # after applying patches to this schema. + alias_method :update_discovery!, :update! + + # Update properties of this object. + def update!(**args) + @api_version = args.key?(:api_version) ? args[:api_version] : "" + update_discovery!(**args) + end + + # @private + class Representation + # @private + # The api_version based on the JSON key value. + property :api_version, as: 'apiVersion' + end + end + end + end + end +end diff --git a/google-apis-generator/lib/google/apis/generator/templates/_method.tmpl b/google-apis-generator/lib/google/apis/generator/templates/_method.tmpl index 7688111f563..374866ee8d7 100644 --- a/google-apis-generator/lib/google/apis/generator/templates/_method.tmpl +++ b/google-apis-generator/lib/google/apis/generator/templates/_method.tmpl @@ -102,6 +102,9 @@ def <%= api_method.generated_name %>(<% for param in api_method.required_paramet <% end -%> <% for param in api.parameters.values.reject {|p| p.name == 'key'} -%> command.query['<%= param.name %>'] = <%= param.generated_name %> unless <%= param.generated_name %>.nil? +<% end -%> +<% unless api_method.api_version.empty? -%> + command.set_api_version_header "<%= api_method.api_version %>" <% end -%> execute_or_queue_command(command, &block) end diff --git a/google-apis-generator/spec/google/apis/generator/patch_spec.rb b/google-apis-generator/spec/google/apis/generator/patch_spec.rb new file mode 100644 index 00000000000..cf671e3cf07 --- /dev/null +++ b/google-apis-generator/spec/google/apis/generator/patch_spec.rb @@ -0,0 +1,9 @@ +require 'spec_helper' +require 'google/apis/generator' + +RSpec.describe 'Google::Apis::Generator with DiscoveryV1 patch' do + + it 'should modify RestMethod if `api_version` is not defined' do + expect(Google::Apis::Generator::Discovery::RestMethod.method_defined? :update_discovery!).to eql(true) + end +end