Skip to content

Commit 50ee88b

Browse files
committed
Make Citation.license optional
1 parent 0cd4ef6 commit 50ee88b

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

Sources/GoogleAI/GenerateContentResponse.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ public struct Citation {
184184
/// A link to the cited source.
185185
public let uri: String
186186

187-
/// The license the cited source work is distributed under.
188-
public let license: String
187+
/// The license the cited source work is distributed under, if specified.
188+
public let license: String?
189189
}
190190

191191
/// A value enumerating possible reasons for a model to terminate a content generation request.
@@ -314,6 +314,11 @@ extension Citation: Decodable {
314314
startIndex = try container.decodeIfPresent(Int.self, forKey: .startIndex) ?? 0
315315
endIndex = try container.decode(Int.self, forKey: .endIndex)
316316
uri = try container.decode(String.self, forKey: .uri)
317-
license = try container.decodeIfPresent(String.self, forKey: .license) ?? ""
317+
if let license = try container.decodeIfPresent(String.self, forKey: .license),
318+
!license.isEmpty {
319+
self.license = license
320+
} else {
321+
license = nil
322+
}
318323
}
319324
}

Tests/GoogleAITests/GenerativeModelTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,17 @@ final class GenerativeModelTests: XCTestCase {
109109
XCTAssertEqual(citationSource1.uri, "https://www.example.com/some-citation-1")
110110
XCTAssertEqual(citationSource1.startIndex, 0)
111111
XCTAssertEqual(citationSource1.endIndex, 128)
112-
XCTAssertEqual(citationSource1.license, "")
112+
XCTAssertNil(citationSource1.license)
113113
let citationSource2 = try XCTUnwrap(citationMetadata.citationSources[1])
114114
XCTAssertEqual(citationSource2.uri, "https://www.example.com/some-citation-2")
115115
XCTAssertEqual(citationSource2.startIndex, 130)
116116
XCTAssertEqual(citationSource2.endIndex, 265)
117-
XCTAssertEqual(citationSource2.license, "")
117+
XCTAssertNil(citationSource2.license)
118118
let citationSource3 = try XCTUnwrap(citationMetadata.citationSources[2])
119119
XCTAssertEqual(citationSource3.uri, "https://www.example.com/some-citation-3")
120120
XCTAssertEqual(citationSource3.startIndex, 272)
121121
XCTAssertEqual(citationSource3.endIndex, 431)
122-
XCTAssertEqual(citationSource3.license, "")
122+
XCTAssertNil(citationSource3.license)
123123
let citationSource4 = try XCTUnwrap(citationMetadata.citationSources[3])
124124
XCTAssertEqual(citationSource4.uri, "https://www.example.com/some-citation-4")
125125
XCTAssertEqual(citationSource4.startIndex, 444)
@@ -740,15 +740,15 @@ final class GenerativeModelTests: XCTestCase {
740740
XCTAssertEqual(citations.count, 8)
741741
XCTAssertTrue(citations
742742
.contains(where: {
743-
$0.startIndex == 0 && $0.endIndex == 128 && !$0.uri.isEmpty && $0.license.isEmpty
743+
$0.startIndex == 0 && $0.endIndex == 128 && !$0.uri.isEmpty && $0.license == nil
744744
}))
745745
XCTAssertTrue(citations
746746
.contains(where: {
747-
$0.startIndex == 130 && $0.endIndex == 265 && !$0.uri.isEmpty && $0.license.isEmpty
747+
$0.startIndex == 130 && $0.endIndex == 265 && !$0.uri.isEmpty && $0.license == nil
748748
}))
749749
XCTAssertTrue(citations
750750
.contains(where: {
751-
$0.startIndex == 272 && $0.endIndex == 431 && !$0.uri.isEmpty && $0.license.isEmpty
751+
$0.startIndex == 272 && $0.endIndex == 431 && !$0.uri.isEmpty && $0.license == nil
752752
}))
753753
XCTAssertTrue(citations
754754
.contains(where: {

0 commit comments

Comments
 (0)