Skip to content

Base64EncodedData initializer taking an array slice shouldn't have a label #71

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 17 additions & 1 deletion Sources/OpenAPIRuntime/Base/Base64EncodedData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,23 @@ public struct Base64EncodedData: Sendable, Hashable {

/// Initializes an instance of ``Base64EncodedData`` wrapping the provided slice of bytes.
/// - Parameter data: The underlying bytes to wrap.
@available(*, deprecated, renamed: "init(_:)")

public init(data: ArraySlice<UInt8>) { self.data = data }

/// Initializes an instance of ``Base64EncodedData`` wrapping the provided slice of bytes.
/// - Parameter data: The underlying bytes to wrap.
public init(_ data: ArraySlice<UInt8>) { self.data = data }

/// Initializes an instance of ``Base64EncodedData`` wrapping the provided sequence of bytes.
/// - Parameter data: The underlying bytes to wrap.
public init(_ data: some Sequence<UInt8>) { self.init(ArraySlice(data)) }
}

extension Base64EncodedData: ExpressibleByArrayLiteral {
/// Initializes an instance of ``Base64EncodedData`` with a sequence of bytes provided as an array literal.
/// - Parameter elements: The sequence of `UInt8` elements representing the underlying bytes.
public init(arrayLiteral elements: UInt8...) { self.init(elements) }
}

extension Base64EncodedData: Codable {
Expand All @@ -74,7 +90,7 @@ extension Base64EncodedData: Codable {
guard let data = Data(base64Encoded: base64EncodedString, options: options) else {
throw RuntimeError.invalidBase64String(base64EncodedString)
}
self.init(data: ArraySlice(data))
self.init(data)
}

/// Encodes the binary data as a base64-encoded string.
Expand Down
6 changes: 3 additions & 3 deletions Tests/OpenAPIRuntimeTests/Base/Test_OpenAPIValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,14 @@ final class Test_OpenAPIValue: Test_Runtime {
}

func testEncoding_base64_success() throws {
let encodedData = Base64EncodedData(data: ArraySlice(testStructData))
let encodedData = Base64EncodedData(testStructData)

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

func testDecoding_base64_success() throws {
let encodedData = Base64EncodedData(data: ArraySlice(testStructData))
let encodedData = Base64EncodedData(testStructData)

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

func testEncodingDecodingRoundtrip_base64_success() throws {
let encodedData = Base64EncodedData(data: ArraySlice(testStructData))
let encodedData = Base64EncodedData(testStructData)
XCTAssertEqual(
try JSONDecoder().decode(Base64EncodedData.self, from: JSONEncoder().encode(encodedData)),
encodedData
Expand Down