Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/sidekick/swift/annotate_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,11 @@ func (c *codec) annotateMethod(method *api.Method, modelAnn *modelAnnotations) e

// Extract routing parameters for gRPC per AIP-4222:
// - Prefer explicit google.api.routing annotations if present.
// - Fall back to extracting path parameters from the google.api.http path template.
// - Fall back to extracting path parameters across all google.api.http bindings.
if method.HasRouting() {
routingParams = c.routingParamsFromRouting(method.Routing())
} else if len(bindings) > 0 {
routingParams = c.routingParamsFromPathTemplate(bindings[0].PathTemplate())
routingParams = c.routingParamsFromBindings(bindings)
}

// In Protobuf APIs (such as Storage Control or pure gRPC services), some RPC
Expand Down
109 changes: 109 additions & 0 deletions internal/sidekick/swift/annotate_method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,115 @@ func TestAnnotateMethod_ExplicitEmptyRoutingSuppressesHTTPFallback(t *testing.T)
}
}

func TestAnnotateMethod_RoutingAdditionalBindings(t *testing.T) {
inputType := api.NewTestMessage("BillingRequest").
WithPackage("google.cloud.billing.v1").
WithFields(
api.NewTestField("name").WithType(api.TypezString),
api.NewTestField("parent").WithType(api.TypezString),
api.NewTestField("destination_parent").WithType(api.TypezString),
)
outputType := api.NewTestMessage("BillingResponse").
WithPackage("google.cloud.billing.v1")

listAccounts := api.NewTestMethod("ListBillingAccounts").
WithInput(inputType).
WithOutput(outputType).
WithBindings(
newTestPathBinding("GET", api.NewPathTemplateBuilder().WithLiteral("v1").WithLiteral("billingAccounts").Build()),
newTestPathBinding("GET", api.NewPathTemplateBuilder().WithLiteral("v1").WithVariableNamed("parent").WithLiteral("billingAccounts").Build()),
)

moveAccount := api.NewTestMethod("MoveBillingAccount").
WithInput(inputType).
WithOutput(outputType).
WithBindings(
newTestPathBinding("POST", api.NewPathTemplateBuilder().WithLiteral("v1").WithVariableNamed("name").WithVerb("move").Build()),
newTestPathBinding("GET", api.NewPathTemplateBuilder().WithLiteral("v1").WithVariableNamed("destination_parent").WithVariableNamed("name").WithVerb("move").Build()),
)

explicitOverride := api.NewTestMethod("ExplicitOverride").
WithInput(inputType).
WithOutput(outputType).
WithBindings(
newTestPathBinding("GET", api.NewPathTemplateBuilder().WithLiteral("v1").WithLiteral("billingAccounts").Build()),
newTestPathBinding("GET", api.NewPathTemplateBuilder().WithLiteral("v1").WithVariableNamed("parent").WithLiteral("billingAccounts").Build()),
).
WithRouting(api.NewTestRoutingInfo("bucket", api.NewTestRoutingInfoVariant("destination_parent")))

explicitEmpty := api.NewTestMethod("ExplicitEmpty").
WithInput(inputType).
WithOutput(outputType).
WithBindings(
newTestPathBinding("GET", api.NewPathTemplateBuilder().WithLiteral("v1").WithLiteral("billingAccounts").Build()),
newTestPathBinding("GET", api.NewPathTemplateBuilder().WithLiteral("v1").WithVariableNamed("parent").WithLiteral("billingAccounts").Build()),
).
WithRouting()

service := api.NewTestService("CloudBilling").
WithPackage("google.cloud.billing.v1").
WithMethods(listAccounts, moveAccount, explicitOverride, explicitEmpty)
model := api.NewTestAPI([]*api.Message{inputType, outputType}, nil, []*api.Service{service}).
WithPackageName("google.cloud.billing.v1")
if err := api.CrossReference(model); err != nil {
t.Fatal(err)
}
codec := newTestCodec(t, model, nil)
if err := codec.annotateModel(); err != nil {
t.Fatal(err)
}

for _, test := range []struct {
name string
method *api.Method
want []*routingParam
}{
{
name: "literal primary with additional binding variable",
method: listAccounts,
want: []*routingParam{newTestMultiWildcardRoutingParam("parent", "request.parent")},
},
{
name: "overlapping and secondary variables preserve order",
method: moveAccount,
want: []*routingParam{
newTestMultiWildcardRoutingParam("name", "request.name"),
newTestMultiWildcardRoutingParam("destination_parent", "request.destinationParent"),
},
},
{
name: "explicit routing overrides additional bindings",
method: explicitOverride,
want: []*routingParam{
{
RoutingKey: "bucket",
Variants: []*routingParamVariant{
{
FieldAccessor: "request.destinationParent",
Last: true,
},
},
},
},
},
{
name: "explicit empty routing suppresses additional bindings",
method: explicitEmpty,
want: nil,
},
} {
t.Run(test.name, func(t *testing.T) {
got := test.method.Codec().(*methodAnnotations)
if diff := cmp.Diff(test.want, got.RoutingParams); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
if got.HasRoutingParams() != (len(test.want) > 0) {
t.Errorf("HasRoutingParams() = %v, want %v", got.HasRoutingParams(), len(test.want) > 0)
}
})
}
}

