Skip to content

Commit 05e786c

Browse files
committed
Encode absolute presentation URL if present
Support for absolute URLs in link summaries was added in 7db36ad. It added support for absolute URLs when decoding a link summary, but not when encoding it: https://github.com/swiftlang/swift-docc/blob/cfcd96f0e992af2287661f6a26e8398096f6bc1e/Sources/SwiftDocC/LinkTargets/LinkDestinationSummary.swift#L772-L773 https://github.com/swiftlang/swift-docc/blob/cfcd96f0e992af2287661f6a26e8398096f6bc1e/Sources/SwiftDocC/LinkTargets/LinkDestinationSummary.swift#L728 Both are needed when using an external link resolver, as the external link resolver needs to encode the entity to send to DocC. Fixes rdar://164628218.
1 parent cfcd96f commit 05e786c

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

Sources/SwiftDocC/LinkTargets/LinkDestinationSummary.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ extension LinkDestinationSummary {
725725
} else {
726726
try container.encode(kind, forKey: .kind)
727727
}
728-
try container.encode(relativePresentationURL, forKey: .relativePresentationURL)
728+
try container.encode(absolutePresentationURL ?? relativePresentationURL, forKey: .relativePresentationURL)
729729
try container.encode(referenceURL, forKey: .referenceURL)
730730
try container.encode(title, forKey: .title)
731731
try container.encodeIfPresent(abstract, forKey: .abstract)

Tests/SwiftDocCTests/Infrastructure/ExternalReferenceResolverTests.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,15 @@ class ExternalReferenceResolverTests: XCTestCase {
14201420
""".utf8))
14211421
XCTAssertEqual(externalEntity.relativePresentationURL.absoluteString, "/path/to/something")
14221422
XCTAssertEqual(externalEntity.absolutePresentationURL?.absoluteString, "https://com.example/path/to/something")
1423-
1423+
1424+
// Test that encoding the link summary preserves the absolute URL
1425+
let encodedData = try JSONEncoder().encode(externalEntity)
1426+
let encodedJSON = try JSONSerialization.jsonObject(with: encodedData) as? [String: Any]
1427+
XCTAssertEqual(encodedJSON?["path"] as? String, "https://com.example/path/to/something", "The encoded path should be the absolute URL")
1428+
let decodedEntity = try JSONDecoder().decode(LinkDestinationSummary.self, from: encodedData)
1429+
XCTAssertEqual(decodedEntity.relativePresentationURL.absoluteString, "/path/to/something")
1430+
XCTAssertEqual(decodedEntity.absolutePresentationURL?.absoluteString, "https://com.example/path/to/something")
1431+
14241432
let resolver = Resolver(entityToReturn: externalEntity)
14251433

14261434
var configuration = DocumentationContext.Configuration()

0 commit comments

Comments
 (0)