Skip to content

Commit

Permalink
Add support for RequestBody on an OpenAPI Operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpolzin committed Jan 27, 2019
1 parent 85d5fef commit cb2800a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 16 deletions.
30 changes: 30 additions & 0 deletions Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,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
Expand Down Expand Up @@ -279,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(
Expand Down
48 changes: 32 additions & 16 deletions Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -771,36 +771,52 @@ public enum OpenAPIPathItem: Equatable {
description: String? = nil,
operationId: String,
parameters: ParameterArray,
requestBody: OpenAPIRequestBody? = nil,
responses: ResponseMap,
deprecated: Bool = false) {
self.tags = tags
self.summary = summary
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

Expand All @@ -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
}
}

Expand Down

0 comments on commit cb2800a

Please sign in to comment.