Skip to content

Commit 498cd4e

Browse files
committed
Support base64-encoded data
Motivation OpenAPI supports base64-encoded data but to this point Swift OpenAPI Generator has not (apple#11). Modifications A data type specified as `type: string, format: byte` will now result in a generated type which is `Codable` and backed by a `OpenAPIRuntime.Base64EncodedData` type which knows how to encode and decode base64 data. Result Users will be able to specify request/response payloads as base64-encoded data which will be encoded and decoded transparently Test Plan Unit tested locally.
1 parent 0de1799 commit 498cd4e

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/TypeMatcher.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,10 @@ struct TypeMatcher {
292292
return nil
293293
}
294294
switch core.format {
295-
case .byte:
296-
typeName = .swift("String")
297295
case .binary:
298296
typeName = .foundation("Data")
297+
case .byte:
298+
typeName = .runtime("Base64EncodedData")
299299
case .dateTime:
300300
typeName = .foundation("Date")
301301
default:

Tests/OpenAPIGeneratorCoreTests/Translator/TypeAssignment/Test_TypeMatcher.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class Test_TypeMatcher: Test_Core {
2424

2525
static let builtinTypes: [(JSONSchema, String)] = [
2626
(.string, "Swift.String"),
27-
(.string(.init(format: .byte), .init()), "Swift.String"),
27+
(.string(.init(format: .byte), .init()), "OpenAPIRuntime.Base64EncodedData"),
2828
(.string(.init(format: .binary), .init()), "Foundation.Data"),
2929
(.string(.init(format: .date), .init()), "Swift.String"),
3030
(.string(.init(format: .dateTime), .init()), "Foundation.Date"),

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,47 @@ final class SnippetBasedReferenceTests: XCTestCase {
948948
)
949949
}
950950

951+
func testComponentsSchemasBase64() throws {
952+
try self.assertSchemasTranslation(
953+
"""
954+
schemas:
955+
MyData:
956+
type: string
957+
format: byte
958+
""",
959+
"""
960+
public enum Schemas {
961+
public typealias MyData = OpenAPIRuntime.Base64EncodedData
962+
}
963+
"""
964+
)
965+
}
966+
967+
func testComponentsSchemasBase64Object() throws {
968+
try self.assertSchemasTranslation(
969+
"""
970+
schemas:
971+
MyObj:
972+
type: object
973+
properties:
974+
stuff:
975+
type: string
976+
format: byte
977+
""",
978+
"""
979+
public enum Schemas {
980+
public struct MyObj: Codable, Hashable, Sendable {
981+
public var stuff: OpenAPIRuntime.Base64EncodedData?
982+
public init(stuff: OpenAPIRuntime.Base64EncodedData? = nil) {
983+
self.stuff = stuff
984+
}
985+
public enum CodingKeys: String, CodingKey { case stuff }
986+
}
987+
}
988+
"""
989+
)
990+
}
991+
951992
func testComponentsResponsesResponseNoBody() throws {
952993
try self.assertResponsesTranslation(
953994
"""
@@ -1433,6 +1474,40 @@ final class SnippetBasedReferenceTests: XCTestCase {
14331474
)
14341475
}
14351476

1477+
1478+
func testResponseWithExampleWithOnlyValueByte() throws {
1479+
try self.assertResponsesTranslation(
1480+
"""
1481+
responses:
1482+
MyResponse:
1483+
description: Some response
1484+
content:
1485+
application/json:
1486+
schema:
1487+
type: string
1488+
format: byte
1489+
examples:
1490+
application/json:
1491+
summary: "a hello response"
1492+
""",
1493+
"""
1494+
public enum Responses {
1495+
public struct MyResponse: Sendable, Hashable {
1496+
@frozen public enum Body: Sendable, Hashable {
1497+
case json(OpenAPIRuntime.Base64EncodedData)
1498+
}
1499+
public var body: Components.Responses.MyResponse.Body
1500+
public init(
1501+
body: Components.Responses.MyResponse.Body
1502+
) {
1503+
self.body = body
1504+
}
1505+
}
1506+
}
1507+
"""
1508+
)
1509+
}
1510+
14361511
}
14371512

14381513
extension SnippetBasedReferenceTests {

0 commit comments

Comments
 (0)