Skip to content

Commit 2081703

Browse files
authored
Bump OpenAPIKit to 3.0.0-rc.3 and recognize more base64/binary encoding locations (#357)
Bump OpenAPIKit to 3.0.0-rc.3 and recognize more base64/binary encoding locations ### Motivation In OpenAPI 3.1.0, the `format: ...` part of schemas is deprecated, and instead `contentEncoding: ...` should be used. Before this PR, we'd recognize the encodings defined in `format` but not `contentEncoding`, this PR fixes that and finds them in both locations. This required a bump of OpenAPIKit, which added support for `contentEncoding` in `3.0.0-rc.3`. ### Modifications Change the type matching logic to first look at `contentEncoding`, if no special value is specified there, look at `format`. ### Result `contentEncoding: base64` and `contentEncoding: binary` will now be correctly represented as their runtime types instead of a plain string. ### Test Plan Adapted the unit tests. Reviewed by: simonjbeaumont Builds: ✔︎ pull request validation (5.10) - Build finished. ✔︎ pull request validation (5.8) - Build finished. ✔︎ pull request validation (5.9) - Build finished. ✔︎ pull request validation (compatibility test) - Build finished. ✔︎ pull request validation (docc test) - Build finished. ✔︎ pull request validation (integration test) - Build finished. ✔︎ pull request validation (nightly) - Build finished. ✔︎ pull request validation (soundness) - Build finished. #357
1 parent 16668dc commit 2081703

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ let package = Package(
5555
.package(url: "https://github.com/apple/swift-algorithms", from: "1.0.0"),
5656

5757
// Read OpenAPI documents
58-
.package(url: "https://github.com/mattpolzin/OpenAPIKit.git", exact: "3.0.0-rc.2"),
58+
.package(url: "https://github.com/mattpolzin/OpenAPIKit.git", exact: "3.0.0-rc.3"),
5959
.package(url: "https://github.com/jpsim/Yams.git", "4.0.0"..<"6.0.0"),
6060

6161
// CLI Tool

Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/Builtins.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ extension TypeName {
4444
/// - Returns: A TypeName representing the type with the given name in the HTTPTypes module.
4545
static func httpTypes(_ name: String) -> TypeName { TypeName(swiftKeyPath: ["HTTPTypes", name]) }
4646

47+
/// Returns the type name for the Date type.
48+
static var date: Self { .foundation("Date") }
49+
4750
/// Returns the type name for the URL type.
4851
static var url: Self { .foundation("URL") }
4952

@@ -78,4 +81,7 @@ extension TypeName {
7881

7982
/// Returns the type name for the copy-on-write box type.
8083
static var box: TypeName { .runtime("CopyOnWriteBox") }
84+
85+
/// Returns the type name for the base64 wrapper.
86+
static var base64: TypeName { .runtime("Base64EncodedData") }
8187
}

Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/TypeMatcher.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,22 @@ struct TypeMatcher {
293293
case .int64: typeName = .swift("Int64")
294294
default: typeName = .swift("Int")
295295
}
296-
case .string(let core, _):
296+
case .string(let core, let stringContext):
297297
if core.allowedValues != nil {
298298
// custom enum isn't a builtin
299299
return nil
300300
}
301-
switch core.format {
301+
switch stringContext.contentEncoding {
302302
case .binary: typeName = .body
303-
case .byte: typeName = .runtime("Base64EncodedData")
304-
case .dateTime: typeName = .foundation("Date")
305-
default: typeName = .swift("String")
303+
case .base64: typeName = .base64
304+
default:
305+
// Check the format as well, for docs converted from OpenAPI 3.0.
306+
switch core.format {
307+
case .binary: typeName = .body
308+
case .byte: typeName = .base64
309+
case .dateTime: typeName = .date
310+
default: typeName = .string
311+
}
306312
}
307313
case .fragment: typeName = .valueContainer
308314
case let .object(_, objectContext):

Tests/OpenAPIGeneratorCoreTests/Translator/TypeAssignment/Test_TypeMatcher.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ final class Test_TypeMatcher: Test_Core {
2525

2626
static let builtinTypes: [(JSONSchema, String)] = [
2727
(.string, "Swift.String"), (.string(.init(format: .binary), .init()), "OpenAPIRuntime.HTTPBody"),
28+
(.string(.init(), .init(contentEncoding: .binary)), "OpenAPIRuntime.HTTPBody"),
2829
(.string(.init(format: .byte), .init()), "OpenAPIRuntime.Base64EncodedData"),
30+
(.string(.init(), .init(contentEncoding: .base64)), "OpenAPIRuntime.Base64EncodedData"),
2931
(.string(.init(format: .date), .init()), "Swift.String"),
3032
(.string(.init(format: .dateTime), .init()), "Foundation.Date"),
3133

0 commit comments

Comments
 (0)