Skip to content

Commit

Permalink
Fixed decoding of unrecognized visual instruction component types
Browse files Browse the repository at this point in the history
Fixed an issue where an unrecognized type of visual instruction component would cause the component to fail to decode. Instead, treat the component as a text component, as specified in the Directions API documentation.
  • Loading branch information
1ec5 committed Dec 20, 2019
1 parent b4b013a commit b8a3c84
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/MapboxDirections/VisualInstructionComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ extension VisualInstruction.Component: Codable {

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let kind = try container.decode(Kind.self, forKey: .kind)
let kind = (try? container.decode(Kind.self, forKey: .kind)) ?? .text

if kind == .lane {
let indications = try container.decode(LaneIndication.self, forKey: .directions)
Expand Down
19 changes: 19 additions & 0 deletions Tests/MapboxDirectionsTests/VisualInstructionComponentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,23 @@ class VisualInstructionComponentTests: XCTestCase {
XCTAssert(JSONSerialization.objectsAreEqual(componentJSON, encodedComponentJSON, approximate: false))
}
}

func testUnrecognizedComponent() {
let componentJSON = [
"type": "emoji",
"text": "👈",
]
let componentData = try! JSONSerialization.data(withJSONObject: componentJSON, options: [])
var component: VisualInstruction.Component?
XCTAssertNoThrow(component = try JSONDecoder().decode(VisualInstruction.Component.self, from: componentData))
XCTAssertNotNil(component)
if let component = component {
switch component {
case .text(let text):
XCTAssertEqual(text.text, "👈")
default:
XCTFail("Component of unrecognized type should be decoded as text component.")
}
}
}
}

0 comments on commit b8a3c84

Please sign in to comment.