@@ -264,7 +264,6 @@ void SetHttpQueryParameters(HttpExtensionInfo const& info,
264264HttpExtensionInfo ParseHttpExtension (
265265 google::protobuf::MethodDescriptor const & method) {
266266 if (!method.options ().HasExtension (google::api::http)) return {};
267- auto api_version = FormatApiVersionFromPackageName (method);
268267
269268 HttpExtensionInfo info;
270269 google::api::HttpRule http_rule =
@@ -322,6 +321,9 @@ HttpExtensionInfo ParseHttpExtension(
322321 out->append (absl::visit (SegmentAsStringVisitor{}, s->value ));
323322 };
324323
324+ auto api_version =
325+ FormatApiVersionFromUrlPattern (url_pattern, method.file ()->name ());
326+
325327 auto rest_path_visitor = RestPathVisitor (api_version, info.rest_path );
326328 for (auto const & s : parsed_http_rule->segments ) {
327329 if (absl::holds_alternative<PathTemplate::Variable>(s->value )) {
@@ -392,6 +394,22 @@ std::string FormatApiVersionFromPackageName(
392394 return {}; // Suppress clang-tidy warnings
393395}
394396
397+ // Generate api version by extracting the version from the url pattern.
398+ // In some cases(i.e. location), there is no version in the package name.
399+ std::string FormatApiVersionFromUrlPattern (std::string const & url_pattern,
400+ std::string const & file_name) {
401+ std::vector<std::string> parts = absl::StrSplit (url_pattern, ' /' );
402+ static std::regex const kRe {R"( v\d+)" };
403+ for (auto const & part : parts) {
404+ if (std::regex_match (part, kRe )) {
405+ return part;
406+ }
407+ }
408+ GCP_LOG (FATAL) << " Unrecognized API version in file: " << file_name
409+ << " , url pattern: " << url_pattern;
410+ return {}; // Suppress clang-tidy warnings
411+ }
412+
395413} // namespace generator_internal
396414} // namespace cloud
397415} // namespace google
0 commit comments