Skip to content

Commit

Permalink
BUG: Fix model decoding errors
Browse files Browse the repository at this point in the history
  • Loading branch information
adamayoung committed Nov 5, 2024
1 parent 31dee6a commit 9867003
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
45 changes: 41 additions & 4 deletions Sources/TMDb/Domain/Models/Company.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public struct Company: Identifiable, Codable, Equatable, Hashable, Sendable {
///
/// Company's homepage.
///
public let homepage: URL
public let homepageURL: URL?

///
/// Company's logo path.
Expand All @@ -74,7 +74,7 @@ public struct Company: Identifiable, Codable, Equatable, Hashable, Sendable {
/// - name: Company name.
/// - description: Description of company.
/// - headquarters: Location of the company's headquarters.
/// - homepage: Company's homepage.
/// - homepageURL: Company's homepage.
/// - logoPath: Company's logo path.
/// - originCountry: Origin country.
/// - parentCompany: Parent company.
Expand All @@ -84,7 +84,7 @@ public struct Company: Identifiable, Codable, Equatable, Hashable, Sendable {
name: String,
description: String,
headquarters: String,
homepage: URL,
homepageURL: URL? = nil,
logoPath: URL,
originCountry: String,
parentCompany: Parent? = nil
Expand All @@ -93,14 +93,51 @@ public struct Company: Identifiable, Codable, Equatable, Hashable, Sendable {
self.name = name
self.description = description
self.headquarters = headquarters
self.homepage = homepage
self.homepageURL = homepageURL
self.logoPath = logoPath
self.originCountry = originCountry
self.parentCompany = parentCompany
}

}

extension Company {

private enum CodingKeys: String, CodingKey {
case id
case name
case description
case headquarters
case homepageURL = "homepage"
case logoPath
case originCountry
case parentCompany
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let container2 = try decoder.container(keyedBy: CodingKeys.self)

self.id = try container.decode(Int.self, forKey: .id)
self.name = try container.decode(String.self, forKey: .name)
self.description = try container.decode(String.self, forKey: .description)
self.headquarters = try container.decode(String.self, forKey: .headquarters)
// Need to deal with empty strings - URL decoding will fail with an empty string
let homepageURLString = try container.decodeIfPresent(String.self, forKey: .homepageURL)
self.homepageURL = try {
guard let homepageURLString, !homepageURLString.isEmpty else {
return nil

Check warning on line 129 in Sources/TMDb/Domain/Models/Company.swift

View check run for this annotation

Codecov / codecov/patch

Sources/TMDb/Domain/Models/Company.swift#L129

Added line #L129 was not covered by tests
}

return try container2.decodeIfPresent(URL.self, forKey: .homepageURL)
}()
self.logoPath = try container.decode(URL.self, forKey: .logoPath)
self.originCountry = try container.decode(String.self, forKey: .originCountry)
self.parentCompany = try container.decodeIfPresent(Parent.self, forKey: .parentCompany)
}

}

public extension Company {

///
Expand Down
4 changes: 2 additions & 2 deletions Sources/TMDb/Domain/Models/PersonListItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public struct PersonListItem: Identifiable, Codable, Equatable, Hashable, Sendab
///
/// Person's movies and TV series they're known for.
///
public let knownFor: [Show]
public let knownFor: [Show]?

///
/// Is the Person only suitable for adults.
Expand Down Expand Up @@ -93,7 +93,7 @@ public struct PersonListItem: Identifiable, Codable, Equatable, Hashable, Sendab
gender: Gender,
profilePath: URL? = nil,
popularity: Double? = nil,
knownFor: [Show] = [],
knownFor: [Show]? = nil,
isAdultOnly: Bool = false
) {
self.id = id
Expand Down
4 changes: 2 additions & 2 deletions Tests/TMDbTests/Domain/Models/CompanyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct CompanyTests {
#expect(result.name == company.name)
#expect(result.description == company.description)
#expect(result.headquarters == company.headquarters)
#expect(result.homepage == company.homepage)
#expect(result.homepageURL == company.homepageURL)
#expect(result.logoPath == company.logoPath)
#expect(result.originCountry == company.originCountry)
let parentCompany = try #require(result.parentCompany)
Expand All @@ -46,7 +46,7 @@ struct CompanyTests {
name: "Pixar",
description: "",
headquarters: "Emeryville, California",
homepage: URL(string: "http://www.pixar.com")!,
homepageURL: URL(string: "http://www.pixar.com")!,
logoPath: URL(string: "/1TjvGVDMYsj6JBxOAkUHpPEwLf7.png")!,
originCountry: "US",
parentCompany: Company.Parent(
Expand Down
6 changes: 3 additions & 3 deletions Tests/TMDbTests/Mocks/Models/Company+Mocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension Company {
name: String = .randomString,
description: String = .randomString,
headquarters: String = .randomString,
homepage: URL = .randomWebSite,
homepageURL: URL = .randomWebSite,
logoPath: URL = .randomImagePath,
originCountry: String = "US",
parentCompany: Company.Parent? = nil
Expand All @@ -37,7 +37,7 @@ extension Company {
name: name,
description: description,
headquarters: headquarters,
homepage: homepage,
homepageURL: homepageURL,
logoPath: logoPath,
originCountry: originCountry,
parentCompany: parentCompany
Expand All @@ -50,7 +50,7 @@ extension Company {
name: "Lucasfilm Ltd.",
description: "Some description",
headquarters: "San Francisco, California",
homepage: URL(string: "https://www.lucasfilm.com")!,
homepageURL: URL(string: "https://www.lucasfilm.com")!,
logoPath: URL(string: "/o86DbpburjxrqAzEDhXZcyE8pDb.png")!,
originCountry: "US"
)
Expand Down

0 comments on commit 9867003

Please sign in to comment.