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

Properly escape multi-segment path parameters #869

Merged
merged 8 commits into from
Mar 28, 2024
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
20 changes: 17 additions & 3 deletions .codegen/impl.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"net/http"
"github.com/databricks/databricks-sdk-go/client"
"github.com/databricks/databricks-sdk-go/httpclient"
{{range .ImportedPackages}}
"github.com/databricks/databricks-sdk-go/service/{{.}}"{{end}}
)
Expand All @@ -21,16 +22,29 @@ type {{.CamelName}}Impl struct {
{{range .Methods}}
func (a *{{.Service.CamelName}}Impl) {{.PascalName}}(ctx context.Context{{if .Request}}, request {{.Request.PascalName}}{{end}}) {{ template "response-type" . }} {
{{- template "response-var" . }}
path := {{if .PathParts -}}
fmt.Sprintf("{{range .PathParts}}{{.Prefix}}{{if or .Field .IsAccountId}}%v{{end}}{{ end }}"{{ range .PathParts }}{{if .Field}}, request.{{.Field.PascalName}}{{ else if .IsAccountId }}, a.client.ConfiguredAccountID(){{end}}{{ end }})
{{- else}}"{{.Path}}"{{end}}
path := {{ template "path" . }}
{{ template "make-header" . }}
err := a.client.Do(ctx, http.Method{{.TitleVerb}}, path, headers, {{ template "request-param" . }}, {{if .Response}}&{{.Response.CamelName}}{{else}}nil{{end}})
return {{ template "response" . }}
}
{{end -}}
{{end}}

{{- define "path" -}}
{{- if .PathParts -}}
fmt.Sprintf("{{range .PathParts -}}
{{.Prefix}}{{if or .Field .IsAccountId}}%v{{end}}{{ end }}"
{{- range .PathParts -}}
{{- if and .Field .Field.IsPathMultiSegment}}, httpclient.EncodeMultiSegmentPathParameter(request.{{.Field.PascalName}})
{{- else if .Field}}, request.{{.Field.PascalName}}
{{- else if .IsAccountId }}, a.client.ConfiguredAccountID()
{{- end -}}
{{- end -}})
{{- else -}}
"{{.Path}}"
{{- end -}}
{{- end -}}

{{ define "request-param" -}}
{{ if or (and .Request (or (eq .Verb "GET") (eq .Verb "DELETE") (eq .Verb "HEAD"))) (and .Operation .Operation.RequestBody) -}}
request{{ if .RequestBodyField }}.{{.RequestBodyField.PascalName}}{{end}}
Expand Down
13 changes: 13 additions & 0 deletions httpclient/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ func makeQueryString(data interface{}) (string, error) {
return "", fmt.Errorf("unsupported query string data: %#v", data)
}

func EncodeMultiSegmentPathParameter(p string) string {
segments := strings.Split(p, "/")
b := strings.Builder{}
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be preallocated to at least len(p).

b.Grow(len(p))
for i, s := range segments {
if i > 0 {
b.WriteString("/")
}
b.WriteString(url.PathEscape(s))
}
return b.String()
}

func makeRequestBody(method string, requestURL *string, data interface{}) (common.RequestBody, error) {
if data == nil {
return common.RequestBody{}, nil
Expand Down
8 changes: 8 additions & 0 deletions httpclient/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
)
Expand Down Expand Up @@ -118,3 +119,10 @@ func TestWithTokenSource(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "awesome token", buf.String())
}

func TestEncodeMultiSegmentPathParameter(t *testing.T) {
// Slashes should not be encoded.
assert.Equal(t, "a/b/c", EncodeMultiSegmentPathParameter("a/b/c"))
// # and ? should be encoded.
assert.Equal(t, "a%23b%3Fc", EncodeMultiSegmentPathParameter("a#b?c"))
}
16 changes: 8 additions & 8 deletions internal/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ func (buf hashable) Hash() uint32 {
return h.Sum32()
}

func TestAccUCUploadAndDownloadFilesAPI(t *testing.T) {
func TestUcAccFilesUploadAndDownload(t *testing.T) {
ctx, w, volume := setupUCVolume(t)

filePath := RandomName("/Volumes/" + volume.CatalogName + "/" + volume.SchemaName + "/" + volume.Name + "/files-")
filePath := RandomName("/Volumes/" + volume.CatalogName + "/" + volume.SchemaName + "/" + volume.Name + "/files-with-?-and-#-")
err := w.Files.Upload(ctx, files.UploadRequest{
FilePath: filePath,
Contents: io.NopCloser(strings.NewReader("abcd")),
Expand All @@ -57,7 +57,7 @@ func TestAccUCUploadAndDownloadFilesAPI(t *testing.T) {
assert.Equal(t, "abcd", string(contents))
}

func TestAccUCDeleteFile(t *testing.T) {
func TestUcAccFilesDelete(t *testing.T) {
ctx, w, volume := setupUCVolume(t)

filePath := RandomName("/Volumes/" + volume.CatalogName + "/" + volume.SchemaName + "/" + volume.Name + "/file-")
Expand All @@ -67,7 +67,7 @@ func TestAccUCDeleteFile(t *testing.T) {
require.NoError(t, err)
}

func TestAccUCGetMetadata(t *testing.T) {
func TestUcAccFilesGetMetadata(t *testing.T) {
ctx, w, volume := setupUCVolume(t)

filePath := RandomName("/Volumes/" + volume.CatalogName + "/" + volume.SchemaName + "/" + volume.Name + "/file-")
Expand All @@ -81,14 +81,14 @@ func TestAccUCGetMetadata(t *testing.T) {
assert.NotEmpty(t, metadata.LastModified)
}

func TestAccUCCreateDirectory(t *testing.T) {
func TestUcAccFilesCreateDirectory(t *testing.T) {
ctx, w, volume := setupUCVolume(t)

directoryPath := RandomName("/Volumes/" + volume.CatalogName + "/" + volume.SchemaName + "/" + volume.Name + "/directory-")
require.NoError(t, createDirectory(t, ctx, w, directoryPath))
}

func TestAccUCListDirectoryContents(t *testing.T) {
func TestUcAccFilesListDirectoryContents(t *testing.T) {
ctx, w, volume := setupUCVolume(t)

directoryPath := RandomName("/Volumes/" + volume.CatalogName + "/" + volume.SchemaName + "/" + volume.Name + "/directory-")
Expand All @@ -102,7 +102,7 @@ func TestAccUCListDirectoryContents(t *testing.T) {
assert.Len(t, response, 3)
}

func TestAccUCDeleteDirectory(t *testing.T) {
func TestUcAccFilesDeleteDirectory(t *testing.T) {
ctx, w, volume := setupUCVolume(t)

directoryPath := RandomName("/Volumes/" + volume.CatalogName + "/" + volume.SchemaName + "/" + volume.Name + "/directory-")
Expand All @@ -112,7 +112,7 @@ func TestAccUCDeleteDirectory(t *testing.T) {
assert.NoError(t, err)
}

func TestAccUCGetDirectoryMetadata(t *testing.T) {
func TestUcAccFilesGetDirectoryMetadata(t *testing.T) {
ctx, w, volume := setupUCVolume(t)

directoryPath := RandomName("/Volumes/" + volume.CatalogName + "/" + volume.SchemaName + "/" + volume.Name + "/directory-")
Expand Down
17 changes: 9 additions & 8 deletions openapi/code/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
// Field of a Type (Entity)
type Field struct {
Named
Required bool
Entity *Entity
Of *Entity
IsJson bool
IsPath bool
IsQuery bool
IsHeader bool
Schema *openapi.Schema
Required bool
Entity *Entity
Of *Entity
IsJson bool
IsPath bool
IsPathMultiSegment bool
IsQuery bool
IsHeader bool
Schema *openapi.Schema
}

func (f *Field) IsOptionalObject() bool {
Expand Down
12 changes: 7 additions & 5 deletions openapi/code/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ func (svc *Service) getDescription(param openapi.Parameter) string {
func (svc *Service) paramToField(op *openapi.Operation, param openapi.Parameter) *Field {
named := Named{param.Name, svc.getDescription(param)}
return &Field{
Named: named,
Required: param.Required,
IsPath: param.In == "path",
IsQuery: param.In == "query",
IsHeader: param.In == "header",
Named: named,
Required: param.Required,
IsPath: param.In == "path",
IsPathMultiSegment: param.MultiSegment,
IsQuery: param.In == "query",
IsHeader: param.In == "header",
Entity: svc.Package.schemaToEntity(param.Schema, []string{
op.Name(),
named.PascalName(),
Expand Down Expand Up @@ -254,6 +255,7 @@ func (svc *Service) addParams(request *Entity, op *openapi.Operation, params []o
field = param
}
field.IsPath = param.IsPath
field.IsPathMultiSegment = param.IsPathMultiSegment
field.IsQuery = param.IsQuery
field.IsHeader = param.IsHeader
request.fields[param.Name] = field
Expand Down
17 changes: 9 additions & 8 deletions service/files/impl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/iam/model.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion service/sql/model.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading