Skip to content

Commit

Permalink
Fix Neighbourhood decoding with null url_force (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamayoung authored Apr 21, 2023
1 parent a22628c commit 085b21d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Sources/PoliceAPI/Models/Neighbourhood.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public struct Neighbourhood: Identifiable, Decodable, Equatable {
}
/// URL for the neighbourhood on the Force's website.
public var policeForceWebsite: URL? {
URL(string: policeForceWebsiteString)
guard let policeForceWebsiteString else {
return nil
}

return URL(string: policeForceWebsiteString)
}
/// An introduction message for the neighbourhood.
public let welcomeMessage: String?
Expand All @@ -38,7 +42,7 @@ public struct Neighbourhood: Identifiable, Decodable, Equatable {
/// Links.
public let links: [Link]

private let policeForceWebsiteString: String
private let policeForceWebsiteString: String?
private let nameString: String
private let descriptionString: String?
private let populationString: String
Expand All @@ -62,7 +66,7 @@ public struct Neighbourhood: Identifiable, Decodable, Equatable {
self.id = id
self.nameString = name
self.descriptionString = description
self.policeForceWebsiteString = policeForceWebsite?.absoluteString ?? ""
self.policeForceWebsiteString = policeForceWebsite?.absoluteString
self.welcomeMessage = welcomeMessage
self.populationString = String(population)
self.contactDetails = contactDetails
Expand Down
7 changes: 7 additions & 0 deletions Tests/PoliceAPITests/Models/NeighbourhoodTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,11 @@ final class NeighbourhoodTests: XCTestCase {
XCTAssertNil(result)
}

func testPoliceForceWebsiteWhenNull() throws {
let result = try JSONDecoder.policeDataAPI
.decode(Neighbourhood.self, fromResource: "neighbourhood-null-force-url").policeForceWebsite

XCTAssertNil(result)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"url_force": null,
"contact_details": {},
"name": "Banks & Hesketh",
"links": [],
"centre": {
"latitude": "52.6389",
"longitude": "-1.13619"
},
"locations": [],
"description": "Some & description",
"id": "AB12",
"population": "1000"
}

0 comments on commit 085b21d

Please sign in to comment.