Skip to content

Commit ada956b

Browse files
committed
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 a919c65 commit ada956b

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
@@ -331,10 +331,10 @@ struct TypeMatcher {
331331
return nil
332332
}
333333
switch core.format {
334-
case .byte:
335-
typeName = .swift("String")
336334
case .binary:
337335
typeName = .body
336+
case .byte:
337+
typeName = .runtime("Base64EncodedData")
338338
case .dateTime:
339339
typeName = .foundation("Date")
340340
default:

Tests/OpenAPIGeneratorCoreTests/Translator/TypeAssignment/Test_TypeMatcher.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ 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"),
2827
(.string(.init(format: .binary), .init()), "OpenAPIRuntime.HTTPBody"),
28+
(.string(.init(format: .byte), .init()), "OpenAPIRuntime.Base64EncodedData"),
2929
(.string(.init(format: .date), .init()), "Swift.String"),
3030
(.string(.init(format: .dateTime), .init()), "Foundation.Date"),
3131

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,47 @@ final class SnippetBasedReferenceTests: XCTestCase {
961961
)
962962
}
963963

964+
func testComponentsSchemasBase64() throws {
965+
try self.assertSchemasTranslation(
966+
"""
967+
schemas:
968+
MyData:
969+
type: string
970+
format: byte
971+
""",
972+
"""
973+
public enum Schemas {
974+
public typealias MyData = OpenAPIRuntime.Base64EncodedData
975+
}
976+
"""
977+
)
978+
}
979+
980+
func testComponentsSchemasBase64Object() throws {
981+
try self.assertSchemasTranslation(
982+
"""
983+
schemas:
984+
MyObj:
985+
type: object
986+
properties:
987+
stuff:
988+
type: string
989+
format: byte
990+
""",
991+
"""
992+
public enum Schemas {
993+
public struct MyObj: Codable, Hashable, Sendable {
994+
public var stuff: OpenAPIRuntime.Base64EncodedData?
995+
public init(stuff: OpenAPIRuntime.Base64EncodedData? = nil) {
996+
self.stuff = stuff
997+
}
998+
public enum CodingKeys: String, CodingKey { case stuff }
999+
}
1000+
}
1001+
"""
1002+
)
1003+
}
1004+
9641005
func testComponentsResponsesResponseNoBody() throws {
9651006
try self.assertResponsesTranslation(
9661007
"""
@@ -1764,6 +1805,40 @@ final class SnippetBasedReferenceTests: XCTestCase {
17641805
"""
17651806
)
17661807
}
1808+
1809+
func testResponseWithExampleWithOnlyValueByte() throws {
1810+
try self.assertResponsesTranslation(
1811+
"""
1812+
responses:
1813+
MyResponse:
1814+
description: Some response
1815+
content:
1816+
application/json:
1817+
schema:
1818+
type: string
1819+
format: byte
1820+
examples:
1821+
application/json:
1822+
summary: "a hello response"
1823+
""",
1824+
"""
1825+
public enum Responses {
1826+
public struct MyResponse: Sendable, Hashable {
1827+
@frozen public enum Body: Sendable, Hashable {
1828+
case json(OpenAPIRuntime.Base64EncodedData)
1829+
}
1830+
public var body: Components.Responses.MyResponse.Body
1831+
public init(
1832+
body: Components.Responses.MyResponse.Body
1833+
) {
1834+
self.body = body
1835+
}
1836+
}
1837+
}
1838+
"""
1839+
)
1840+
}
1841+
17671842
}
17681843

17691844
extension SnippetBasedReferenceTests {

0 commit comments

Comments
 (0)