Skip to content

Commit c4f3f1e

Browse files
authored
[Parsing] Allow OAS versions 3.1.2 and 3.2.0 (#840)
### Motivation ### Modifications The OpenAPIKit dependency has been bumped to 3.9.0+ to use the new version mapping feature. The generator has been instructed to accept OAS documents of versions 3.1.2 and 3.2.0 in addition to the existing supported versions. In order to parse OAS 3.2.0 without any of the features it adds, OpenAPIKit has been instructed to parse OAS 3.2.0 as if it were OAS 3.1.2. ### Result OpenAPI Documents versioned 3.1.2 and 3.2.0 will successfully load into the generator. ### Test Plan The test suite has been updated.
1 parent 076b1f7 commit c4f3f1e

File tree

5 files changed

+37
-20
lines changed

5 files changed

+37
-20
lines changed

Package.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ let package = Package(
4545
.package(url: "https://github.com/apple/swift-collections", from: "1.1.4"),
4646

4747
// Read OpenAPI documents
48-
.package(url: "https://github.com/mattpolzin/OpenAPIKit", from: "3.3.0"),
48+
.package(url: "https://github.com/mattpolzin/OpenAPIKit", from: "3.9.0"),
4949
.package(url: "https://github.com/jpsim/Yams", "4.0.0"..<"7.0.0"),
5050

5151
// CLI Tool
@@ -113,8 +113,10 @@ let package = Package(
113113
dependencies: [
114114
"_OpenAPIGeneratorCore",
115115
// Everything except windows: https://github.com/swiftlang/swift-package-manager/issues/6367
116-
.target(name: "swift-openapi-generator", condition: .when(platforms: [.android, .linux, .macOS, .openbsd, .wasi, .custom("freebsd")])),
117-
.product(name: "ArgumentParser", package: "swift-argument-parser"),
116+
.target(
117+
name: "swift-openapi-generator",
118+
condition: .when(platforms: [.android, .linux, .macOS, .openbsd, .wasi, .custom("freebsd")])
119+
), .product(name: "ArgumentParser", package: "swift-argument-parser"),
118120
],
119121
resources: [.copy("Resources")],
120122
swiftSettings: swiftSettings
@@ -162,5 +164,4 @@ for target in package.targets {
162164
case .macro, .plugin, .system, .binary: () // not applicable
163165
@unknown default: () // we don't know what to do here, do nothing
164166
}
165-
}
166-
// --- END: STANDARD CROSS-REPO SETTINGS DO NOT EDIT --- //
167+
}// --- END: STANDARD CROSS-REPO SETTINGS DO NOT EDIT --- //

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The code is generated at build-time, so it's always in sync with the OpenAPI doc
1717

1818
## Features
1919

20-
- Works with OpenAPI Specification versions 3.0 and 3.1.
20+
- Works with OpenAPI Specification versions 3.0 and 3.1 and has preliminary support for version 3.2.
2121
- Streaming request and response bodies enabling use cases such as JSON event streams, and large payloads without buffering.
2222
- Support for JSON, multipart, URL-encoded form, base64, plain text, and raw bytes, represented as value types with type-safe properties.
2323
- Client, server, and middleware abstractions, decoupling the generated code from the HTTP client library and web framework.
@@ -91,9 +91,9 @@ The Swift OpenAPI Generator project is split across multiple repositories to ena
9191

9292
## Requirements and supported features
9393

94-
| Generator versions | Supported OpenAPI versions |
95-
| ------------------ | -------------------------- |
96-
| `1.0.0` ... `main` | 3.0, 3.1 |
94+
| Generator versions | Supported OpenAPI versions |
95+
| ------------------ | --------------------------- |
96+
| `1.0.0` ... `main` | 3.0, 3.1, 3.2 (preliminary) |
9797

9898
See also [Supported OpenAPI features][supported-openapi-features].
9999

Sources/_OpenAPIGeneratorCore/Parser/YamsParser.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public struct YamsParser: ParserProtocol {
5454
let decoder = YAMLDecoder()
5555
let openapiData = input.contents
5656

57+
let decodingOptions = [
58+
DocumentConfiguration.versionMapKey: [
59+
// Until we move to OpenAPIKit v5.0+ we will parse OAS 3.2.0 as if it were OAS 3.1.2
60+
"3.2.0": OpenAPI.Document.Version.v3_1_2
61+
]
62+
]
63+
5764
struct OpenAPIVersionedDocument: Decodable { var openapi: String? }
5865

5966
let versionedDocument: OpenAPIVersionedDocument
@@ -73,7 +80,14 @@ public struct YamsParser: ParserProtocol {
7380
case "3.0.0", "3.0.1", "3.0.2", "3.0.3", "3.0.4":
7481
let openAPI30Document = try decoder.decode(OpenAPIKit30.OpenAPI.Document.self, from: input.contents)
7582
document = openAPI30Document.convert(to: .v3_1_0)
76-
case "3.1.0", "3.1.1": document = try decoder.decode(OpenAPIKit.OpenAPI.Document.self, from: input.contents)
83+
case "3.1.0", "3.1.1", "3.1.2":
84+
document = try decoder.decode(OpenAPIKit.OpenAPI.Document.self, from: input.contents)
85+
case "3.2.0":
86+
document = try decoder.decode(
87+
OpenAPIKit.OpenAPI.Document.self,
88+
from: input.contents,
89+
userInfo: decodingOptions
90+
)
7791
default:
7892
throw Diagnostic.openAPIVersionError(
7993
versionString: "openapi: \(openAPIVersion)",
@@ -128,7 +142,7 @@ extension Diagnostic {
128142
static func openAPIVersionError(versionString: String, location: Location) -> Diagnostic {
129143
error(
130144
message:
131-
"Unsupported document version: \(versionString). Please provide a document with OpenAPI versions in the 3.0.x or 3.1.x sets.",
145+
"Unsupported document version: \(versionString). Please provide a document with OpenAPI versions in the 3.0.x, 3.1.x, or 3.2.x sets.",
132146
location: location
133147
)
134148
}
@@ -139,7 +153,7 @@ extension Diagnostic {
139153
static func openAPIMissingVersionError(location: Location) -> Diagnostic {
140154
error(
141155
message:
142-
"No key named openapi found. Please provide a valid OpenAPI document with OpenAPI versions in the 3.0.x or 3.1.x sets.",
156+
"No key named openapi found. Please provide a valid OpenAPI document with OpenAPI versions in the 3.0.x, 3.1.x, or 3.2.x sets.",
143157
location: location
144158
)
145159
}

Sources/swift-openapi-generator/Documentation.docc/Swift-OpenAPI-Generator.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The code is generated at build-time, so it's always in sync with the OpenAPI doc
1616

1717
## Features
1818

19-
- Works with OpenAPI Specification versions 3.0 and 3.1.
19+
- Works with OpenAPI Specification versions 3.0 and 3.1 and has preliminary support for version 3.2.
2020
- Streaming request and response bodies enabling use cases such as JSON event streams, and large payloads without buffering.
2121
- Support for JSON, multipart, URL-encoded form, base64, plain text, and raw bytes, represented as value types with type-safe properties.
2222
- Client, server, and middleware abstractions, decoupling the generated code from the HTTP client library and web framework.
@@ -90,9 +90,9 @@ The Swift OpenAPI Generator project is split across multiple repositories to ena
9090

9191
### Requirements and supported features
9292

93-
| Generator versions | Supported OpenAPI versions | Minimum Swift version |
94-
| ------------------ | -------------------------- | --------------------- |
95-
| `1.0.0` ... `main` | 3.0, 3.1 | 5.9 |
93+
| Generator versions | Supported OpenAPI versions | Minimum Swift version |
94+
| ------------------ | --------------------------- | --------------------- |
95+
| `1.0.0` ... `main` | 3.0, 3.1, 3.2 (preliminary) | 5.9 |
9696

9797
See also <doc:Supported-OpenAPI-features>.
9898

Tests/OpenAPIGeneratorCoreTests/Parser/Test_YamsParser.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ final class Test_YamsParser: Test_Core {
2424
XCTAssertNoThrow(try _test(openAPIVersionString: "3.0.4"))
2525
XCTAssertNoThrow(try _test(openAPIVersionString: "3.1.0"))
2626
XCTAssertNoThrow(try _test(openAPIVersionString: "3.1.1"))
27+
XCTAssertNoThrow(try _test(openAPIVersionString: "3.1.2"))
28+
XCTAssertNoThrow(try _test(openAPIVersionString: "3.2.0"))
2729

2830
let expected1 =
29-
"/foo.yaml: error: Unsupported document version: openapi: 3.2.0. Please provide a document with OpenAPI versions in the 3.0.x or 3.1.x sets."
30-
assertThrownError(try _test(openAPIVersionString: "3.2.0"), expectedDiagnostic: expected1)
31+
"/foo.yaml: error: Unsupported document version: openapi: 3.3.0. Please provide a document with OpenAPI versions in the 3.0.x, 3.1.x, or 3.2.x sets."
32+
assertThrownError(try _test(openAPIVersionString: "3.3.0"), expectedDiagnostic: expected1)
3133

3234
let expected2 =
33-
"/foo.yaml: error: Unsupported document version: openapi: 2.0. Please provide a document with OpenAPI versions in the 3.0.x or 3.1.x sets."
35+
"/foo.yaml: error: Unsupported document version: openapi: 2.0. Please provide a document with OpenAPI versions in the 3.0.x, 3.1.x, or 3.2.x sets."
3436
assertThrownError(try _test(openAPIVersionString: "2.0"), expectedDiagnostic: expected2)
3537
}
3638

@@ -56,7 +58,7 @@ final class Test_YamsParser: Test_Core {
5658
"""
5759

5860
let expected =
59-
"/foo.yaml: error: No key named openapi found. Please provide a valid OpenAPI document with OpenAPI versions in the 3.0.x or 3.1.x sets."
61+
"/foo.yaml: error: No key named openapi found. Please provide a valid OpenAPI document with OpenAPI versions in the 3.0.x, 3.1.x, or 3.2.x sets."
6062
assertThrownError(try _test(yaml), expectedDiagnostic: expected)
6163
}
6264

0 commit comments

Comments
 (0)