Skip to content

Commit 67ad6a0

Browse files
committed
fix(specs): recommend is optional (generated)
algolia/api-clients-automation#3967 Co-authored-by: algolia-bot <[email protected]>
1 parent e6d0ae5 commit 67ad6a0

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

Sources/Recommend/Models/RecommendHit.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ public struct RecommendHit: Codable, JSONEncodable {
1717
public var rankingInfo: RecommendRankingInfo?
1818
public var distinctSeqID: Int?
1919
/// Recommendation score.
20-
public var score: Double
20+
public var score: Double?
2121

2222
public init(
2323
objectID: String,
2424
highlightResult: [String: RecommendHighlightResult]? = nil,
2525
snippetResult: [String: RecommendSnippetResult]? = nil,
2626
rankingInfo: RecommendRankingInfo? = nil,
2727
distinctSeqID: Int? = nil,
28-
score: Double
28+
score: Double? = nil
2929
) {
3030
self.objectID = objectID
3131
self.highlightResult = highlightResult
@@ -72,10 +72,8 @@ public struct RecommendHit: Codable, JSONEncodable {
7272

7373
self.distinctSeqID = dictionary["distinctSeqID"]?.value as? Int
7474

75-
guard let score = dictionary["score"]?.value as? Double else {
76-
throw GenericError(description: "Failed to cast")
77-
}
78-
self.score = score
75+
self.score = dictionary["score"]?.value as? Double
76+
7977
for (key, value) in dictionary {
8078
switch key {
8179
case "objectID", "highlightResult", "snippetResult", "rankingInfo", "distinctSeqID", "score":
@@ -95,7 +93,7 @@ public struct RecommendHit: Codable, JSONEncodable {
9593
try container.encodeIfPresent(self.snippetResult, forKey: .snippetResult)
9694
try container.encodeIfPresent(self.rankingInfo, forKey: .rankingInfo)
9795
try container.encodeIfPresent(self.distinctSeqID, forKey: .distinctSeqID)
98-
try container.encode(self.score, forKey: .score)
96+
try container.encodeIfPresent(self.score, forKey: .score)
9997
var additionalPropertiesContainer = encoder.container(keyedBy: String.self)
10098
try additionalPropertiesContainer.encodeMap(self.additionalProperties)
10199
}
@@ -116,7 +114,7 @@ public struct RecommendHit: Codable, JSONEncodable {
116114
)
117115
self.rankingInfo = try container.decodeIfPresent(RecommendRankingInfo.self, forKey: .rankingInfo)
118116
self.distinctSeqID = try container.decodeIfPresent(Int.self, forKey: .distinctSeqID)
119-
self.score = try container.decode(Double.self, forKey: .score)
117+
self.score = try container.decodeIfPresent(Double.self, forKey: .score)
120118
var nonAdditionalPropertyKeys = Set<String>()
121119
nonAdditionalPropertyKeys.insert("objectID")
122120
nonAdditionalPropertyKeys.insert("_highlightResult")
@@ -151,7 +149,7 @@ extension RecommendHit: Hashable {
151149
hasher.combine(self.snippetResult?.hashValue)
152150
hasher.combine(self.rankingInfo?.hashValue)
153151
hasher.combine(self.distinctSeqID?.hashValue)
154-
hasher.combine(self.score.hashValue)
152+
hasher.combine(self.score?.hashValue)
155153
hasher.combine(self.additionalProperties.hashValue)
156154
}
157155
}

Sources/Recommend/Models/TrendingFacetHit.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import Foundation
99
/// Trending facet hit.
1010
public struct TrendingFacetHit: Codable, JSONEncodable {
1111
/// Recommendation score.
12-
public var score: Double
12+
public var score: Double?
1313
/// Facet attribute. To be used in combination with `facetValue`. If specified, only recommendations matching the
1414
/// facet filter will be returned.
1515
public var facetName: String
1616
/// Facet value. To be used in combination with `facetName`. If specified, only recommendations matching the facet
1717
/// filter will be returned.
1818
public var facetValue: String
1919

20-
public init(score: Double, facetName: String, facetValue: String) {
20+
public init(score: Double? = nil, facetName: String, facetValue: String) {
2121
self.score = score
2222
self.facetName = facetName
2323
self.facetValue = facetValue
@@ -33,7 +33,7 @@ public struct TrendingFacetHit: Codable, JSONEncodable {
3333

3434
public func encode(to encoder: Encoder) throws {
3535
var container = encoder.container(keyedBy: CodingKeys.self)
36-
try container.encode(self.score, forKey: .score)
36+
try container.encodeIfPresent(self.score, forKey: .score)
3737
try container.encode(self.facetName, forKey: .facetName)
3838
try container.encode(self.facetValue, forKey: .facetValue)
3939
}
@@ -49,7 +49,7 @@ extension TrendingFacetHit: Equatable {
4949

5050
extension TrendingFacetHit: Hashable {
5151
public func hash(into hasher: inout Hasher) {
52-
hasher.combine(self.score.hashValue)
52+
hasher.combine(self.score?.hashValue)
5353
hasher.combine(self.facetName.hashValue)
5454
hasher.combine(self.facetValue.hashValue)
5555
}

0 commit comments

Comments
 (0)