Skip to content

Commit

Permalink
Merge pull request #797 from mapbox/NAVIOS-892
Browse files Browse the repository at this point in the history
Fix possible crash if only one element array was passed to DirectionOptions
  • Loading branch information
kried committed Feb 2, 2023
2 parents 1f87861 + a2cf98c commit a42b3b8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v2.10.0

* Added `Matrix` API wrapper. The [Mapbox Matrix API](https://docs.mapbox.com/api/navigation/matrix/) computes travel times between many points, and returns a matrix of all travel times between the locations. [#626](https://github.com/mapbox/mapbox-directions-swift/pull/626)
* Fixed a crash that could occur if only one `Waypoint` with a nonnull `name` was used in the Directions request. [#797](https://github.com/mapbox/mapbox-directions-swift/pull/797)

## v2.9.0

Expand Down
4 changes: 3 additions & 1 deletion Sources/MapboxDirections/DirectionsOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ open class DirectionsOptions: Codable {
*/
var legSeparators: [Waypoint] {
var waypoints = self.waypoints
guard waypoints.count > 1 else { return [] }

let source = waypoints.removeFirst()
let destination = waypoints.removeLast()
return [source] + waypoints.filter { $0.separatesLegs } + [destination]
Expand Down Expand Up @@ -563,7 +565,7 @@ open class DirectionsOptions: Codable {
}

private var waypointNames: String? {
if waypoints.compactMap({ $0.name }).isEmpty {
guard !waypoints.compactMap({ $0.name }).isEmpty, waypoints.count > 1 else {
return nil
}
return legSeparators.map({ $0.name ?? "" }).joined(separator: ";")
Expand Down
26 changes: 24 additions & 2 deletions Tests/MapboxDirectionsTests/RouteOptionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,34 @@ class RouteOptionsTests: XCTestCase {
let expectedIncludeQueryItem = URLQueryItem(name: "include", value: "hov2,hov3,hot")
XCTAssertTrue(options.urlQueryItems.contains(expectedIncludeQueryItem))
}
func testNoWaypointsAndOneWaypoint() {

func testReturnPathIfNoWaypointsAndOneWaypoint() {
let noWaypointOptions = RouteOptions(coordinates: [])
XCTAssertEqual(noWaypointOptions.path, noWaypointOptions.abridgedPath)

let oneWaypointOptions = RouteOptions(coordinates: [LocationCoordinate2D(latitude: 0.0, longitude: 0.0)])
XCTAssertEqual(oneWaypointOptions.path, oneWaypointOptions.abridgedPath)

let waypoints = [
Waypoint(coordinate: LocationCoordinate2D(latitude: 0.0, longitude: 0.0), name: "name")
]
let oneWaypointOptionsWithNonNilName = RouteOptions(waypoints: waypoints)
XCTAssertEqual(oneWaypointOptionsWithNonNilName.path, oneWaypointOptionsWithNonNilName.abridgedPath)
}

func testReturnUrlQueryWaypoinNameItemsIfNoWaypointsAndOneWaypoint() {
let noWaypointOptions = RouteOptions(coordinates: [])
XCTAssertFalse(noWaypointOptions.urlQueryItems.map { $0.name }.contains("waypoint_names"))

let oneWaypointOptionsWithNilName = RouteOptions(coordinates: [LocationCoordinate2D(latitude: 0.0, longitude: 0.0)])
XCTAssertFalse(oneWaypointOptionsWithNilName.urlQueryItems.map { $0.name }.contains("waypoint_names"))

let waypoints = [
Waypoint(coordinate: LocationCoordinate2D(latitude: 0.0, longitude: 0.0), name: "name")
]

let oneWaypointOptionsWithNonNilName = RouteOptions(waypoints: waypoints)
XCTAssertFalse(oneWaypointOptionsWithNonNilName.urlQueryItems.map { $0.name }.contains("waypoint_names"))
}
}

Expand Down

0 comments on commit a42b3b8

Please sign in to comment.