Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support OpenAPI 3.1 in addition to 3.0 #219

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ let package = Package(
.target(
name: "_OpenAPIGeneratorCore",
dependencies: [
.product(name: "OpenAPIKit", package: "OpenAPIKit"),
.product(name: "OpenAPIKit30", package: "OpenAPIKit"),
.product(name: "OpenAPIKitCompat", package: "OpenAPIKit"),
.product(name: "Algorithms", package: "swift-algorithms"),
.product(name: "Yams", package: "Yams"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
Expand Down
2 changes: 1 addition & 1 deletion Sources/_OpenAPIGeneratorCore/Diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
import Foundation
import OpenAPIKit30
import OpenAPIKit

/// A message emitted by the generator.
public struct Diagnostic: Error, Codable {
Expand Down
8 changes: 6 additions & 2 deletions Sources/_OpenAPIGeneratorCore/Extensions/OpenAPIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

extension Either {

Expand All @@ -20,7 +20,7 @@ extension Either {
/// - Parameter components: The Components section of the OpenAPI document.
func resolve(
in components: OpenAPI.Components
) throws -> B where A == JSONReference<B> {
) throws -> B where A == OpenAPI.Reference<B> {
switch self {
case let .a(a):
return try components.lookup(a)
Expand Down Expand Up @@ -59,6 +59,8 @@ extension JSONSchema.Schema {
return "reference"
case .fragment:
return "fragment"
case .null:
return "null"
}
}

Expand Down Expand Up @@ -89,6 +91,8 @@ extension JSONSchema.Schema {
return nil
case .fragment(let coreContext):
return coreContext.formatString
case .null:
return nil
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/_OpenAPIGeneratorCore/GeneratorPipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit
import Foundation
import Yams

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

/// An OpenAPIKit document that contains the operations and types for which the generator emits Swift types.
typealias ParsedOpenAPIRepresentation = OpenAPI.Document
40 changes: 24 additions & 16 deletions Sources/_OpenAPIGeneratorCore/Parser/YamsParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
//
//===----------------------------------------------------------------------===//
import Foundation
import OpenAPIKit
import OpenAPIKit30
import OpenAPIKitCompat
import Yams

/// A parser that uses the Yams library to parse the provided
Expand Down Expand Up @@ -44,21 +46,27 @@ struct YamsParser: ParserProtocol {
guard let openAPIVersion = versionedDocument.openapi else {
throw Diagnostic.openAPIMissingVersionError(location: .init(filePath: input.absolutePath.path))
}
switch openAPIVersion {
case "3.0.0", "3.0.1", "3.0.2", "3.0.3":
break
default:
throw Diagnostic.openAPIVersionError(
versionString: "openapi: \(openAPIVersion)",
location: .init(filePath: input.absolutePath.path)
)
}

do {
return try decoder.decode(
OpenAPI.Document.self,
from: input.contents
)
let document: OpenAPIKit.OpenAPI.Document
switch openAPIVersion {
case "3.0.0", "3.0.1", "3.0.2", "3.0.3":
let openAPI30Document = try decoder.decode(
OpenAPIKit30.OpenAPI.Document.self,
from: input.contents
)
document = openAPI30Document.convert(to: .v3_1_0)
case "3.1.0":
document = try decoder.decode(
OpenAPIKit.OpenAPI.Document.self,
from: input.contents
)
default:
throw Diagnostic.openAPIVersionError(
versionString: "openapi: \(openAPIVersion)",
location: .init(filePath: input.absolutePath.path)
)
}
return document
} catch DecodingError.dataCorrupted(let errorContext) {
try checkParsingError(context: errorContext, input: input)
throw DecodingError.dataCorrupted(errorContext)
Expand Down Expand Up @@ -104,7 +112,7 @@ extension Diagnostic {
static func openAPIVersionError(versionString: String, location: Location) -> Diagnostic {
return error(
message:
"Unsupported document version: \(versionString). Please provide a document with OpenAPI versions in the 3.0.x set.",
"Unsupported document version: \(versionString). Please provide a document with OpenAPI versions in the 3.0.x or 3.1.x sets.",
czechboy0 marked this conversation as resolved.
Show resolved Hide resolved
location: location
)
}
Expand All @@ -115,7 +123,7 @@ extension Diagnostic {
static func openAPIMissingVersionError(location: Location) -> Diagnostic {
return error(
message:
"No openapi key found, please provide a valid OpenAPI document with OpenAPI versions in the 3.0.x set.",
"No openapi key found, please provide a valid OpenAPI document with OpenAPI versions in the 3.0.x or 3.1.x sets.",
location: location
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

/// A translator for the generated client.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

extension ClientFileTranslator {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

/// Describes one of the two options: allOf or anyOf.
enum AllOrAnyOf {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

extension FileTranslator {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

extension FileTranslator {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

extension FileTranslator {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

extension FileTranslator {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

extension FileTranslator {

Expand All @@ -38,7 +38,7 @@ extension FileTranslator {
switch schema {
case let .a(ref):
// reference, wrap that into JSONSchema
unwrappedSchema = .reference(ref)
unwrappedSchema = .reference(ref.jsonReference)
case let .b(schema):
unwrappedSchema = schema
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

extension FileTranslator {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

extension FileTranslator {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

extension FileTranslator {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

extension Comment {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

/// Constant values used in generated code, some of which refer to type names
/// in the Runtime library, so they need to be kept in sync.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit
import Foundation

/// A child schema of a oneOf with a discriminator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

/// A container of properties that can be defined at multiple levels in
/// the OpenAPI document. If a property is filled in, the value is used instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

/// A structure that contains the information about an OpenAPI object that is
/// required to generate a matching Swift structure.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

/// Utilities for asking questions about OpenAPI.Content
extension FileTranslator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

extension FileTranslator {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

/// A content type of a request, response, and other types.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

/// A type representing OpenAPI content that contains both a content type
/// and the optional JSON schema.
Expand Down Expand Up @@ -48,4 +48,4 @@ struct TypedSchemaContent {
/// An unresolved OpenAPI schema.
///
/// Can be either a reference or an inline schema.
typealias UnresolvedSchema = Either<JSONReference<JSONSchema>, JSONSchema>
typealias UnresolvedSchema = Either<OpenAPI.Reference<JSONSchema>, JSONSchema>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

extension FileTranslator {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

/// An object that generates a Swift file for a provided OpenAPI document.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

/// A translator that inspects the generator configuration and delegates
/// the code generation logic to the appropriate translator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

/// A wrapper of an OpenAPI operation that includes the information
/// about the parent containers of the operation, such as its path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit30
import OpenAPIKit

/// A container for an OpenAPI parameter and its computed Swift type usage.
struct TypedParameter {
Expand Down Expand Up @@ -258,7 +258,7 @@ extension FileTranslator {
/// An unresolved OpenAPI parameter.
///
/// Can be either a reference or an inline parameter.
typealias UnresolvedParameter = Either<JSONReference<OpenAPI.Parameter>, OpenAPI.Parameter>
typealias UnresolvedParameter = Either<OpenAPI.Reference<OpenAPI.Parameter>, OpenAPI.Parameter>

extension OpenAPI.Parameter.Context.Location {

Expand Down
Loading