Skip to content

Commit

Permalink
Merge pull request #394 from mapbox/1ec5-visualcomponent-type-none
Browse files Browse the repository at this point in the history
Fix decoding of unrecognized visual instruction component types
  • Loading branch information
1ec5 authored Dec 21, 2019
2 parents 64d3814 + b8a3c84 commit ff9997d
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 ff9997d

Please sign in to comment.