Skip to content

Commit

Permalink
Fix "default" decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Aug 13, 2023
1 parent 2aad83f commit fabc396
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion _examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ require (
github.com/santhosh-tekuri/jsonschema/v3 v3.1.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/swaggest/form/v5 v5.1.1 // indirect
github.com/swaggest/refl v1.2.0 // indirect
github.com/swaggest/refl v1.2.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions _examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ github.com/swaggest/jsonschema-go v0.3.57 h1:n6D/2K9557Yqn/NohXoszmjuN0Lp5n0DyHl
github.com/swaggest/jsonschema-go v0.3.57/go.mod h1:5WFFGBBte5JAWAV8gDpNRJ/tlQnb1AHDdf/ghgsVUik=
github.com/swaggest/openapi-go v0.2.39 h1:GfICsAAFnQuyxfywsGyCbPqDKeMXxots4N/9j6+qSCk=
github.com/swaggest/openapi-go v0.2.39/go.mod h1:g+AfRIkPCHdhqfW8zOD1Sk3PwLhxpWW8SNWHXrmA08c=
github.com/swaggest/refl v1.2.0 h1:Qqqhfwi7REXF6/4cwJmj3gQMzl0Q0cYquxTYdD0kvi0=
github.com/swaggest/refl v1.2.0/go.mod h1:CkC6g7h1PW33KprTuYRSw8UUOslRUt4lF3oe7tTIgNU=
github.com/swaggest/refl v1.2.1 h1:1meX9NaXjM5lmb4kk4RP3OZsXFRke9B1EHAP/pCEKO0=
github.com/swaggest/refl v1.2.1/go.mod h1:CkC6g7h1PW33KprTuYRSw8UUOslRUt4lF3oe7tTIgNU=
github.com/swaggest/swgui v1.7.2 h1:N5hMPCQ+bIedVJoQDNjFUn8BqtISQDwaqEa76VkvzLs=
github.com/swaggest/swgui v1.7.2/go.mod h1:gGFKvKH+nmlPVXBc5S1/sUThCi2f+cthHaY2MfsWlAM=
github.com/swaggest/usecase v1.2.1 h1:XYVdK9tK2KCPglTflUi7aWBrVwIyb58D5mvGWED7pNs=
Expand Down
3 changes: 2 additions & 1 deletion chirouter/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ func (r *Wrapper) captureHandler(h http.Handler) {
}

func (r *Wrapper) prepareHandler(method, pattern string, h http.Handler) http.Handler {
h = nethttp.WrapHandler(h, nethttp.HandlerWithRouteMiddleware(method, r.resolvePattern(pattern)))
mw := nethttp.HandlerWithRouteMiddleware(method, r.resolvePattern(pattern))
h = nethttp.WrapHandler(h, mw)
r.handlers = append(r.handlers, h)
h = nethttp.WrapHandler(h, r.wraps...)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/swaggest/form/v5 v5.1.1
github.com/swaggest/jsonschema-go v0.3.57
github.com/swaggest/openapi-go v0.2.39
github.com/swaggest/refl v1.2.0
github.com/swaggest/refl v1.2.1
github.com/swaggest/usecase v1.2.1
)

Expand Down
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,11 @@ github.com/swaggest/form/v5 v5.1.1 h1:ct6/rOQBGrqWUQ0FUv3vW5sHvTUb31AwTUWj947N6c
github.com/swaggest/form/v5 v5.1.1/go.mod h1:X1hraaoONee20PMnGNLQpO32f9zbQ0Czfm7iZThuEKg=
github.com/swaggest/jsonschema-go v0.3.57 h1:n6D/2K9557Yqn/NohXoszmjuN0Lp5n0DyHlVLRZXbOM=
github.com/swaggest/jsonschema-go v0.3.57/go.mod h1:5WFFGBBte5JAWAV8gDpNRJ/tlQnb1AHDdf/ghgsVUik=
github.com/swaggest/openapi-go v0.2.38 h1:umFRZ2wg75eDAofQCMLfXf8eWBLuQNfU9jOjGxwFdng=
github.com/swaggest/openapi-go v0.2.38/go.mod h1:g+AfRIkPCHdhqfW8zOD1Sk3PwLhxpWW8SNWHXrmA08c=
github.com/swaggest/openapi-go v0.2.39-0.20230808224522-65df89a56f9b h1:z6txhy0FqQLgaD8MbWe9Zn3CHNb41IMlPKzEUFQcBdQ=
github.com/swaggest/openapi-go v0.2.39-0.20230808224522-65df89a56f9b/go.mod h1:g+AfRIkPCHdhqfW8zOD1Sk3PwLhxpWW8SNWHXrmA08c=
github.com/swaggest/openapi-go v0.2.39 h1:GfICsAAFnQuyxfywsGyCbPqDKeMXxots4N/9j6+qSCk=
github.com/swaggest/openapi-go v0.2.39/go.mod h1:g+AfRIkPCHdhqfW8zOD1Sk3PwLhxpWW8SNWHXrmA08c=
github.com/swaggest/refl v1.2.0 h1:Qqqhfwi7REXF6/4cwJmj3gQMzl0Q0cYquxTYdD0kvi0=
github.com/swaggest/refl v1.2.0/go.mod h1:CkC6g7h1PW33KprTuYRSw8UUOslRUt4lF3oe7tTIgNU=
github.com/swaggest/refl v1.2.1 h1:1meX9NaXjM5lmb4kk4RP3OZsXFRke9B1EHAP/pCEKO0=
github.com/swaggest/refl v1.2.1/go.mod h1:CkC6g7h1PW33KprTuYRSw8UUOslRUt4lF3oe7tTIgNU=
github.com/swaggest/usecase v1.2.1 h1:XYVdK9tK2KCPglTflUi7aWBrVwIyb58D5mvGWED7pNs=
github.com/swaggest/usecase v1.2.1/go.mod h1:5ccwVsLJ9eQpU4m0AGTM444pdqSPQBiocIwMmdRH9lQ=
github.com/yosuke-furukawa/json5 v0.1.2-0.20201207051438-cf7bb3f354ff h1:7YqG491bE4vstXRz1lD38rbSgbXnirvROz1lZiOnPO8=
Expand Down
5 changes: 4 additions & 1 deletion nethttp/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ func WrapHandler(h http.Handler, mw ...func(http.Handler) http.Handler) http.Han
panic("nil handler returned from middleware: " + runtime.FuncForPC(reflect.ValueOf(mw[i]).Pointer()).Name())
}

