Skip to content

Commit 6468f3d

Browse files
authored
fix(tool/cmd/migrate): derive APIShortnameOverride instead of hardcode (#5527)
This change refactors how api_shortname is determined and migrated for Java libraries to ensure behavioral parity with the hermetic build while minimizing configuration noise. Migration Script (java.go) now mimics the derives the shortname logic (using serviceconfig.Find) and only adds an APIShortnameOverride to librarian.yaml if the legacy configuration differs. This replaces the hardcoded map in java_module.go. Updated migration tests to use testdata (e.g., secretmanager, places) to verify both standard discovery and discrepancy-based overriding. Confirmed local running migrate, the previous hardcoded overrides are kept. Fix #5296
1 parent 2f0d75c commit 6468f3d

4 files changed

Lines changed: 39 additions & 27 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type: google.api.Service
2+
config_version: 3
3+
name: places.googleapis.com
4+
title: Places API (New)
5+
publishing:
6+
api_short_name: places

tool/cmd/migrate/java.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/googleapis/librarian/internal/config"
3131
"github.com/googleapis/librarian/internal/librarian"
3232
"github.com/googleapis/librarian/internal/librarian/java"
33+
"github.com/googleapis/librarian/internal/serviceconfig"
3334
"github.com/googleapis/librarian/internal/yaml"
3435
)
3536

@@ -294,6 +295,17 @@ func buildConfig(gen *GenerationConfig, repoPath string, src *config.Source, ver
294295
TransportOverride: l.Transport,
295296
},
296297
}
298+
if len(apis) > 0 {
299+
derivedShortName := name
300+
serviceconfig.SortAPIs(apis)
301+
api, err := serviceconfig.Find(src.Dir, apis[0].Path, config.LanguageJava)
302+
if err == nil && api.ShortName != "" {
303+
derivedShortName = api.ShortName
304+
}
305+
if derivedShortName != l.APIShortName {
306+
lib.Java.APIShortnameOverride = l.APIShortName
307+
}
308+
}
297309
if override, ok := keepOverride[lib.Name]; ok {
298310
lib.Keep = override
299311
} else {
@@ -303,9 +315,6 @@ func buildConfig(gen *GenerationConfig, repoPath string, src *config.Source, ver
303315
}
304316
lib.Keep = keep
305317
}
306-
if shortnameOverride, ok := apiShortnameOverrides[lib.Name]; ok {
307-
lib.Java.APIShortnameOverride = shortnameOverride
308-
}
309318
libs = append(libs, lib)
310319
}
311320
if len(libs) == 0 {

tool/cmd/migrate/java_module.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@
1515
package main
1616

1717
var (
18-
apiShortnameOverrides = map[string]string{
19-
"beyondcorp-appconnections": "beyondcorp-appconnections",
20-
"beyondcorp-appconnectors": "beyondcorp-appconnectors",
21-
"beyondcorp-appgateways": "beyondcorp-appgateways",
22-
"beyondcorp-clientconnectorservices": "beyondcorp-clientconnectorservices",
23-
"beyondcorp-clientgateways": "beyondcorp-clientgateways",
24-
"dialogflow-cx": "dialogflow-cx",
25-
"distributedcloudedge": "distributedcloudedge",
26-
"gke-backup": "gke-backup",
27-
"apigee-registry": "apigee-registry",
28-
}
29-
3018
excludedSamplesLibraries = map[string]bool{
3119
"bigquerystorage": true,
3220
"datastore": true,

tool/cmd/migrate/java_test.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,29 +190,29 @@ func TestBuildConfig(t *testing.T) {
190190
gen: &GenerationConfig{
191191
Libraries: []LibraryConfig{
192192
{
193-
LibraryName: "language-v1",
194-
APIShortName: "language",
193+
LibraryName: "secretmanager-v1",
194+
APIShortName: "secretmanager",
195195
GAPICs: []GAPICConfig{
196-
{ProtoPath: "google/cloud/language/v1"},
196+
{ProtoPath: "google/cloud/secretmanager/v1"},
197197
},
198198
},
199199
},
200200
},
201-
src: &config.Source{},
201+
src: &config.Source{Dir: "../../../internal/testdata/googleapis"},
202202
want: &config.Config{
203203
Language: "java",
204204
Repo: "googleapis/google-cloud-java",
205205
Default: &config.Default{
206206
Java: &config.JavaModule{},
207207
},
208208
Sources: &config.Sources{
209-
Googleapis: &config.Source{},
209+
Googleapis: &config.Source{Dir: "../../../internal/testdata/googleapis"},
210210
},
211211
Libraries: []*config.Library{
212212
{
213-
Name: "language-v1",
213+
Name: "secretmanager-v1",
214214
APIs: []*config.API{
215-
{Path: "google/cloud/language/v1"},
215+
{Path: "google/cloud/secretmanager/v1"},
216216
},
217217
Java: &config.JavaModule{},
218218
},
@@ -429,24 +429,33 @@ func TestBuildConfig(t *testing.T) {
429429
name: "api shortname overrides",
430430
gen: &GenerationConfig{
431431
Libraries: []LibraryConfig{
432-
{APIShortName: "beyondcorp-appconnections"},
432+
{
433+
LibraryName: "maps-places",
434+
APIShortName: "maps-places",
435+
GAPICs: []GAPICConfig{
436+
{ProtoPath: "google/maps/places/v1"},
437+
},
438+
},
433439
},
434440
},
435-
src: &config.Source{},
441+
src: &config.Source{Dir: "../../../internal/testdata/googleapis"},
436442
want: &config.Config{
437443
Language: "java",
438444
Repo: "googleapis/google-cloud-java",
439445
Default: &config.Default{
440446
Java: &config.JavaModule{},
441447
},
442448
Sources: &config.Sources{
443-
Googleapis: &config.Source{},
449+
Googleapis: &config.Source{Dir: "../../../internal/testdata/googleapis"},
444450
},
445451
Libraries: []*config.Library{
446452
{
447-
Name: "beyondcorp-appconnections",
453+
Name: "maps-places",
454+
APIs: []*config.API{
455+
{Path: "google/maps/places/v1"},
456+
},
448457
Java: &config.JavaModule{
449-
APIShortnameOverride: "beyondcorp-appconnections",
458+
APIShortnameOverride: "maps-places",
450459
},
451460
},
452461
},

0 commit comments

Comments
 (0)