Skip to content

Commit

Permalink
fix dubbo option mapTo throwing unexpected errors (apache#177)
Browse files Browse the repository at this point in the history
Former-commit-id: b5ed650
  • Loading branch information
williamfeng323 committed May 26, 2021
1 parent 18deed6 commit 24e2e51
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
6 changes: 3 additions & 3 deletions pkg/client/dubbo/dubbo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions pkg/client/dubbo/dubbo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/client/dubbo/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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])
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 24e2e51

Please sign in to comment.