Skip to content

Latest and Greatest

Pre-release
Pre-release
Compare
Choose a tag to compare
@mattpolzin mattpolzin released this 26 Dec 03:05
d40e6c3

⚠️ This version attempts not to break anything, but it does move some types into different modules and use type aliasing to retain existing namespacing so there is some possibility of a problem there. Please report any issues you have if you move to this version in your project!


With this version, the new OpenAPIKitCompat module can be used to take an OpenAPIKit30.OpenAPI.Document and convert it into an OpenAPIKit.OpenAPI.Document. In other words, you can convert an OpenAPI v3.0 document into an OpenAPI v3.1 document. Equally as importantly, you can create projects with- (or migrate existing projects over to-) the OpenAPIKit module where they used to use the OpenAPIKit30 module and support the previous version of the OpenAPI standard by decoding to OpenAPIKit30.OpenAPI.Document and then calling convert(to: .v3_1_0) on the result.

For example, a (currently unreleased) branch of the OpenAPIDiff project supports both OpenAPI 3.0 and 3.1 in its main executable while most of the project only cares about OpenAPI 3.1 with the following code that decodes and converts v3.0 documents: https://github.com/mattpolzin/OpenAPIDiff/blob/openapikit-3/Sources/openapi-diff/main.swift#L63..L100

Here's the gist:

// import OpenAPIKit30 for OpenAPI 3.0 document support
import OpenAPIKit30
// import OpenAPIKit for OpenAPI 3.1 document support
import OpenAPIKit
// import OpenAPIKitCompat to convert between the versions
import OpenAPIKitCompat

// if most of your project just works with OpenAPI v3.1, most files only need to import OpenAPIKit.
// Only in the file where you are supporting converting from OpenAPI v3.0 to v3.1 do you need the
// other two imports.

// we can support either version by attempting to parse an old version and then a new version if the old version fails
let oldDoc: OpenAPIKit30.OpenAPI.Document?
let newDoc: OpenAPIKit.OpenAPI.Document

oldDoc = try? JSONDecoder().decode(OpenAPI.Document.self, from: someFileData)

newDoc = oldDoc?.convert(to: .v3_1_0) ??
  (try! JSONDecoder().decode(OpenAPI.Document.self, from: someFileData))