diff --git a/Sources/MapboxDirections/VisualInstructionComponent.swift b/Sources/MapboxDirections/VisualInstructionComponent.swift index ff1b0e1fc..42af6c023 100644 --- a/Sources/MapboxDirections/VisualInstructionComponent.swift +++ b/Sources/MapboxDirections/VisualInstructionComponent.swift @@ -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) diff --git a/Tests/MapboxDirectionsTests/VisualInstructionComponentTests.swift b/Tests/MapboxDirectionsTests/VisualInstructionComponentTests.swift index 8d762a8e0..765e0aa77 100644 --- a/Tests/MapboxDirectionsTests/VisualInstructionComponentTests.swift +++ b/Tests/MapboxDirectionsTests/VisualInstructionComponentTests.swift @@ -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.") + } + } + } }