Compare commits

...

4 Commits

Author SHA1 Message Date
Mathew Polzin cb2800abd4 Add support for RequestBody on an OpenAPI Operation. 2019-01-27 13:57:04 -08:00
Mathew Polzin 85d5fef3c8 Fix bug where some references were correctly encoded as objects and others were just encoded as strings 2019-01-27 13:22:18 -08:00
Mathew Polzin e4ef61fd56 bugfix: OpenAPI path components should begin with a slash. 2019-01-27 12:46:13 -08:00
Mathew Polzin 75ec4f156e Fix a third dictionary-as-array bug 2019-01-26 20:04:11 -08:00
2 changed files with 112 additions and 31 deletions
@@ -155,7 +155,6 @@ extension JSONNode: Encodable {
case oneOf
case anyOf
case not
case reference = "$ref"
}
public func encode(to encoder: Encoder) throws {
@@ -192,16 +191,20 @@ extension JSONNode: Encodable {
try container.encode(node, forKey: .not)
case .reference(let reference):
var container = encoder.container(keyedBy: SubschemaCodingKeys.self)
var container = encoder.singleValueContainer()
try container.encode(reference, forKey: .reference)
try container.encode(reference)
}
}
}
extension JSONReference: Encodable {
private enum CodingKeys: String, CodingKey {
case ref = "$ref"
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
var container = encoder.container(keyedBy: CodingKeys.self)
let referenceString: String = {
switch self {
@@ -212,7 +215,7 @@ extension JSONReference: Encodable {
}
}()
try container.encode(referenceString)
try container.encode(referenceString, forKey: .ref)
}
}
@@ -241,6 +244,32 @@ extension OpenAPIResponse.Code: Encodable {
}
}
extension OpenAPIRequestBody: Encodable {
private enum CodingKeys: String, CodingKey {
case description
case content
case required
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
if description != nil {
try container.encode(description, forKey: .description)
}
// Hack to work around Dictionary encoding
// itself as an array in this case:
let stringKeyedDict = Dictionary(
content.map { ($0.key.rawValue, $0.value) },
uniquingKeysWith: { $1 }
)
try container.encode(stringKeyedDict, forKey: .content)
try container.encode(required, forKey: .required)
}
}
extension OpenAPIPathItem.PathProperties.Operation: Encodable {
private enum CodingKeys: String, CodingKey {
case tags
@@ -276,6 +305,10 @@ extension OpenAPIPathItem.PathProperties.Operation: Encodable {
try container.encode(parameters, forKey: .parameters)
if requestBody != nil {
try container.encode(requestBody, forKey: .requestBody)
}
// Hack to work around Dictionary encoding
// itself as an array in this case:
let stringKeyedDict = Dictionary(
@@ -388,3 +421,34 @@ extension OpenAPIPathItem: Encodable {
}
}
}
extension OpenAPISchema: Encodable {
private enum CodingKeys: String, CodingKey {
case openAPIVersion = "openapi"
case info
case servers
case paths
case components
case security
case tags
case externalDocs
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(openAPIVersion, forKey: .openAPIVersion)
try container.encode(info, forKey: .info)
// Hack to work around Dictionary encoding
// itself as an array in this case:
let stringKeyedDict = Dictionary(
paths.map { ($0.key.rawValue, $0.value) },
uniquingKeysWith: { $1 }
)
try container.encode(stringKeyedDict, forKey: .paths)
try container.encode(components, forKey: .components)
}
}
@@ -759,7 +759,7 @@ public enum OpenAPIPathItem: Equatable {
// public let externalDocs:
public let operationId: String
public let parameters: ParameterArray
// public let requestBody:
public let requestBody: OpenAPIRequestBody?
public let responses: ResponseMap
// public let callbacks:
public let deprecated: Bool // default is false
@@ -771,6 +771,7 @@ public enum OpenAPIPathItem: Equatable {
description: String? = nil,
operationId: String,
parameters: ParameterArray,
requestBody: OpenAPIRequestBody? = nil,
responses: ResponseMap,
deprecated: Bool = false) {
self.tags = tags
@@ -778,29 +779,44 @@ public enum OpenAPIPathItem: Equatable {
self.description = description
self.operationId = operationId
self.parameters = parameters
self.requestBody = requestBody
self.responses = responses
self.deprecated = deprecated
}
public typealias ResponseMap = [OpenAPIResponse.Code: Either<OpenAPIResponse, JSONReference<OpenAPIComponents, OpenAPIResponse>>]
public typealias ContentMap = [OpenAPIContentType: OpenAPIContent]
}
}
}
public struct OpenAPIRequestBody: Equatable {
public let description: String?
public let content: OpenAPIPathItem.PathProperties.Operation.ContentMap
public let required: Bool
public init(description: String? = nil,
content: OpenAPIPathItem.PathProperties.Operation.ContentMap,
required: Bool = true) {
self.description = description
self.content = content
self.required = required
}
}
public struct OpenAPIResponse: Equatable {
public let description: String
// public let headers:
public let content: ContentMap
public let content: OpenAPIPathItem.PathProperties.Operation.ContentMap
// public let links:
public init(description: String,
content: ContentMap) {
content: OpenAPIPathItem.PathProperties.Operation.ContentMap) {
self.description = description
self.content = content
}
public typealias ContentMap = [ContentType: Content]
public enum Code: RawRepresentable, Equatable, Hashable {
public typealias RawValue = String
@@ -825,20 +841,20 @@ public struct OpenAPIResponse: Equatable {
}
}
}
}
public enum ContentType: String, Encodable, Equatable, Hashable {
case json = "application/json"
}
public enum OpenAPIContentType: String, Encodable, Equatable, Hashable {
case json = "application/json"
}
public struct Content: Encodable, Equatable {
public let schema: Either<JSONNode, JSONReference<OpenAPIComponents, JSONNode>>
// public let example:
// public let examples:
// public let encoding:
public struct OpenAPIContent: Encodable, Equatable {
public let schema: Either<JSONNode, JSONReference<OpenAPIComponents, JSONNode>>
// public let example:
// public let examples:
// public let encoding:
public init(schema: Either<JSONNode, JSONReference<OpenAPIComponents, JSONNode>>) {
self.schema = schema
}
public init(schema: Either<JSONNode, JSONReference<OpenAPIComponents, JSONNode>>) {
self.schema = schema
}
}
@@ -878,14 +894,7 @@ public struct OpenAPIComponents: Equatable, Encodable, ReferenceRoot {
}
/// The root of an OpenAPI 3.0 document.
public struct OpenAPISchema: Encodable {
private enum CodingKeys: String, CodingKey {
case openAPIVersion = "openapi"
case info
case paths
case components
}
public struct OpenAPISchema {
public let openAPIVersion: Version
public let info: Info
// public let servers:
@@ -928,17 +937,25 @@ public struct OpenAPISchema: Encodable {
}
}
public struct PathComponents: Encodable, Equatable, Hashable {
public struct PathComponents: RawRepresentable, Encodable, Equatable, Hashable {
public let components: [String]
public init(_ components: [String]) {
self.components = components
}
public init?(rawValue: String) {
components = rawValue.split(separator: "/").map(String.init)
}
public var rawValue: String {
return "/\(components.joined(separator: "/"))"
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(components.joined(separator: "/"))
try container.encode(rawValue)
}
}
}