mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-07-10 12:18:40 -07:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 75ec4f156e | |||
| b45fc73188 | |||
| 3b73dc9989 | |||
| b2c81026f4 |
@@ -148,10 +148,12 @@ extension Entity: OpenAPIEncodedNodeType where Description.Attributes: Sampleabl
|
||||
|
||||
// TODO: metadata, links
|
||||
|
||||
let idNode = JSONNode.string(.init(format: .generic,
|
||||
required: true),
|
||||
.init())
|
||||
let idProperty = ("id", idNode)
|
||||
let idNode: JSONNode? = Id.RawType.self != Unidentified.self
|
||||
? JSONNode.string(.init(format: .generic,
|
||||
required: true),
|
||||
.init())
|
||||
: nil
|
||||
let idProperty = idNode.map { ("id", $0) }
|
||||
|
||||
let typeNode = JSONNode.string(.init(format: .generic,
|
||||
required: true,
|
||||
|
||||
@@ -276,7 +276,13 @@ extension OpenAPIPathItem.PathProperties.Operation: Encodable {
|
||||
|
||||
try container.encode(parameters, forKey: .parameters)
|
||||
|
||||
try container.encode(responses, forKey: .responses)
|
||||
// Hack to work around Dictionary encoding
|
||||
// itself as an array in this case:
|
||||
let stringKeyedDict = Dictionary(
|
||||
responses.map { ($0.key.rawValue, $0.value) },
|
||||
uniquingKeysWith: { $1 }
|
||||
)
|
||||
try container.encode(stringKeyedDict, forKey: .responses)
|
||||
|
||||
try container.encode(deprecated, forKey: .deprecated)
|
||||
}
|
||||
@@ -346,6 +352,29 @@ extension OpenAPIPathItem.PathProperties: Encodable {
|
||||
}
|
||||
}
|
||||
|
||||
extension OpenAPIResponse: Encodable {
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case description
|
||||
case headers
|
||||
case content
|
||||
case links
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
extension OpenAPIPathItem: Encodable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
@@ -359,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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -787,7 +787,7 @@ public enum OpenAPIPathItem: Equatable {
|
||||
}
|
||||
}
|
||||
|
||||
public struct OpenAPIResponse: Encodable, Equatable {
|
||||
public struct OpenAPIResponse: Equatable {
|
||||
public let description: String
|
||||
// public let headers:
|
||||
public let content: ContentMap
|
||||
@@ -801,9 +801,29 @@ public struct OpenAPIResponse: Encodable, Equatable {
|
||||
|
||||
public typealias ContentMap = [ContentType: Content]
|
||||
|
||||
public enum Code: Equatable, Hashable {
|
||||
public enum Code: RawRepresentable, Equatable, Hashable {
|
||||
public typealias RawValue = String
|
||||
|
||||
case `default`
|
||||
case status(code: Int)
|
||||
|
||||
public var rawValue: String {
|
||||
switch self {
|
||||
case .default:
|
||||
return "default"
|
||||
|
||||
case .status(code: let code):
|
||||
return String(code)
|
||||
}
|
||||
}
|
||||
|
||||
public init?(rawValue: String) {
|
||||
if let val = Int(rawValue) {
|
||||
self = .status(code: val)
|
||||
} else {
|
||||
self = .default
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum ContentType: String, Encodable, Equatable, Hashable {
|
||||
@@ -858,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:
|
||||
@@ -908,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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,31 @@ class JSONAPIEntityOpenAPITests: XCTestCase {
|
||||
.init()))
|
||||
}
|
||||
|
||||
func test_UnidentifiedEmptyEntity() {
|
||||
let node = try! UnidentifiedTestType1.openAPINode(using: JSONEncoder())
|
||||
|
||||
XCTAssertTrue(node.required)
|
||||
XCTAssertEqual(node.jsonTypeFormat, .object(.generic))
|
||||
|
||||
guard case let .object(contextA, objectContext1) = node else {
|
||||
XCTFail("Expected Object node")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(contextA, .init(format: .generic,
|
||||
required: true,
|
||||
nullable: false,
|
||||
allowedValues: nil))
|
||||
|
||||
XCTAssertEqual(objectContext1.minProperties, 1)
|
||||
XCTAssertEqual(Set(objectContext1.requiredProperties), Set(["type"]))
|
||||
XCTAssertEqual(Set(objectContext1.properties.keys), Set(["type"]))
|
||||
XCTAssertEqual(objectContext1.properties["type"], .string(.init(format: .generic,
|
||||
required: true,
|
||||
allowedValues: [.init(TestType1.jsonType)]),
|
||||
.init()))
|
||||
}
|
||||
|
||||
func test_AttributesEntity() {
|
||||
|
||||
let dateFormatter = DateFormatter()
|
||||
@@ -272,6 +297,7 @@ extension JSONAPIEntityOpenAPITests {
|
||||
}
|
||||
|
||||
typealias TestType1 = BasicEntity<TestType1Description>
|
||||
typealias UnidentifiedTestType1 = JSONAPI.Entity<TestType1Description, NoMetadata, NoLinks, Unidentified>
|
||||
|
||||
enum TestType2Description: EntityDescription {
|
||||
public static var jsonType: String { return "test2" }
|
||||
|
||||
Reference in New Issue
Block a user