Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle double slash in Files API #712

Merged
merged 1 commit into from
Nov 28, 2023
Merged
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
6 changes: 6 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/url"
"strings"
"time"

"github.com/databricks/databricks-sdk-go/apierr"
Expand Down Expand Up @@ -108,6 +109,11 @@ func (c *DatabricksClient) Do(ctx context.Context, method, path string,
opts = append(opts, httpclient.WithRequestHeaders(headers))
opts = append(opts, httpclient.WithRequestData(request))
opts = append(opts, httpclient.WithResponseUnmarshal(response))
// Remove extra `/` from path for files API.
Copy link
Contributor

@nfx nfx Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mgyucht it's better applied in the RequestVisitors slice, as it'll be more forward-compatible.

I wonder if we can trim+add a slash from end of URL and remove the slash from the beginning of the path? it'll be more logical and fit more cases

Copy link
Contributor Author

@mgyucht mgyucht Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is at its core a bug in the files API spec. I don't want to support this in general and will fix it soon, after which I'll remove it. So consider this change to be temporary until the next OpenAPI bump.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's better do a more proactive change #713

// Once the OpenAPI spec doesn't include the extra slash, we can remove this
if strings.HasPrefix(path, "/api/2.0/fs/files//") {
path = strings.Replace(path, "/api/2.0/fs/files//", "/api/2.0/fs/files/", 1)
}
return c.client.Do(ctx, method, path, opts...)
}

Expand Down
28 changes: 28 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/databricks-sdk-go/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -219,3 +220,30 @@ func TestHttpTransport(t *testing.T) {
require.NoError(t, err)
require.True(t, calledMock)
}

func TestDoRemovesDoubleSlashesFromFilesAPI(t *testing.T) {
i := 0
expectedPaths := []string{
"/api/2.0/fs/files/Volumes/abc/def/ghi",
"/api/2.0/anotherservice//test",
}
c, err := New(&config.Config{
Host: "some",
Token: "token",
ConfigFile: "/dev/null",
HTTPTransport: hc(func(r *http.Request) (*http.Response, error) {
assert.Equal(t, expectedPaths[i], r.URL.Path)
i++
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(`{}`)),
Request: r,
}, nil
}),
})
require.NoError(t, err)
err = c.Do(context.Background(), "GET", "/api/2.0/fs/files//Volumes/abc/def/ghi", nil, map[string]any{}, nil)
require.NoError(t, err)
err = c.Do(context.Background(), "GET", "/api/2.0/anotherservice//test", nil, map[string]any{}, nil)
require.NoError(t, err)
}