Files
JSONAPI/Sources/JSONAPIOpenAPI/JSONAPIOpenAPITypes.swift
T

82 lines
2.3 KiB
Swift
Raw Normal View History

//
// JSONAPIOpenAPITypes.swift
// JSONAPIOpenAPI
//
// Created by Mathew Polzin on 1/13/19.
//
import JSONAPI
2019-01-14 23:14:25 -08:00
private protocol _Optional {}
extension Optional: _Optional {}
extension Attribute: OpenAPINodeType where RawValue: OpenAPINodeType {
static public var openAPINode: JSONNode {
2019-01-14 23:14:25 -08:00
// If the RawValue is not required, we actually consider it
// nullable. To be not required is for the Attribute itself
// to be optional.
if !RawValue.openAPINode.required {
return RawValue.openAPINode.requiredNode().nullableNode()
}
return RawValue.openAPINode
}
}
extension TransformedAttribute: OpenAPINodeType where RawValue: OpenAPINodeType {
static public var openAPINode: JSONNode {
2019-01-14 23:14:25 -08:00
// If the RawValue is not required, we actually consider it
// nullable. To be not required is for the Attribute itself
// to be optional.
if !RawValue.openAPINode.required {
return RawValue.openAPINode.requiredNode().nullableNode()
}
return RawValue.openAPINode
}
}
extension ToOneRelationship: OpenAPINodeType {
// TODO: const for json `type`
2019-01-14 23:29:49 -08:00
// TODO: metadata & links
static public var openAPINode: JSONNode {
let nullable = Identifiable.self is _Optional.Type
return .object(.init(format: .generic,
required: true),
.init(properties: [
"data": .object(.init(format: .generic,
required: true,
nullable: nullable),
.init(properties: [
"id": .string(.init(format: .generic,
required: true),
.init()),
"type": .string(.init(format: .generic,
required: true),
.init())
]))
]))
}
}
extension ToManyRelationship: OpenAPINodeType {
// TODO: const for json `type`
2019-01-14 23:29:49 -08:00
// TODO: metadata & links
static public var openAPINode: JSONNode {
return .object(.init(format: .generic,
required: true),
.init(properties: [
"data": .array(.init(format: .generic,
required: true),
.init(items: .object(.init(format: .generic,
required: true),
.init(properties: [
"id": .string(.init(format: .generic,
required: true),
.init()),
"type": .string(.init(format: .generic,
required: true),
.init())
]))))
]))
}
}