Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
49 changes: 49 additions & 0 deletions Sources/OpenAPIRuntime/Base/Base64EncodedData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftOpenAPIGenerator open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Foundation

public struct Base64EncodedData: Sendable, Codable, Hashable {
var data: ArraySlice<UInt8>

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

public init(from decoder: any Decoder) throws {
let container = try decoder.singleValueContainer()
let base64EncodedString = try container.decode(String.self)

// permissive decoding
let options = Data.Base64DecodingOptions.ignoreUnknownCharacters

guard let data = Data(base64Encoded: base64EncodedString, options: options) else {
throw RuntimeError.invalidBase64String(base64EncodedString)
}
self.init(data: ArraySlice(data))
}

public func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()

// https://datatracker.ietf.org/doc/html/rfc4648#section-3.1
// "Implementations MUST NOT add line feeds to base-encoded data unless
// the specification referring to this document explicitly directs base
// encoders to add line feeds after a specific number of characters."
let options = Data.Base64EncodingOptions()

let base64String = Data(data).base64EncodedString(options: options)
try container.encode(base64String)
}
}
3 changes: 3 additions & 0 deletions Sources/OpenAPIRuntime/Errors/RuntimeError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal enum RuntimeError: Error, CustomStringConvertible, LocalizedError, Pret
case invalidServerURL(String)
case invalidExpectedContentType(String)
case invalidHeaderFieldName(String)
case invalidBase64String(String)

// Data conversion
case failedToDecodeStringConvertibleValue(type: String)
Expand Down Expand Up @@ -73,6 +74,8 @@ internal enum RuntimeError: Error, CustomStringConvertible, LocalizedError, Pret
return "Invalid expected content type: '\(string)'"
case .invalidHeaderFieldName(let name):
return "Invalid header field name: '\(name)'"
case .invalidBase64String(let string):
return "Invalid base64-encoded string: '\(string)'"
case .failedToDecodeStringConvertibleValue(let string):
return "Failed to decode a value of type '\(string)'."
case .unsupportedParameterStyle(name: let name, location: let location, style: let style, explode: let explode):
Expand Down
29 changes: 28 additions & 1 deletion Tests/OpenAPIRuntimeTests/Base/Test_OpenAPIValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
import XCTest
@_spi(Generated) import OpenAPIRuntime
@_spi(Generated)@testable import OpenAPIRuntime

final class Test_OpenAPIValue: Test_Runtime {

Expand Down Expand Up @@ -266,4 +266,31 @@ final class Test_OpenAPIValue: Test_Runtime {
let nestedValue = try XCTUnwrap(nestedDict["nested"] as? Int)
XCTAssertEqual(nestedValue, 2)
}

func testEncoding_base64_success() throws {
let encodedData = Base64EncodedData(data: ArraySlice(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))

// `testStructBase64EncodedString` quoted and base64-encoded again
let JSONEncoded = Data(base64Encoded: "ImV5SnVZVzFsSWpvaVJteDFabVo2SW4wPSI=")!

XCTAssertEqual(
try JSONDecoder().decode(Base64EncodedData.self, from: JSONEncoded),
encodedData
)
}

func testEncodingDecodingRoundtrip_base64_success() throws {
let encodedData = Base64EncodedData(data: ArraySlice(testStructData))
XCTAssertEqual(
try JSONDecoder().decode(Base64EncodedData.self, from: JSONEncoder().encode(encodedData)),
encodedData
)
}
}
4 changes: 4 additions & 0 deletions Tests/OpenAPIRuntimeTests/Test_Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ class Test_Runtime: XCTestCase {
"age=3&name=Rover%21&type=Golden+Retriever"
}

var testStructBase64EncodedString: String {
#""eyJuYW1lIjoiRmx1ZmZ6In0=""# // {"name":"Fluffz"}
}

var testEnum: TestHabitat {
.water
}
Expand Down