Skip to content

Commit

Permalink
Merge pull request #263 from mattpolzin/feature/195/compat-take-2
Browse files Browse the repository at this point in the history
First pass at function that takes OpenAPI 3.0 documents to OpenAPI 3.1 documents
  • Loading branch information
mattpolzin authored Dec 26, 2022
2 parents 15e39d1 + d96f819 commit d40e6c3
Show file tree
Hide file tree
Showing 59 changed files with 2,345 additions and 1,855 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on: [pull_request]
jobs:
codecov:
container:
image: swift:5.5
image: swift:5.7
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
14 changes: 10 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@ jobs:
- swift:5.5-focal
- swift:5.5-centos8
- swift:5.5-amazonlinux2
- swiftlang/swift:nightly-xenial
- swift:5.6-bionic
- swift:5.6-focal
- swift:5.6-amazonlinux2
- swift:5.7-bionic
- swift:5.7-focal
- swift:5.7-jammy
- swift:5.7-amazonlinux2
- swiftlang/swift:nightly-bionic
- swiftlang/swift:nightly-focal
- swiftlang/swift:nightly-centos8
- swiftlang/swift:nightly-jammy
- swiftlang/swift:nightly-amazonlinux2
container: ${{ matrix.image }}
steps:
Expand All @@ -74,10 +80,10 @@ jobs:
- name: Run tests
run: swift test
osx:
runs-on: macOS-11
runs-on: macOS-12
steps:
- name: Select latest available Xcode
uses: maxim-lobanov/setup-xcode@v1.2.3
uses: maxim-lobanov/setup-xcode@v1.5.1
with:
xcode-version: latest
- name: Checkout code
Expand Down
12 changes: 11 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ let package = Package(
.library(
name: "OpenAPIKit",
targets: ["OpenAPIKit"]),
.library(
name: "OpenAPIKitCompat",
targets: ["OpenAPIKitCompat"]),
],
dependencies: [
.package(url: "https://github.com/jpsim/Yams.git", "4.0.0"..<"6.0.0") // just for tests
Expand Down Expand Up @@ -60,7 +63,14 @@ let package = Package(
dependencies: ["OpenAPIKit", "Yams"]),
.testTarget(
name: "OpenAPIKitErrorReportingTests",
dependencies: ["OpenAPIKit", "Yams"])
dependencies: ["OpenAPIKit", "Yams"]),

.target(
name: "OpenAPIKitCompat",
dependencies: ["OpenAPIKit30", "OpenAPIKit"]),
.testTarget(
name: "OpenAPIKitCompatTests",
dependencies: ["OpenAPIKitCompat"])
],
swiftLanguageVersions: [ .v5 ]
)
56 changes: 0 additions & 56 deletions Sources/OpenAPIKit/Callbacks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,6 @@ import Foundation

extension OpenAPI {

/// A URL template where the placeholders are OpenAPI **Runtime Expressions** instead
/// of named variables.
///
/// See [OpenAPI Callback Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#callback-object) and [OpenAPI Runtime Expression](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#runtime-expressions) for more.
///
public struct CallbackURL: Hashable, RawRepresentable {
public let template: URLTemplate

/// The string value of the URL without variable replacement.
///
/// Variables cannot be replaced based on other information in the
/// OpenAPI document; they are only available at "runtime" which is
/// where the name of the OpenAPI structure `CallbackURL`
/// represents comes from.
public var rawValue: String {
template.rawValue
}

/// Get a URL from the runtime expression if it is a valid URL without
/// variable replacement.
///
/// Callback URLs with variables in them will not be valid URLs
/// and are therefore guaranteed to return `nil`.
public var url: URL? {
template.url
}

/// Create a CallbackURL from the string if possible.
public init?(rawValue: String) {
guard let template = URLTemplate(rawValue: rawValue) else {
return nil
}
self.template = template
}

public init(url: URL) {
template = .init(url: url)
}
}

/// A map from runtime expressions to path items to be used as
/// callbacks for the API. The OpenAPI Spec "Callback Object."
///
Expand All @@ -60,19 +20,3 @@ extension OpenAPI {
/// A map of named collections of Callback Objects (`OpenAPI.Callbacks`).
public typealias CallbacksMap = OrderedDictionary<String, Either<OpenAPI.Reference<Callbacks>, Callbacks>>
}

extension OpenAPI.CallbackURL: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()

try container.encode(rawValue)
}
}

extension OpenAPI.CallbackURL: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()

template = try container.decode(URLTemplate.self)
}
}
58 changes: 0 additions & 58 deletions Sources/OpenAPIKit/Components Object/Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,64 +72,6 @@ extension OpenAPI {
}

