This is an automated email from the ASF dual-hosted git repository.

xiaoliu pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go-pixiu.git


The following commit(s) were added to refs/heads/develop by this push:
     new b5ed650  fix dubbo option mapTo throwing unexpected errors (#177)
b5ed650 is described below

commit b5ed650bb6157ceb2b960454b364792648fb7201
Author: williamfeng323 <[email protected]>
AuthorDate: Wed May 26 21:08:29 2021 +0800

    fix dubbo option mapTo throwing unexpected errors (#177)
---
 pkg/client/dubbo/dubbo.go      |  6 +++---
 pkg/client/dubbo/dubbo_test.go | 33 +++++++++++++++++++++++++++++++++
 pkg/client/dubbo/mapper.go     |  8 ++++----
 3 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/pkg/client/dubbo/dubbo.go b/pkg/client/dubbo/dubbo.go
index 465df54..0c828eb 100644
--- a/pkg/client/dubbo/dubbo.go
+++ b/pkg/client/dubbo/dubbo.go
@@ -139,13 +139,13 @@ func (dc *Client) Close() error {
 // Call invoke service
 func (dc *Client) Call(req *client.Request) (res interface{}, err error) {
        values, err := dc.genericArgs(req)
+       if err != nil {
+               return nil, err
+       }
        val, ok := values.(*dubboTarget)
        if !ok {
                return nil, errors.New("map parameters failed")
        }
-       if err != nil {
-               return nil, err
-       }
 
        dm := req.API.Method.IntegrationRequest
        method := dm.Method
diff --git a/pkg/client/dubbo/dubbo_test.go b/pkg/client/dubbo/dubbo_test.go
index 8dcf922..1bd3f69 100644
--- a/pkg/client/dubbo/dubbo_test.go
+++ b/pkg/client/dubbo/dubbo_test.go
@@ -176,6 +176,39 @@ func TestMappingParams(t *testing.T) {
        assert.Equal(t, params.(*dubboTarget).Values[2], "1234567")
        assert.Equal(t, params.(*dubboTarget).Values[3], "male")
        assert.Equal(t, params.(*dubboTarget).Values[4], "Joe")
+
+       r, _ = http.NewRequest("POST", "/mock/test?id=12345&age=19", 
bytes.NewReader([]byte(`{"sex": "male", "name":{"firstName": "Joe", "lastName": 
"Biden"}}`)))
+       api = mock.GetMockAPI(config.MethodGet, "/mock/test")
+       api.IntegrationRequest.MappingParams = []config.MappingParam{
+               {
+                       Name:  "queryStrings.id",
+                       MapTo: "opt.method",
+               },
+               {
+                       Name:  "queryStrings.age",
+                       MapTo: "opt.application",
+               },
+               {
+                       Name:  "headers.Auth",
+                       MapTo: "opt.group",
+               },
+               {
+                       Name:  "requestBody.sex",
+                       MapTo: "opt.values",
+               },
+               {
+                       Name:  "requestBody.name.firstName",
+                       MapTo: "opt.interface",
+               },
+       }
+       r.Header.Set("Auth", "1234567")
+       req = client.NewReq(context.TODO(), r, api)
+       _, err = dClient.MapParams(req)
+       assert.Nil(t, err)
+       assert.Equal(t, 
req.API.Method.IntegrationRequest.DubboBackendConfig.ApplicationName, "19")
+       assert.Equal(t, 
req.API.Method.IntegrationRequest.DubboBackendConfig.Group, "1234567")
+       assert.Equal(t, 
req.API.Method.IntegrationRequest.DubboBackendConfig.Interface, "Joe")
+       assert.Equal(t, 
req.API.Method.IntegrationRequest.DubboBackendConfig.Method, "12345")
 }
 
 func TestBuildOption(t *testing.T) {
diff --git a/pkg/client/dubbo/mapper.go b/pkg/client/dubbo/mapper.go
index f697ec2..efbf4f4 100644
--- a/pkg/client/dubbo/mapper.go
+++ b/pkg/client/dubbo/mapper.go
@@ -92,7 +92,7 @@ func (qm queryStringsMapper) Map(mp config.MappingParam, c 
*client.Request, targ
                return err
        }
        pos, err := strconv.Atoi(mp.MapTo)
-       if err != nil {
+       if err != nil && option == nil {
                return errors.Errorf("Parameter mapping %v incorrect", mp)
        }
        qValue := queryValues.Get(key[0])
@@ -113,7 +113,7 @@ func (hm headerMapper) Map(mp config.MappingParam, c 
*client.Request, target int
        }
        _, key, err := client.ParseMapSource(mp.Name)
        pos, err := strconv.Atoi(mp.MapTo)
-       if err != nil {
+       if err != nil && option == nil {
                return errors.Errorf("Parameter mapping %+v incorrect", mp)
        }
        header := c.IngressRequest.Header.Get(key[0])
@@ -138,7 +138,7 @@ func (bm bodyMapper) Map(mp config.MappingParam, c 
*client.Request, target inter
                return err
        }
        pos, err := strconv.Atoi(mp.MapTo)
-       if err != nil {
+       if err != nil && option == nil {
                return errors.Errorf("Parameter mapping %v incorrect, 
parameters for Dubbo backend must be mapped to an int to represent position", 
mp)
        }
 
@@ -174,7 +174,7 @@ func (um uriMapper) Map(mp config.MappingParam, c 
*client.Request, target interf
                return err
        }
        pos, err := strconv.Atoi(mp.MapTo)
-       if err != nil {
+       if err != nil && option == nil {
                return errors.Errorf("Parameter mapping %v incorrect", mp)
        }
        uriValues := router.GetURIParams(&c.API, *c.IngressRequest.URL)

Reply via email to