Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that bundle identifiers are valid URL host components #1069

Merged
merged 21 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
28b9522
Add bundle identifier type that ensures a valid URL host component
d-ronnqvist Oct 22, 2024
0e62ffc
Use new Identifier type in `DocumentationBundle/Info`
d-ronnqvist Oct 22, 2024
b46b5d3
Update code to not use deprecated `Info/identifier` property
d-ronnqvist Oct 22, 2024
bfa673b
Use new Identifier type in `DocumentationBundle`
d-ronnqvist Oct 22, 2024
98c6a58
Update code to not use deprecated `identifier` property
d-ronnqvist Oct 22, 2024
399ed9a
Use new Identifier type in `[Unresolved|Resolved]TopicReference`
d-ronnqvist Oct 22, 2024
a7310b7
Update code to not use deprecated topic reference `bundleIdentifier` …
d-ronnqvist Oct 22, 2024
221891a
Deprecate `BundleIdentifier` in favor of `DocumentationBundle/Identif…
d-ronnqvist Oct 22, 2024
890e323
Use new Identifier type in `BuildMetadata`
d-ronnqvist Oct 22, 2024
3c56a2d
Use new Identifier type in `AssetReference`
d-ronnqvist Oct 22, 2024
13b11fb
Use new Identifier type in `ResourceReference`
d-ronnqvist Oct 22, 2024
414ef9c
Use new Identifier type in `ConvertServiceFallbackResolver`
d-ronnqvist Oct 22, 2024
61cc5e2
Use new Identifier type in `SerializableLinkResolutionInformation`
d-ronnqvist Oct 22, 2024
5369b1c
Use new Identifier type in `ConvertAction/Indexer`
d-ronnqvist Oct 22, 2024
4805a05
Prefer `bundleID` over `id` for types that scoped inside bundle
d-ronnqvist Oct 22, 2024
0f2f8ef
Merge branch 'main' into validate-bundle-identifier
d-ronnqvist Oct 31, 2024
c2442bc
Merge branch 'main' into validate-bundle-identifier
d-ronnqvist Nov 11, 2024
71b27b6
Merge branch 'main' into validate-bundle-identifier
d-ronnqvist Nov 19, 2024
3d063ef
Merge branch 'main' into validate-bundle-identifier
d-ronnqvist Nov 21, 2024
f97e6d3
Use `bundleID` instead of `id` for property names outside the bundle
d-ronnqvist Nov 21, 2024
9478d47
Use "bundle id" in local variable in fallback resolver
d-ronnqvist Nov 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2024 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import Foundation

extension DocumentationBundle {
/// A stable and locally unique identifier for a collection of documentation inputs.
public struct Identifier: RawRepresentable {
public let rawValue: String

public init(rawValue: String) {
// To ensure that the identifier can be used as a valid "host" component of a resolved topic reference's url,
// replace any consecutive sequence of unsupported characters with a "-".
self.rawValue = rawValue
.components(separatedBy: Self.charactersToReplace)
.filter { !$0.isEmpty }
.joined(separator: "-")
}

private static let charactersToReplace = CharacterSet.urlHostAllowed.inverted
d-ronnqvist marked this conversation as resolved.
Show resolved Hide resolved
}
}

extension DocumentationBundle.Identifier: Hashable {}
extension DocumentationBundle.Identifier: Sendable {}

// Support creating an identifier from a string literal.
extension DocumentationBundle.Identifier: ExpressibleByStringLiteral {
public init(stringLiteral value: StringLiteralType) {
self.init(rawValue: value)
}
}

// Sort identifiers based on their raw string value.
extension DocumentationBundle.Identifier: Comparable {
public static func < (lhs: Self, rhs: Self) -> Bool {
lhs.rawValue < rhs.rawValue
}
}

// Print as a single string value
extension DocumentationBundle.Identifier: CustomStringConvertible {
public var description: String {
rawValue
}
}

// Encode and decode the identifier as a single string value.
extension DocumentationBundle.Identifier: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let rawValue = try container.decode(String.self)
self.init(rawValue: rawValue)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(rawValue)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2024 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import XCTest
@testable import SwiftDocC

class DocumentationBundleIdentifierTests: XCTestCase {
private typealias Identifier = DocumentationBundle.Identifier

func testInitialization() {
let id = Identifier(rawValue: "com.example.test")
XCTAssertEqual(id.rawValue, "com.example.test")

let idWithSpace = Identifier(rawValue: "Package Name")
XCTAssertEqual(idWithSpace.rawValue, "Package-Name", "The initializer transforms the value to a valid identifier")
}

func testExpressibleByStringLiteral() {
let id: Identifier = "com.example.test"
XCTAssertEqual(id.rawValue, "com.example.test")

let idWithSpace: Identifier = "Package Name"
XCTAssertEqual(idWithSpace.rawValue, "Package-Name", "The initializer transforms the value to a valid identifier")
}

func testEquatable() {
XCTAssertEqual(Identifier(rawValue: "A"), "A")
XCTAssertNotEqual(Identifier(rawValue: "A"), "B")
}

func testComparable() {
XCTAssertLessThan(Identifier(rawValue: "B"), "C")
XCTAssertGreaterThan(Identifier(rawValue: "B"), "A")
}

func testCustomStringConvertible() {
XCTAssertEqual(Identifier(rawValue: "com.example.test").description, "com.example.test")
XCTAssertEqual(Identifier(rawValue: "Package Name").description, "Package-Name")

}

func testEncodesAsPlainString() throws {
let id = Identifier(rawValue: "com.example.test")
let encoded = try String(data: JSONEncoder().encode(id), encoding: .utf8)
XCTAssertEqual(encoded, "\"com.example.test\"")
}

func testDecodingFromPlainString() throws {
let decoded = try JSONDecoder().decode(Identifier.self, from: Data("\"com.example.test\"".utf8))
XCTAssertEqual(decoded, "com.example.test")
}
}