Skip to content

Commit fd9f0e9

Browse files
authored
Don't resolve ConvertService fallback links unless bundle ID matches (#850)
rdar://122964958
1 parent 2f7b3aa commit fd9f0e9

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

Sources/SwiftDocC/DocumentationService/Convert/Fallback Link Resolution/ConvertServiceFallbackResolver.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ import Foundation
2323
/// "external" content, even if it represents pages that would be "local" if the full project was built together.
2424
protocol ConvertServiceFallbackResolver {
2525

26+
/// The bundle identifier for the fallback resolver.
27+
///
28+
/// The fallback resolver will only resolve links with this bundle identifier.
29+
var bundleIdentifier: String { get }
30+
2631
// MARK: References
2732

2833
/// Attempts to resolve an unresolved reference for a page that couldn't be resolved locally.

Sources/SwiftDocC/Infrastructure/Link Resolution/LinkResolver.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ private final class FallbackResolverBasedLinkResolver {
170170
// Check if a fallback reference resolver should resolve this
171171
let referenceBundleIdentifier = unresolvedReference.bundleIdentifier ?? parent.bundleIdentifier
172172
guard let fallbackResolver = context.convertServiceFallbackResolver,
173-
let knownBundleIdentifier = context.registeredBundles.first(where: { $0.identifier == referenceBundleIdentifier || urlReadablePath($0.displayName) == referenceBundleIdentifier })?.identifier
173+
let knownBundleIdentifier = context.registeredBundles.first(where: { $0.identifier == referenceBundleIdentifier || urlReadablePath($0.displayName) == referenceBundleIdentifier })?.identifier,
174+
fallbackResolver.bundleIdentifier == knownBundleIdentifier
174175
else {
175176
return nil
176177
}

Tests/SwiftDocCTests/DocumentationService/ConvertService/ConvertServiceTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,45 @@ class ConvertServiceTests: XCTestCase {
23242324
}
23252325
}
23262326

2327+
func testDoesNotResolveLinksUnlessBundleIDMatches() throws {
2328+
let tempURL = try createTempFolder(content: [
2329+
Folder(name: "unit-test.docc", content: [
2330+
TextFile(name: "SomeExtension.md", utf8Content: """
2331+
# ``/ModuleName/SymbolName``
2332+
2333+
Some documentation extension
2334+
"""),
2335+
2336+
// This catalog doesn't have any symbol graph files
2337+
2338+
InfoPlist(identifier: "com.example.something")
2339+
])
2340+
])
2341+
let bundleURL = tempURL.appendingPathComponent("unit-test.docc")
2342+
2343+
let requestWithDifferentBundleID = ConvertRequest(
2344+
bundleInfo: DocumentationBundle.Info(displayName: "DisplayName", identifier: "com.example.something-else"),
2345+
externalIDsToConvert: [],
2346+
bundleLocation: bundleURL,
2347+
symbolGraphs: [],
2348+
markupFiles: [],
2349+
miscResourceURLs: []
2350+
)
2351+
XCTAssertEqual(try linkResolutionRequestsForConvertRequest(requestWithDifferentBundleID), [], "Shouldn't make any link resolution requests because the bundle IDs are different.")
2352+
2353+
let requestWithSameBundleID = ConvertRequest(
2354+
bundleInfo: DocumentationBundle.Info(displayName: "DisplayName", identifier: "com.example.something"),
2355+
externalIDsToConvert: [],
2356+
bundleLocation: bundleURL,
2357+
symbolGraphs: [],
2358+
markupFiles: [],
2359+
miscResourceURLs: []
2360+
)
2361+
let linkResolutionRequests = try linkResolutionRequestsForConvertRequest(requestWithSameBundleID)
2362+
XCTAssertFalse(linkResolutionRequests.isEmpty, "Should have made some link resolution requests to try to match the extension file")
2363+
XCTAssert(linkResolutionRequests.allSatisfy { $0.hasSuffix("/SymbolName") }, "Should have made some link resolution requests to try to match the extension file")
2364+
}
2365+
23272366
func testReturnsErrorWhenConversionHasProblems() throws {
23282367
let request = ConvertRequest(
23292368
bundleInfo: testBundleInfo,

Tests/SwiftDocCTests/Infrastructure/ExternalReferenceResolverTests.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ class ExternalReferenceResolverTests: XCTestCase {
192192

193193
do {
194194
class TestFallbackResolver: ConvertServiceFallbackResolver {
195+
init(bundleIdentifier: String) {
196+
resolver.bundleIdentifier = bundleIdentifier
197+
}
198+
var bundleIdentifier: String {
199+
resolver.bundleIdentifier
200+
}
195201
private var resolver = TestExternalReferenceResolver()
196202
func resolve(_ reference: SwiftDocC.TopicReference) -> TopicReferenceResolutionResult {
197203
TestExternalReferenceResolver().resolve(reference)
@@ -205,7 +211,7 @@ class ExternalReferenceResolverTests: XCTestCase {
205211
}
206212

207213
context.externalDocumentationSources = [:]
208-
context.convertServiceFallbackResolver = TestFallbackResolver()
214+
context.convertServiceFallbackResolver = TestFallbackResolver(bundleIdentifier: "org.swift.docc.example")
209215

210216
guard case let .success(resolved) = context.resolve(.unresolved(unresolved), in: parent) else {
211217
XCTFail("The reference was unexpectedly unresolved.")

0 commit comments

Comments
 (0)