fp := reflect.ValueOf(mw[i]).Pointer()
mwName := runtime.FuncForPC(fp).Name()

h = &wrappedHandler{
Handler: w,
wrapped: h,
mwName: runtime.FuncForPC(reflect.ValueOf(mw[i]).Pointer()).Name(),
mwName: mwName,
}
}

Expand Down
13 changes: 13 additions & 0 deletions request/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,16 @@ func TestDecoder_Decode_unknownParams(t *testing.T) {
assert.Equal(t, rest.ValidationErrors{"query:quux": []string{"unknown parameter with value 123"}}, err,
fmt.Sprintf("%#v", err))
}

func TestDecoderFactory_MakeDecoder_default_unexported(t *testing.T) {
type showImageInput struct {
Hash int `path:"hash"`
req *http.Request
}

f := request.NewDecoderFactory()
f.ApplyDefaults = true

dec := f.MakeDecoder(http.MethodGet, showImageInput{}, nil)
assert.NotNil(t, dec)
}
6 changes: 3 additions & 3 deletions request/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func DecoderMiddleware(factory DecoderMaker) func(http.Handler) http.Handler {

input := useCaseWithInput.InputPort()
if input != nil {
setRequestDecoder.SetRequestDecoder(
factory.MakeDecoder(withRoute.RouteMethod(), useCaseWithInput.InputPort(), customMapping),
)
method := withRoute.RouteMethod()
dec := factory.MakeDecoder(method, input, customMapping)
setRequestDecoder.SetRequestDecoder(dec)
}

return handler
Expand Down
3 changes: 2 additions & 1 deletion web/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ func (s *Service) Delete(pattern string, uc usecase.Interactor, options ...func(

// Get adds the route `pattern` that matches a GET http method to invoke use case interactor.
func (s *Service) Get(pattern string, uc usecase.Interactor, options ...func(h *nethttp.Handler)) {
s.Method(http.MethodGet, pattern, nethttp.NewHandler(uc, options...))
h := nethttp.NewHandler(uc, options...)
s.Method(http.MethodGet, pattern, h)
}

// Head adds the route `pattern` that matches a HEAD http method to invoke use case interactor.
Expand Down

0 comments on commit fabc396

Please sign in to comment.