Skip to content

Commit

Permalink
Fix unsupported param type url.URL
Browse files Browse the repository at this point in the history
 closes #583
  • Loading branch information
ddl-ebrown committed Sep 19, 2024
1 parent 69d32c3 commit 73191d4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 11 additions & 0 deletions huma.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"io"
"net"
"net/http"
"net/url"
"reflect"
"regexp"
"slices"
Expand Down Expand Up @@ -1127,6 +1128,16 @@ func Register[I, O any](api API, op Operation, handler func(context.Context, *I)
f.Set(reflect.ValueOf(t))
pv = value
break
// Special case: url.URL
} else if f.Type() == urlType {
u, err := url.Parse(value)
if err != nil {
res.Add(pb, value, "invalid url.URL value")
return
}
f.Set(reflect.ValueOf(*u))
pv = value
break

Check warning on line 1140 in huma.go

View check run for this annotation

Codecov / codecov/patch

huma.go#L1138-L1140

Added lines #L1138 - L1140 were not covered by tests
}

// Last resort: use the `encoding.TextUnmarshaler` interface.
Expand Down
5 changes: 4 additions & 1 deletion huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -434,6 +435,7 @@ func TestFeatures(t *testing.T) {
QueryFloat float32 `query:"float"`
QueryBefore time.Time `query:"before"`
QueryDate time.Time `query:"date" timeFormat:"2006-01-02"`
QueryURL url.URL `query:"url"`
QueryUint uint32 `query:"uint"`
QueryBool bool `query:"bool"`
QueryInts []int `query:"ints"`
Expand All @@ -454,14 +456,15 @@ func TestFeatures(t *testing.T) {
})
},
Method: http.MethodGet,
URL: "/test-params/bad/not-a-uuid?int=bad&float=bad&before=bad&date=bad&uint=bad&bool=bad&ints=bad&ints8=bad&ints16=bad&ints32=bad&ints64=bad&uints=bad&uints16=bad&uints32=bad&uints64=bad&floats32=bad&floats64=bad",
URL: "/test-params/bad/not-a-uuid?int=bad&float=bad&before=bad&date=bad&url=:&uint=bad&bool=bad&ints=bad&ints8=bad&ints16=bad&ints32=bad&ints64=bad&uints=bad&uints16=bad&uints32=bad&uints64=bad&floats32=bad&floats64=bad",
Assert: func(t *testing.T, resp *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code)

assert.Contains(t, resp.Body.String(), "invalid integer")
assert.Contains(t, resp.Body.String(), "invalid value: invalid UUID length: 10")
assert.Contains(t, resp.Body.String(), "invalid float")
assert.Contains(t, resp.Body.String(), "invalid date/time")
assert.Contains(t, resp.Body.String(), "invalid url.URL")
assert.Contains(t, resp.Body.String(), "invalid bool")
assert.Contains(t, resp.Body.String(), "required query parameter is missing")
assert.Contains(t, resp.Body.String(), "required header parameter is missing")
Expand Down

0 comments on commit 73191d4

Please sign in to comment.