Skip to content

Commit 9da9ad6

Browse files
authored
Base64EncodedData initializer taking an array slice shouldn't have a label (#71)
### Motivation - Fixes [#369](apple/swift-openapi-generator#368) ### Modifications - Remove init label argument from Base64EncodedData ### Result - The Base64EncodedData init will be called without the label. ### Test Plan - Adjust some tests from Test_OpenAPIValue
1 parent f6085a3 commit 9da9ad6

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

Sources/OpenAPIRuntime/Base/Base64EncodedData.swift

+17-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,23 @@ public struct Base64EncodedData: Sendable, Hashable {
5656

5757
/// Initializes an instance of ``Base64EncodedData`` wrapping the provided slice of bytes.
5858
/// - Parameter data: The underlying bytes to wrap.
59+
@available(*, deprecated, renamed: "init(_:)")
60+
5961
public init(data: ArraySlice<UInt8>) { self.data = data }
62+
63+
/// Initializes an instance of ``Base64EncodedData`` wrapping the provided slice of bytes.
64+
/// - Parameter data: The underlying bytes to wrap.
65+
public init(_ data: ArraySlice<UInt8>) { self.data = data }
66+
67+
/// Initializes an instance of ``Base64EncodedData`` wrapping the provided sequence of bytes.
68+
/// - Parameter data: The underlying bytes to wrap.
69+
public init(_ data: some Sequence<UInt8>) { self.init(ArraySlice(data)) }
70+
}
71+
72+
extension Base64EncodedData: ExpressibleByArrayLiteral {
73+
/// Initializes an instance of ``Base64EncodedData`` with a sequence of bytes provided as an array literal.
74+
/// - Parameter elements: The sequence of `UInt8` elements representing the underlying bytes.
75+
public init(arrayLiteral elements: UInt8...) { self.init(elements) }
6076
}
6177

6278
extension Base64EncodedData: Codable {
@@ -74,7 +90,7 @@ extension Base64EncodedData: Codable {
7490
guard let data = Data(base64Encoded: base64EncodedString, options: options) else {
7591
throw RuntimeError.invalidBase64String(base64EncodedString)
7692
}
77-
self.init(data: ArraySlice(data))
93+
self.init(data)
7894
}
7995

8096
/// Encodes the binary data as a base64-encoded string.

Tests/OpenAPIRuntimeTests/Base/Test_OpenAPIValue.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,14 @@ final class Test_OpenAPIValue: Test_Runtime {
241241
}
242242

243243
func testEncoding_base64_success() throws {
244-
let encodedData = Base64EncodedData(data: ArraySlice(testStructData))
244+
let encodedData = Base64EncodedData(testStructData)
245245

246246
let JSONEncoded = try JSONEncoder().encode(encodedData)
247247
XCTAssertEqual(String(data: JSONEncoded, encoding: .utf8)!, testStructBase64EncodedString)
248248
}
249249

250250
func testDecoding_base64_success() throws {
251-
let encodedData = Base64EncodedData(data: ArraySlice(testStructData))
251+
let encodedData = Base64EncodedData(testStructData)
252252

253253
// `testStructBase64EncodedString` quoted and base64-encoded again
254254
let JSONEncoded = Data(base64Encoded: "ImV5SnVZVzFsSWpvaVJteDFabVo2SW4wPSI=")!
@@ -257,7 +257,7 @@ final class Test_OpenAPIValue: Test_Runtime {
257257
}
258258

259259
func testEncodingDecodingRoundtrip_base64_success() throws {
260-
let encodedData = Base64EncodedData(data: ArraySlice(testStructData))
260+
let encodedData = Base64EncodedData(testStructData)
261261
XCTAssertEqual(
262262
try JSONDecoder().decode(Base64EncodedData.self, from: JSONEncoder().encode(encodedData)),
263263
encodedData

0 commit comments

Comments
 (0)