func TestAnnotateMethod_SeeAlsoLinks(t *testing.T) {
nameField := api.NewTestField("name").WithType(api.TypezString)
typeField := api.NewTestField("type").WithType(api.TypezString)
Expand Down
24 changes: 24 additions & 0 deletions internal/sidekick/swift/format_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ func (c *codec) routingParamsFromRouting(routingInfos []*api.RoutingInfo) []*rou
// Extracts fallback routing parameters from a google.api.http path template when explicit
// google.api.routing annotations are absent per AIP-4222.
func (c *codec) routingParamsFromPathTemplate(t *api.PathTemplate) []*routingParam {
if t == nil {
return nil
}
var params []*routingParam
for _, segment := range t.Segments() {
if segment.Variable() != nil {
Expand All @@ -289,3 +292,24 @@ func (c *codec) routingParamsFromPathTemplate(t *api.PathTemplate) []*routingPar
}
return params
}

// routingParamsFromBindings extracts fallback routing parameters across all
// google.api.http bindings in declaration order, deduplicating repeated routing
// keys per AIP-4222.
func (c *codec) routingParamsFromBindings(bindings []*api.PathBinding) []*routingParam {
var params []*routingParam
seen := make(map[string]bool)
for _, binding := range bindings {
if binding == nil {
continue
}
for _, param := range c.routingParamsFromPathTemplate(binding.PathTemplate()) {
if seen[param.RoutingKey] {
continue
}
seen[param.RoutingKey] = true
params = append(params, param)
}
}
return params
}
144 changes: 129 additions & 15 deletions internal/sidekick/swift/format_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,26 +520,140 @@ func TestRoutingParamsFromPathTemplate(t *testing.T) {
t.Fatal(err)
}

template := api.NewPathTemplateBuilder().
WithLiteral("v2").
WithVariableNamed("name").
Build()
for _, test := range []struct {
name string
template *api.PathTemplate
want []*routingParam
}{
{
name: "nil template",
template: nil,
want: nil,
},
{
name: "template with variable",
template: api.NewPathTemplateBuilder().
WithLiteral("v2").
WithVariableNamed("name").
Build(),
want: []*routingParam{newTestMultiWildcardRoutingParam("name", "request.name")},
},
} {
t.Run(test.name, func(t *testing.T) {
got := codec.routingParamsFromPathTemplate(test.template)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}

got := codec.routingParamsFromPathTemplate(template)
func TestRoutingParamsFromBindings(t *testing.T) {
c := &codec{}

want := []*routingParam{
for _, test := range []struct {
name string
bindings []*api.PathBinding
want []*routingParam
}{
{
RoutingKey: "name",
Variants: []*routingParamVariant{
{
FieldAccessor: "request.name",
MatchingSegments: []string{".multiWildcard"},
Last: true,
},
name: "empty bindings",
bindings: nil,
want: nil,
},
{
name: "literal-only bindings",
bindings: []*api.PathBinding{
newTestPathBinding("GET", api.NewPathTemplateBuilder().WithLiteral("v1").WithLiteral("items").Build()),
},
want: nil,
},
{
name: "nil binding and nil template are skipped",
bindings: []*api.PathBinding{
nil,
api.NewPathBindingBuilder().WithVerb("GET").Build(),
newTestPathBinding("GET", api.NewPathTemplateBuilder().WithLiteral("v1").WithVariableNamed("parent").Build()),
},
want: []*routingParam{newTestMultiWildcardRoutingParam("parent", "request.parent")},
},
{
// CloudBilling.ListBillingAccounts: GET /v1/billingAccounts + GET /v1/{parent=organizations/*}/billingAccounts
name: "primary literal-only with variable in additional_bindings",
bindings: []*api.PathBinding{
newTestPathBinding("GET", api.NewPathTemplateBuilder().WithLiteral("v1").WithLiteral("billingAccounts").Build()),
newTestPathBinding("GET", api.NewPathTemplateBuilder().WithLiteral("v1").WithVariableNamed("parent").WithLiteral("billingAccounts").Build()),
},
want: []*routingParam{newTestMultiWildcardRoutingParam("parent", "request.parent")},
},
{
// CloudBilling.MoveBillingAccount: POST /v1/{name=billingAccounts/*}:move + GET /v1/{destination_parent=organizations/*}/{name=billingAccounts/*}:move
name: "overlapping and secondary-only variables preserve first-seen order",
bindings: []*api.PathBinding{
newTestPathBinding("POST", api.NewPathTemplateBuilder().WithLiteral("v1").WithVariableNamed("name").WithVerb("move").Build()),
newTestPathBinding("GET", api.NewPathTemplateBuilder().WithLiteral("v1").WithVariableNamed("destination_parent").WithVariableNamed("name").WithVerb("move").Build()),
},
want: []*routingParam{
newTestMultiWildcardRoutingParam("name", "request.name"),
newTestMultiWildcardRoutingParam("destination_parent", "request.destinationParent"),
},
},
{
// StorageControl.SummarizeIntelligenceFindings: repeated {parent=...} with distinct resource sub-patterns across 3 bindings
name: "distinct sub-patterns on same variable deduplicated",
bindings: []*api.PathBinding{
newTestPathBinding("GET", api.NewPathTemplateBuilder().
WithLiteral("v2").
WithVariable(api.NewPathVariableBuilder("parent").WithLiteral("projects").WithMatch().Build()).
WithLiteral("intelligenceFindings").
WithVerb("summarize").Build()),
newTestPathBinding("GET", api.NewPathTemplateBuilder().
WithLiteral("v2").
WithVariable(api.NewPathVariableBuilder("parent").WithLiteral("folders").WithMatch().Build()).
WithLiteral("intelligenceFindings").
WithVerb("summarize").Build()),
newTestPathBinding("GET", api.NewPathTemplateBuilder().
WithLiteral("v2").
WithVariable(api.NewPathVariableBuilder("parent").WithLiteral("organizations").WithMatch().Build()).
WithLiteral("intelligenceFindings").
WithVerb("summarize").Build()),
},
want: []*routingParam{newTestMultiWildcardRoutingParam("parent", "request.parent")},
},
{
name: "nested field path deduplication",
bindings: []*api.PathBinding{
newTestPathBinding("PATCH", api.NewPathTemplateBuilder().WithLiteral("v1").WithVariableNamed("book", "name").Build()),
newTestPathBinding("PATCH", api.NewPathTemplateBuilder().WithLiteral("v1").WithVariableNamed("shelf").WithVariableNamed("book", "name").Build()),
},
want: []*routingParam{
newTestMultiWildcardRoutingParam("book.name", "request.book?.name"),
newTestMultiWildcardRoutingParam("shelf", "request.shelf"),
},
},
} {
t.Run(test.name, func(t *testing.T) {
got := c.routingParamsFromBindings(test.bindings)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}

func newTestPathBinding(verb string, template *api.PathTemplate) *api.PathBinding {
return api.NewPathBindingBuilder().WithVerb(verb).WithPathTemplate(template).Build()
}

func newTestMultiWildcardRoutingParam(key, accessor string) *routingParam {
return &routingParam{
RoutingKey: key,
Variants: []*routingParamVariant{
{
FieldAccessor: accessor,
MatchingSegments: []string{".multiWildcard"},
Last: true,
},
},
}
}
Loading
Loading