Skip to content

Commit ddb4c5e

Browse files
authored
feat(internal/librarian/java): add api shortname override support (#5304)
An api_shortname_override field is added to the Java configuration to allow the `api_shortname` field in `.repo-metadata.json` to be overridden. This provides flexibility for libraries that require a specific shortname that differs from the default derived value. For #5296
1 parent 8804db4 commit ddb4c5e

4 files changed

Lines changed: 81 additions & 36 deletions

File tree

doc/config-schema.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ This document describes the schema for the librarian.yaml.
253253
| `api_id_override` | string | Is the ID of the API (e.g., "pubsub.googleapis.com"), allows the "api_id" field in .repo-metadata.json to be overridden. Defaults to "{library.api_shortname}.googleapis.com". |
254254
| `api_reference` | string | Is the URL for the API reference documentation. |
255255
| `api_description_override` | string | Allows the "api_description" field in .repo-metadata.json to be overridden. |
256+
| `api_shortname_override` | string | Allows the "api_shortname" field in .repo-metadata.json to be overridden. |
256257
| `client_documentation_override` | string | Allows the "client_documentation" field in .repo-metadata.json to be overridden. |
257258
| `non_cloud_api` | bool | Indicates whether the API is NOT a Google Cloud API. Defaults to false. |
258259
| `codeowner_team` | string | Is the GitHub team that owns the code. |

internal/config/language.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@ type JavaModule struct {
496496
// .repo-metadata.json to be overridden.
497497
APIDescriptionOverride string `yaml:"api_description_override,omitempty"`
498498

499+
// APIShortnameOverride allows the "api_shortname" field in
500+
// .repo-metadata.json to be overridden.
501+
APIShortnameOverride string `yaml:"api_shortname_override,omitempty"`
502+
499503
// ClientDocumentationOverride allows the "client_documentation" field in
500504
// .repo-metadata.json to be overridden.
501505
ClientDocumentationOverride string `yaml:"client_documentation_override,omitempty"`

internal/librarian/java/repometadata.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ func deriveRepoMetadata(cfg *config.Config, library *config.Library, googleapisD
110110

111111
// Java-specific overrides and optional fields
112112
if library.Java != nil {
113+
if library.Java.APIShortnameOverride != "" {
114+
metadata.APIShortname = library.Java.APIShortnameOverride
115+
metadata.APIID = fmt.Sprintf("%s.googleapis.com", library.Java.APIShortnameOverride)
116+
}
113117
if library.Java.APIIDOverride != "" {
114118
metadata.APIID = library.Java.APIIDOverride
115119
}

internal/librarian/java/repometadata_test.go

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -78,42 +78,78 @@ func TestDeriveRepoMetadata_Overrides(t *testing.T) {
7878
cfg := sample.Config()
7979
cfg.Language = config.LanguageJava
8080
cfg.Repo = "googleapis/google-cloud-java"
81-
library := &config.Library{
82-
Name: "secretmanager",
83-
APIs: []*config.API{{Path: apiPath}},
84-
Java: &config.JavaModule{
85-
GroupID: "com.custom",
86-
DistributionNameOverride: "com.custom:custom-artifact",
87-
APIIDOverride: "custom.googleapis.com",
88-
APIDescriptionOverride: "Custom description",
89-
NamePrettyOverride: "Custom Pretty Name",
90-
ProductDocumentationOverride: "https://custom.docs",
91-
ClientDocumentationOverride: "https://custom.client.docs",
92-
BillingNotRequired: true,
93-
LibraryTypeOverride: "OTHER",
94-
},
95-
}
96-
got, err := deriveRepoMetadata(cfg, library, googleapis)
97-
if err != nil {
98-
t.Fatalf("deriveRepoMetadata failed: %v", err)
99-
}
10081
s := sample.RepoMetadata()
101-
want := &repoMetadata{
102-
NamePretty: "Custom Pretty Name",
103-
ProductDocumentation: "https://custom.docs",
104-
APIDescription: "Custom description",
105-
ClientDocumentation: "https://custom.client.docs",
106-
ReleaseLevel: s.ReleaseLevel,
107-
Transport: "both",
108-
Language: cfg.Language,
109-
Repo: cfg.Repo,
110-
RepoShort: "java-secretmanager",
111-
DistributionName: "com.custom:custom-artifact",
112-
APIID: "custom.googleapis.com",
113-
LibraryType: "OTHER",
114-
RequiresBilling: false,
115-
}
116-
if diff := cmp.Diff(want, got, cmp.AllowUnexported(repoMetadata{})); diff != "" {
117-
t.Errorf("mismatch (-want +got):\n%s", diff)
82+
for _, test := range []struct {
83+
name string
84+
java *config.JavaModule
85+
want *repoMetadata
86+
}{
87+
{
88+
name: "all overrides",
89+
java: &config.JavaModule{
90+
GroupID: "com.custom",
91+
DistributionNameOverride: "com.custom:custom-artifact",
92+
APIIDOverride: "custom.googleapis.com",
93+
APIDescriptionOverride: "Custom description",
94+
APIShortnameOverride: "custom-shortname",
95+
NamePrettyOverride: "Custom Pretty Name",
96+
ProductDocumentationOverride: "https://custom.docs",
97+
ClientDocumentationOverride: "https://custom.client.docs",
98+
BillingNotRequired: true,
99+
LibraryTypeOverride: "OTHER",
100+
},
101+
want: &repoMetadata{
102+
APIShortname: "custom-shortname",
103+
NamePretty: "Custom Pretty Name",
104+
ProductDocumentation: "https://custom.docs",
105+
APIDescription: "Custom description",
106+
ClientDocumentation: "https://custom.client.docs",
107+
ReleaseLevel: s.ReleaseLevel,
108+
Transport: "both",
109+
Language: cfg.Language,
110+
Repo: cfg.Repo,
111+
RepoShort: "java-secretmanager",
112+
DistributionName: "com.custom:custom-artifact",
113+
APIID: "custom.googleapis.com",
114+
LibraryType: "OTHER",
115+
RequiresBilling: false,
116+
},
117+
},
118+
{
119+
name: "only overrides api shortname",
120+
java: &config.JavaModule{
121+
APIShortnameOverride: "custom-shortname",
122+
},
123+
want: &repoMetadata{
124+
APIShortname: "custom-shortname",
125+
ClientDocumentation: "https://cloud.google.com/java/docs/reference/google-cloud-secretmanager/latest/overview",
126+
ReleaseLevel: "stable",
127+
Transport: "both",
128+
Language: "java",
129+
Repo: "googleapis/google-cloud-java",
130+
RepoShort: "java-secretmanager",
131+
DistributionName: "com.google.cloud:google-cloud-secretmanager",
132+
// API ID is also override.
133+
APIID: "custom-shortname.googleapis.com",
134+
LibraryType: "GAPIC_AUTO",
135+
RequiresBilling: true,
136+
},
137+
},
138+
} {
139+
t.Run(test.name, func(t *testing.T) {
140+
library := &config.Library{
141+
Name: "secretmanager",
142+
APIs: []*config.API{{Path: apiPath}},
143+
Java: test.java,
144+
}
145+
got, err := deriveRepoMetadata(cfg, library, googleapis)
146+
if err != nil {
147+
t.Fatal(err)
148+
}
149+
if diff := cmp.Diff(test.want, got, cmp.AllowUnexported(repoMetadata{})); diff != "" {
150+
t.Errorf("mismatch (-want +got):\n%s", diff)
151+
}
152+
})
118153
}
154+
119155
}

0 commit comments

Comments
 (0)