extension OpenAPI {
/// A key for one of the component dictionaries.
///
/// These keys must match the regex
/// `^[a-zA-Z0-9\.\-_]+$`.
public struct ComponentKey: RawRepresentable, ExpressibleByStringLiteral, Codable, Equatable, Hashable, StringConvertibleHintProvider {
public let rawValue: String

public init(stringLiteral value: StringLiteralType) {
self.rawValue = value
}

public init?(rawValue: String) {
guard !rawValue.isEmpty else {
return nil
}
var allowedCharacters = CharacterSet.alphanumerics
allowedCharacters.insert(charactersIn: "-_.")
guard CharacterSet(charactersIn: rawValue).isSubset(of: allowedCharacters) else {
return nil
}
self.rawValue = rawValue
}

public static func problem(with proposedString: String) -> String? {
if Self(rawValue: proposedString) == nil {
return "Keys for components in the Components Object must conform to the regex `^[a-zA-Z0-9\\.\\-_]+$`. '\(proposedString)' does not.."
}
return nil
}

public init(from decoder: Decoder) throws {
let rawValue = try decoder.singleValueContainer().decode(String.self)
guard let key = Self(rawValue: rawValue) else {
throw InconsistencyError(
subjectName: "Component Key",
details: "Keys for components in the Components Object must conform to the regex `^[a-zA-Z0-9\\.\\-_]+$`. '\(rawValue)' does not..",
codingPath: decoder.codingPath
)
}
self = key
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()

// we check for consistency on encode because a string literal
// may result in an invalid component key being constructed.
guard Self(rawValue: rawValue) != nil else {
throw InconsistencyError(
subjectName: "Component Key",
details: "Keys for components in the Components Object must conform to the regex `^[a-zA-Z0-9\\.\\-_]+$`. '\(rawValue)' does not..",
codingPath: container.codingPath
)
}

try container.encode(rawValue)
}
}

public typealias ComponentDictionary<T> = OrderedDictionary<ComponentKey, T>
}
Expand Down
File renamed without changes.
9 changes: 0 additions & 9 deletions Sources/OpenAPIKit/Parameter/ParameterContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,6 @@ extension OpenAPI.Parameter {
}

extension OpenAPI.Parameter.Context {
public enum Location: String, CaseIterable, Codable {
case query
case header
case path
case cookie
}

public var location: Location {
switch self {
case .query:
Expand All @@ -93,5 +86,3 @@ extension OpenAPI.Parameter.Context {
}
}
}

extension OpenAPI.Parameter.Context.Location: Validatable {}
56 changes: 23 additions & 33 deletions Sources/OpenAPIKit/Parameter/ParameterSchemaContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,41 +127,31 @@ extension OpenAPI.Parameter {
}
}

extension OpenAPI.Parameter.SchemaContext {
public enum Style: String, CaseIterable, Codable {
case form
case simple
case matrix
case label
case spaceDelimited
case pipeDelimited
case deepObject

/// Get the default `Style` for the given location
/// per the OpenAPI Specification.
///
/// See the `style` fixed field under
/// [OpenAPI Parameter Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#parameter-object).
public static func `default`(for location: OpenAPI.Parameter.Context) -> Self {
switch location {
case .query:
return .form
case .cookie:
return .form
case .path:
return .simple
case .header:
return .simple
}
extension OpenAPI.Parameter.SchemaContext.Style {
/// Get the default `Style` for the given location
/// per the OpenAPI Specification.
///
/// See the `style` fixed field under
/// [OpenAPI Parameter Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#parameter-object).
public static func `default`(for location: OpenAPI.Parameter.Context) -> Self {
switch location {
case .query:
return .form
case .cookie:
return .form
case .path:
return .simple
case .header:
return .simple
}
}

internal var defaultExplode: Bool {
switch self {
case .form:
return true
default:
return false
}
internal var defaultExplode: Bool {
switch self {
case .form:
return true
default:
return false
}
}
}
Expand Down
31 changes: 0 additions & 31 deletions Sources/OpenAPIKit/Path Item/PathItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,6 @@

import OpenAPIKitCore

extension OpenAPI {
/// OpenAPI Spec "Paths Object" path field pattern support.
///
/// See [OpenAPI Paths Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#paths-object)
/// and [OpenAPI Patterned Fields](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#patterned-fields).
public struct Path: RawRepresentable, Equatable, Hashable {
public let components: [String]

public init(_ components: [String]) {
self.components = components
}

public init(rawValue: String) {
let pathComponents = rawValue.split(separator: "/").map(String.init)
components = pathComponents.count > 0 && pathComponents[0].isEmpty
? Array(pathComponents.dropFirst())
: pathComponents
}

public var rawValue: String {
return "/\(components.joined(separator: "/"))"
}
}
}

extension OpenAPI.Path: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(rawValue: value)
}
}

extension OpenAPI {
/// OpenAPI Spec "Path Item Object"
///
Expand Down
Loading

0 comments on commit d40e6c3

Please sign in to comment.