Copilot commented on code in PR #1174:
URL: https://github.com/apache/iceberg-go/pull/1174#discussion_r3376778776


##########
catalog/rest/endpoints_integration_test.go:
##########
@@ -0,0 +1,47 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//go:build integration
+
+package rest
+
+import (
+       "context"
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+       "github.com/stretchr/testify/require"
+)
+
+func TestEndpointNegotiation(t *testing.T) {
+       // Loading the catalog reads /v1/config and negotiates the endpoint set 
the
+       // fixture advertises. The live server uses the same wire format as our
+       // endpoint constants, so the negotiated set must contain the core
+       // operations; a format mismatch would surface here as a missing entry
+       // rather than as a silent fallback.
+       cat, err := NewCatalog(context.Background(), "rest", 
"http://localhost:8181";)
+       require.NoError(t, err)
+
+       require.NotEmpty(t, cat.endpoints, "fixture should advertise an 
endpoints list")
+       for _, want := range []endpoint{

Review Comment:
   `require.NotEmpty(t, cat.endpoints, ...)` doesn't actually verify that the 
fixture advertised an `endpoints` list: `resolveEndpoints(nil/empty)` falls 
back to `fallbackEndpoints`, which is always non-empty. As written, this 
assertion will pass even when the server advertises no endpoints, so it won't 
catch a silent fallback.



##########
catalog/rest/rest.go:
##########
@@ -796,7 +802,7 @@ func (r *Catalog) fetchTableCreds(ctx context.Context, 
ident []string, location
                return nil, err
        }
 
-       ret, err := doGet[loadCredentialsResponse](ctx, r.baseURI, 
[]string{"namespaces", ns, "tables", tbl, "credentials"},
+       ret, err := doGet[loadCredentialsResponse](ctx, r.baseURI, 
endpointTableCredentials.reqPath(ns, tbl),
                r.cl, map[int]error{http.StatusNotFound: 
catalog.ErrNoSuchTable})

Review Comment:
   `fetchTableCreds` uses the negotiated request path 
(`endpointTableCredentials.reqPath`) but does not check whether the server 
advertised the credentials endpoint. This means catalogs that omit `GET 
/namespaces/{namespace}/tables/{table}/credentials` will fail later with a 
generic REST error instead of the intended `ErrEndpointNotSupported` gating.



##########
catalog/rest/endpoints.go:
##########
@@ -0,0 +1,173 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package rest
+
+import (
+       "fmt"
+       "net/http"
+       "strings"
+)
+
+// ErrEndpointNotSupported indicates the server did not advertise a REST 
endpoint
+// required by the requested operation. It wraps [ErrRESTError].
+var ErrEndpointNotSupported = fmt.Errorf("%w: endpoint not supported by 
server", ErrRESTError)
+
+// pathPrefix is the part of every endpoint template already encoded in the 
base
+// URI (API version plus optional catalog prefix); reqPath strips it.
+const pathPrefix = "/v1/{prefix}"
+
+// endpoint identifies a REST catalog operation by HTTP method and path 
template
+// (e.g. "GET /v1/{prefix}/namespaces"). Servers advertise the endpoints they
+// support in the config response so the client can negotiate capabilities.
+//
+// Each endpoint both gates capability and builds the request path (see
+// [endpoint.reqPath]), making it the single source of truth for its operation.
+type endpoint struct {
+       method string
+       path   string
+}
+
+func (e endpoint) String() string { return e.method + " " + e.path }
+
+// reqPath renders the request path relative to the base URI, substituting 
params
+// in order for the "{...}" placeholders that follow [pathPrefix].
+func (e endpoint) reqPath(params ...string) []string {
+       segs := strings.Split(strings.TrimPrefix(e.path, pathPrefix+"/"), "/")
+
+       out := make([]string, len(segs))
+       p := 0
+       for i, s := range segs {
+               if strings.HasPrefix(s, "{") && strings.HasSuffix(s, "}") {
+                       out[i], p = params[p], p+1
+               } else {
+                       out[i] = s
+               }
+       }

Review Comment:
   `endpoint.reqPath` indexes into `params` for every `{...}` placeholder 
without validating the parameter count. If a caller accidentally supplies too 
few params for an endpoint template, this will panic with an index-out-of-range 
at runtime. Adding a small bounds check avoids crashing the process and makes 
misuse easier to diagnose.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to