SwiftOpenAPI is a Swift library which can generate output compatible with OpenAPI version 3.1.0. You can describe your API using OpenAPIObject type. The main accent in the library is on simplifying the syntax: the active use of literals (array, dictionary, string etc) and static methods greatly simplifies writing and reading OpenAPI docs in Swift.
There is a possibility to create SchemeObject, [ParameterObject], AnyValue and [String: HeaderObject] instances from Codable types. It’s possible to use SchemeObject.decode/encode, [ParameterObject].decode/encode, [String: HeaderObject].decode/encode and AnyValue.encode methods for it.
let loginBodySchemeFromType: SchemeObject = try .decode(LoginBody.self)
let loginBodySchemeFromInstance: SchemeObject = try .encode(LoginBody.example)
let loginBodyExample = try ExampleObject(value: .encode(LoginBody.example))
You can customize the encoding/decoding result by implementing OpenAPIDescriptable and OpenAPIType protocols.
OpenAPIDescriptable protocol allows you to provide a custom description for the type and its properties.
OpenAPIType protocol allows you to provide a custom schema for the type.
struct Color: Codable, OpenAPIType {
static var openAPISchema: SchemaObject {
.string(format: "hex", description: "Color in hex format")
}
}
Specification extensions
While the OpenAPI Specification tries to accommodate most use cases, additional data can be added to extend the specification at certain points.\
var api = OpenAPIObject(...)
api.specificationExtensions = ["x-some-extension": "some value"]
// or
api.specificationExtensions = try? SpecificationExtensions(from: someEncodable)
It was a bit tricky challenge to implement additional dynamic properties for any codable struct. The solution is to use SpecificationExtendable protocol in combination with WithSpecExtensions property wrapper.
There is two ways to decode/encode SpecificationExtendable types with additional properties:
Use SpecificationExtendable.json, SpecificationExtendable.Type.from(json:) methods.
let schema = try SchemaObject.from(json: jsonData)
let jsonData = try schema.json()
If you cannot use custom decoding methods, you can use WithSpecExtensions wrapper.
let api = try WithSpecExtensions(wrappedValue: OpenAPIObject(...))
let jsonData = try JSONEncoder().encode(api)
SwiftOpenAPI
Description
SwiftOpenAPI is a Swift library which can generate output compatible with OpenAPI version 3.1.0. You can describe your API using
OpenAPIObjecttype.The main accent in the library is on simplifying the syntax: the active use of literals (array, dictionary, string etc) and static methods greatly simplifies writing and reading
OpenAPIdocs inSwift.Short example
Pets store example
PetsSwagger.swift demonstrates syntaxis well
Creating schemas and parameters for
CodabletypesThere is a possibility to create
SchemeObject,[ParameterObject],AnyValueand[String: HeaderObject]instances fromCodabletypes. It’s possible to useSchemeObject.decode/encode,[ParameterObject].decode/encode,[String: HeaderObject].decode/encodeandAnyValue.encodemethods for it.You can customize the encoding/decoding result by implementing
OpenAPIDescriptableandOpenAPITypeprotocols.OpenAPIDescriptableprotocol allows you to provide a custom description for the type and its properties.OpenAPITypeprotocol allows you to provide a custom schema for the type.Specification extensions
While the OpenAPI Specification tries to accommodate most use cases, additional data can be added to extend the specification at certain points.\
It was a bit tricky challenge to implement additional dynamic properties for any codable struct. The solution is to use
SpecificationExtendableprotocol in combination withWithSpecExtensionsproperty wrapper. There is two ways to decode/encodeSpecificationExtendabletypes with additional properties:SpecificationExtendable.json,SpecificationExtendable.Type.from(json:)methods.WithSpecExtensionswrapper.TODO
URItype instead ofStringrefactormethod onOpenAPIObject(?)RuntimeExpressiontypeDataEncodingFormatInstallation
Create a
Package.swiftfile.Add the following line to your Podfile:
and run
pod updatefrom the podfile directory first.Related projects
Author
dankinsoid, voidilov@gmail.com
License
SwiftOpenAPI is available under the MIT license. See the LICENSE file for more info.