Skip to content

Commit

Permalink
Fix a third dictionary-as-array bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpolzin committed Jan 27, 2019
1 parent b45fc73 commit 75ec4f1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
31 changes: 31 additions & 0 deletions Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,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)
}
}
21 changes: 11 additions & 10 deletions Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -878,14 +878,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:
Expand Down Expand Up @@ -928,17 +921,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)
}
}
}

0 comments on commit 75ec4f1

Please sign in to comment.