Skip to content

Commit d2dbd0d

Browse files
authored
Support OpenAPI 3.1 in addition to 3.0 (#219)
Support OpenAPI 3.1 in addition to 3.0 ### Motivation Fixes #39. We need to start accepting 3.1 documents in addition to 3.0. It also has useful features, like being able to add descriptions to references. ### Modifications Migrated the codebase to use the `OpenAPIKit` instead of the `OpenAPIKit30` module, and updated the parser to handle both 3.0 and 3.1, and convert 3.0 to 3.1 documents in memory transparently. ### Result OpenAPI 3.1 documents are now accepted, instead of rejected. Any 3.1 specific features can be addressed separately, this is the MVP support. ### Test Plan Updated the file-based reference test to use version 3.1.0, and the parser tests that 3.0.x documents are loaded and converted successfully. So no need to duplicate our reference tests or anything. Reviewed by: Kyle-Ye, glbrntt Builds: ✔︎ pull request validation (5.8) - Build finished. ✔︎ pull request validation (5.9) - 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. #219
1 parent 640a58c commit d2dbd0d

File tree

95 files changed

+180
-173
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+180
-173
lines changed

IntegrationTest/Sources/openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
openapi: "3.0.3"
1+
openapi: "3.1.0"
22
info:
33
title: "GreetingService"
44
version: "1.0.0"

Package.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ let package = Package(
6464
// Read OpenAPI documents
6565
.package(
6666
url: "https://github.com/mattpolzin/OpenAPIKit.git",
67-
exact: "3.0.0-beta.1"
67+
exact: "3.0.0-beta.2"
6868
),
6969
.package(
7070
url: "https://github.com/jpsim/Yams.git",
@@ -91,7 +91,9 @@ let package = Package(
9191
.target(
9292
name: "_OpenAPIGeneratorCore",
9393
dependencies: [
94+
.product(name: "OpenAPIKit", package: "OpenAPIKit"),
9495
.product(name: "OpenAPIKit30", package: "OpenAPIKit"),
96+
.product(name: "OpenAPIKitCompat", package: "OpenAPIKit"),
9597
.product(name: "Algorithms", package: "swift-algorithms"),
9698
.product(name: "Yams", package: "Yams"),
9799
.product(name: "SwiftSyntax", package: "swift-syntax"),

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ Choose one of the transports listed below, or create your own by adopting the `C
2929

3030
## Requirements and supported features
3131

32-
- Swift 5.8
33-
- OpenAPI 3.0.x
32+
| Generator versions | Supported OpenAPI versions | Minimum Swift version |
33+
| -------- | ------- | ----- |
34+
| `0.1.0` ... `0.1.11` | 3.0 | 5.8 |
35+
| `0.1.12` ... `main` | 3.0, 3.1 | 5.8 |
3436

3537
### Supported platforms and minimum versions
3638

Sources/_OpenAPIGeneratorCore/Diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414
import Foundation
15-
import OpenAPIKit30
15+
import OpenAPIKit
1616

1717
/// A message emitted by the generator.
1818
public struct Diagnostic: Error, Codable {

Sources/_OpenAPIGeneratorCore/Extensions/OpenAPIKit.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14-
import OpenAPIKit30
14+
import OpenAPIKit
1515

1616
extension Either {
1717

@@ -20,7 +20,7 @@ extension Either {
2020
/// - Parameter components: The Components section of the OpenAPI document.
2121
func resolve(
2222
in components: OpenAPI.Components
23-
) throws -> B where A == JSONReference<B> {
23+
) throws -> B where A == OpenAPI.Reference<B> {
2424
switch self {
2525
case let .a(a):
2626
return try components.lookup(a)
@@ -59,6 +59,8 @@ extension JSONSchema.Schema {
5959
return "reference"
6060
case .fragment:
6161
return "fragment"
62+
case .null:
63+
return "null"
6264
}
6365
}
6466

@@ -89,6 +91,8 @@ extension JSONSchema.Schema {
8991
return nil
9092
case .fragment(let coreContext):
9193
return coreContext.formatString
94+
case .null:
95+
return nil
9296
}
9397
}
9498

Sources/_OpenAPIGeneratorCore/GeneratorPipeline.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14-
import OpenAPIKit30
14+
import OpenAPIKit
1515
import Foundation
1616
import Yams
1717

Sources/_OpenAPIGeneratorCore/Layers/ParsedOpenAPIRepresentation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14-
import OpenAPIKit30
14+
import OpenAPIKit
1515

1616
/// An OpenAPIKit document that contains the operations and types for which the generator emits Swift types.
1717
typealias ParsedOpenAPIRepresentation = OpenAPI.Document

Sources/_OpenAPIGeneratorCore/Parser/YamsParser.swift

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414
import Foundation
15+
import OpenAPIKit
1516
import OpenAPIKit30
17+
import OpenAPIKitCompat
1618
import Yams
1719

1820
/// A parser that uses the Yams library to parse the provided
@@ -44,21 +46,27 @@ struct YamsParser: ParserProtocol {
4446
guard let openAPIVersion = versionedDocument.openapi else {
4547
throw Diagnostic.openAPIMissingVersionError(location: .init(filePath: input.absolutePath.path))
4648
}
47-
switch openAPIVersion {
48-
case "3.0.0", "3.0.1", "3.0.2", "3.0.3":
49-
break
50-
default:
51-
throw Diagnostic.openAPIVersionError(
52-
versionString: "openapi: \(openAPIVersion)",
53-
location: .init(filePath: input.absolutePath.path)
54-
)
55-
}
56-
5749
do {
58-
return try decoder.decode(
59-
OpenAPI.Document.self,
60-
from: input.contents
61-
)
50+
let document: OpenAPIKit.OpenAPI.Document
51+
switch openAPIVersion {
52+
case "3.0.0", "3.0.1", "3.0.2", "3.0.3":
53+
let openAPI30Document = try decoder.decode(
54+
OpenAPIKit30.OpenAPI.Document.self,
55+
from: input.contents
56+
)
57+
document = openAPI30Document.convert(to: .v3_1_0)
58+
case "3.1.0":
59+
document = try decoder.decode(
60+
OpenAPIKit.OpenAPI.Document.self,
61+
from: input.contents
62+
)
63+
default:
64+
throw Diagnostic.openAPIVersionError(
65+
versionString: "openapi: \(openAPIVersion)",
66+
location: .init(filePath: input.absolutePath.path)
67+
)
68+
}
69+
return document
6270
} catch DecodingError.dataCorrupted(let errorContext) {
6371
try checkParsingError(context: errorContext, input: input)
6472
throw DecodingError.dataCorrupted(errorContext)
@@ -104,7 +112,7 @@ extension Diagnostic {
104112
static func openAPIVersionError(versionString: String, location: Location) -> Diagnostic {
105113
return error(
106114
message:
107-
"Unsupported document version: \(versionString). Please provide a document with OpenAPI versions in the 3.0.x set.",
115+
"Unsupported document version: \(versionString). Please provide a document with OpenAPI versions in the 3.0.x or 3.1.x sets.",
108116
location: location
109117
)
110118
}
@@ -115,7 +123,7 @@ extension Diagnostic {
115123
static func openAPIMissingVersionError(location: Location) -> Diagnostic {
116124
return error(
117125
message:
118-
"No openapi key found, please provide a valid OpenAPI document with OpenAPI versions in the 3.0.x set.",
126+
"No openapi key found, please provide a valid OpenAPI document with OpenAPI versions in the 3.0.x or 3.1.x sets.",
119127
location: location
120128
)
121129
}

Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14-
import OpenAPIKit30
14+
import OpenAPIKit
1515

1616
/// A translator for the generated client.
1717
///

Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/translateClientMethod.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14-
import OpenAPIKit30
14+
import OpenAPIKit
1515

1616
extension ClientFileTranslator {
1717

0 commit comments

Comments
 (0)