Skip to content

Commit

Permalink
fix(v8): correct route query params
Browse files Browse the repository at this point in the history
Multiple query parameter values for the same key should be comma separated.
  • Loading branch information
philiphassel committed Apr 13, 2023
1 parent ff9ab93 commit de7722f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
27 changes: 14 additions & 13 deletions routingv8/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ func (s *RoutingService) Routes(
}

values := make(url.Values)
returns := []ReturnAttribute{"summary"}
returns := make([]string, 0, len(req.Return))
if len(req.Return) > 0 {
returns = req.Return
}
for _, attribute := range returns {
values.Add("return", string(attribute))
for _, attribute := range req.Return {
returns = append(returns, string(attribute))
}
} else {
returns = []string{string(SummaryReturnAttribute)}
}
values.Add("return", strings.Join(returns, ","))
if req.DepartureTime != "" {
values.Add("departureTime", req.DepartureTime)
}
Expand All @@ -41,18 +43,17 @@ func (s *RoutingService) Routes(
values.Add("destination", fmt.Sprintf("%v,%v", req.Destination.Lat, req.Destination.Long))

if req.AvoidAreas != nil {
var avoidString string
for _, a := range req.AvoidAreas {
avoid := a.String()
if avoid == invalid {
areas := make([]string, 0, len(req.AvoidAreas))
for _, area := range req.AvoidAreas {
a := area.String()
if a == invalid {
return nil, fmt.Errorf("invalid avoid area")
}
if avoid != unspecified {
avoidString += fmt.Sprintf("%s,%s", avoidString, avoid)
if a != unspecified {
areas = append(areas, a)
}
}
avoidString = strings.Trim(avoidString, ",")
values.Add("avoid[features]", avoidString)
values.Add("avoid[features]", strings.Join(areas, ","))
}

r, err := s.Client.NewRequest(ctx, u, http.MethodGet, values.Encode(), nil)
Expand Down
30 changes: 30 additions & 0 deletions routingv8/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,36 @@ func TestRoutingervice_Routes_QueryParams(t *testing.T) {
expected: "destination=59.337492%2C18.063672&origin=57.707752%2C11.949767" +
"&return=summary&transportMode=car",
},
{
name: "multiple avoid areas",
request: &routingv8.RoutesRequest{
Origin: origin,
Destination: destination,
TransportMode: routingv8.TransportModeCar,
AvoidAreas: []routingv8.AreaFeature{
routingv8.AreaFeatureFerry,
routingv8.AreaFeatureTollRoad,
routingv8.AreaFeatureTunnel,
routingv8.AreaFeatureControlledAccessHighway,
},
},
expected: "avoid%5Bfeatures%5D=ferry%2CtollRoad%2Ctunnel%2CcontrolledAccessHighway" +
"&destination=59.337492%2C18.063672&origin=57.707752%2C11.949767&return=summary&transportMode=car",
},
{
name: "multiple return attributes",
request: &routingv8.RoutesRequest{
Origin: origin,
Destination: destination,
TransportMode: routingv8.TransportModeCar,
Return: []routingv8.ReturnAttribute{
routingv8.SummaryReturnAttribute,
routingv8.PolylineReturnAttribute,
},
},
expected: "destination=59.337492%2C18.063672&origin=57.707752%2C11.949767" +
"&return=summary%2Cpolyline&transportMode=car",
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit de7722f

Please sign in to comment.