diff --git a/generator/src/googleapis/codegen/api.py b/generator/src/googleapis/codegen/api.py index 10d56806d84..e437bc02976 100755 --- a/generator/src/googleapis/codegen/api.py +++ b/generator/src/googleapis/codegen/api.py @@ -685,6 +685,10 @@ def __init__(self, api, name, def_dict, parent=None): # TODO(user): if rest_path is not set, raise a good error and fail fast. self.SetTemplateValue('restPath', rest_path) + # Check that ApiVersion field exists and is not empty string ("") + if 'apiVersion' in def_dict and def_dict.get('apiVersion'): + self.SetTemplateValue('apiVersion', def_dict.get('apiVersion')) + # Figure out the input and output types and schemas for this method. expected_request = self.values.get('request') if expected_request: diff --git a/generator/src/googleapis/codegen/languages/java/2.0.0/templates/_method.tmpl b/generator/src/googleapis/codegen/languages/java/2.0.0/templates/_method.tmpl index 5bd3b41d7fb..674b2f1c401 100644 --- a/generator/src/googleapis/codegen/languages/java/2.0.0/templates/_method.tmpl +++ b/generator/src/googleapis/codegen/languages/java/2.0.0/templates/_method.tmpl @@ -88,6 +88,10 @@ public class {{ method.className }} extends {{ api.className }}Request<{{ method private static final String REST_PATH = "{{ method.restPath }}"; + {% if method.apiVersion %} + private static final String API_VERSION = "{{ method.apiVersion }}"; + {% endif %} + {% for param in method.parameters %} {% if param.pattern %} private final java.util.regex.Pattern {{ param.constantName }}_PATTERN = @@ -136,6 +140,13 @@ public class {{ method.className }} extends {{ api.className }}Request<{{ method {% if method.supportsMediaDownload %} initializeMediaDownload(); {% endif %} + {% if method.apiVersion %} + // Does not override the API_VERSION if already configured + com.google.api.client.http.HttpHeaders headers = getRequestHeaders(); + if (headers.get(API_VERSION_HEADER) == null) { + headers.set(API_VERSION_HEADER, API_VERSION); + } + {% endif %} } {% endfilter %} @@ -184,6 +195,13 @@ public class {{ method.className }} extends {{ api.className }}Request<{{ method com.google.api.client.util.Preconditions.checkNotNull(mediaContent, "Required parameter mediaContent must be specified."); initializeMediaUpload(mediaContent); {% call_template _alt_def method=method %} + {% if method.apiVersion %} + // Does not override the API_VERSION if already configured + com.google.api.client.http.HttpHeaders headers = getRequestHeaders(); + if (headers.get(API_VERSION_HEADER) == null) { + headers.set(API_VERSION_HEADER, API_VERSION); + } + {% endif %} } {% endif %} {% endfilter %}