Skip to content

Commit

Permalink
fix: handle default for parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Nov 21, 2024
1 parent 74c185a commit 63e6118
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/content/internal/arshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestRequestArshaler(t *testing.T) {
req, _ := http.NewRequest("GET", "http://0.0.0.0/users/{id}?limit=20&filter=1&filter=2", bytes.NewBufferString("test"))
req, _ := http.NewRequest("GET", "http://0.0.0.0/users/{id}?filter=1&filter=2", bytes.NewBufferString("test"))
req.Header.Set("Content-Type", "text/plain")
req.Header.Set("Cookie", "cookie=xxx")

Expand All @@ -35,15 +35,15 @@ func TestRequestArshaler(t *testing.T) {

testingx.Expect(t, op.ID, testingx.Be("1"))
testingx.Expect(t, op.ContentType, testingx.Be("text/plain"))
testingx.Expect(t, op.Limit, testingx.Be(20))
testingx.Expect(t, op.Limit, testingx.Be(10))
testingx.Expect(t, op.Filter, testingx.Equal([]string{"1", "2"}))
testingx.Expect(t, op.Data, testingx.Equal("test"))
testingx.Expect(t, op.Cookie, testingx.Equal("xxx"))

req2, err := internal.NewRequest(context.Background(), "GET", "/users/{id}", op)
testingx.Expect(t, err, testingx.BeNil[error]())
testingx.Expect(t, req2, testingutil.BeRequest(`
GET /users/1?filter=1&filter=2&limit=20 HTTP/1.1
GET /users/1?filter=1&filter=2&limit=10 HTTP/1.1
Content-Length: 4
Content-Type: text/plain; charset=utf-8
Cookie: cookie=xxx
Expand Down
6 changes: 6 additions & 0 deletions pkg/content/internal/param_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func (p *ParamValue) AddrValues(sf *jsonflags.StructField, n int) iter.Seq2[int,
}

func (p *ParamValue) UnmarshalValues(ctx context.Context, sf *jsonflags.StructField, values []string) error {
if len(values) == 0 {
if v, ok := sf.Tag.Lookup("default"); ok {
values = []string{v}
}
}

readers := make([]io.ReadCloser, len(values))
for i := range values {
readers[i] = io.NopCloser(bytes.NewBufferString(values[i]))
Expand Down

0 comments on commit 63e6118

Please sign in to comment.