mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-07-10 12:18:40 -07:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8d057b4398 | |||
| c8421cdd58 | |||
| cde10a8491 | |||
| ad05d3908a | |||
| 23b2b2e04f | |||
| 2988503d7d | |||
| 5ea83b07c1 | |||
| 2b59f54067 | |||
| 58a7c82436 | |||
| dc30cb3b9e | |||
| 7045373708 | |||
| 952fe8ba7e | |||
| 951c04ad44 | |||
| 57df6b147e | |||
| a88844fe5e | |||
| d1cf19f9fe | |||
| 59835fbe11 |
@@ -1,5 +1,6 @@
|
||||
import Foundation
|
||||
import JSONAPI
|
||||
import Poly
|
||||
|
||||
// MARK: - Preamble (setup)
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import Foundation
|
||||
import JSONAPI
|
||||
import JSONAPIOpenAPI
|
||||
import Poly
|
||||
|
||||
// print Entity Schema
|
||||
let encoder = JSONEncoder()
|
||||
@@ -15,16 +16,31 @@ print("====")
|
||||
print(personSchemaData.map { String(data: $0, encoding: .utf8)! } ?? "Schema Construction Failed")
|
||||
print("====")
|
||||
|
||||
let dogDocumentSchemaData = try? encoder.encode(SingleDogDocument.openAPINodeWithExample())
|
||||
let dogDocumentSchemaData = try? encoder.encode(SingleDogDocument.openAPINodeWithExample(using: encoder))
|
||||
|
||||
print("Dog Document Schema")
|
||||
print("====")
|
||||
print(dogDocumentSchemaData.map { String(data: $0, encoding: .utf8)! } ?? "Schema Construction Failed")
|
||||
print("====")
|
||||
|
||||
let batchPersonSchemaData = try? encoder.encode(BatchPeopleDocument.openAPINodeWithExample())
|
||||
let batchPersonSchemaData = try? encoder.encode(BatchPeopleDocument.openAPINodeWithExample(using: encoder))
|
||||
|
||||
print("Batch Person Document Schema")
|
||||
print("====")
|
||||
print(batchPersonSchemaData.map { String(data: $0, encoding: .utf8)! } ?? "Schema Construction Failed")
|
||||
print("====")
|
||||
|
||||
let tmp: [String: OpenAPIComponents.SchemasDict.RefType] = [
|
||||
"BatchPerson": try! BatchPeopleDocument.openAPINodeWithExample()
|
||||
]
|
||||
|
||||
let components = OpenAPIComponents(schemas: tmp)
|
||||
|
||||
let batchPeopleRef = JSONReference(type: \OpenAPIComponents.schemas, selector: "BatchPerson")
|
||||
|
||||
let tmp2 = JSONNode.reference(batchPeopleRef)
|
||||
|
||||
print("====")
|
||||
print("====")
|
||||
//print(String(data: try! encoder.encode(components), encoding: .utf8)!)
|
||||
print(String(data: try! encoder.encode(tmp2), encoding: .utf8)!)
|
||||
|
||||
+2
-2
@@ -23,9 +23,9 @@
|
||||
"package": "Poly",
|
||||
"repositoryURL": "https://github.com/mattpolzin/Poly.git",
|
||||
"state": {
|
||||
"branch": "master",
|
||||
"branch": null,
|
||||
"revision": "77f45b8963a51c02d71fc4075eba5cff47ff0d07",
|
||||
"version": null
|
||||
"version": "1.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ let package = Package(
|
||||
targets: ["JSONAPIOpenAPI"])
|
||||
],
|
||||
dependencies: [
|
||||
.package(url: "https://github.com/mattpolzin/Poly.git", .branch("master")),
|
||||
.package(url: "https://github.com/mattpolzin/Poly.git", from: "1.0.0"),
|
||||
.package(url: "https://github.com/Flight-School/AnyCodable.git", from: "0.1.0"),
|
||||
.package(url: "https://github.com/typelift/SwiftCheck.git", from: "0.11.0")
|
||||
],
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
//
|
||||
|
||||
import JSONAPI
|
||||
import Foundation
|
||||
|
||||
extension Includes: OpenAPINodeType where I: OpenAPINodeType {
|
||||
public static func openAPINode() throws -> JSONNode {
|
||||
|
||||
@@ -6,11 +6,17 @@
|
||||
//
|
||||
|
||||
import JSONAPI
|
||||
import Foundation
|
||||
import AnyCodable
|
||||
|
||||
private protocol _Optional {}
|
||||
extension Optional: _Optional {}
|
||||
|
||||
private protocol Wrapper {
|
||||
associatedtype Wrapped
|
||||
}
|
||||
extension Optional: Wrapper {}
|
||||
|
||||
extension Attribute: OpenAPINodeType where RawValue: OpenAPINodeType {
|
||||
static public func openAPINode() throws -> JSONNode {
|
||||
// If the RawValue is not required, we actually consider it
|
||||
@@ -48,14 +54,26 @@ extension Attribute: WrappedRawOpenAPIType where RawValue: RawOpenAPINodeType {
|
||||
}
|
||||
|
||||
extension Attribute: AnyJSONCaseIterable where RawValue: CaseIterable, RawValue: Codable {
|
||||
public static var allCases: [AnyCodable] {
|
||||
return (try? allCases(from: Array(RawValue.allCases))) ?? []
|
||||
public static func allCases(using encoder: JSONEncoder) -> [AnyCodable] {
|
||||
return (try? allCases(from: Array(RawValue.allCases), using: encoder)) ?? []
|
||||
}
|
||||
}
|
||||
|
||||
extension Attribute: AnyWrappedJSONCaseIterable where RawValue: AnyJSONCaseIterable {
|
||||
public static var allCases: [AnyCodable] {
|
||||
return RawValue.allCases
|
||||
public static func allCases(using encoder: JSONEncoder) -> [AnyCodable] {
|
||||
return RawValue.allCases(using: encoder)
|
||||
}
|
||||
}
|
||||
|
||||
extension Attribute: GenericOpenAPINodeType where RawValue: GenericOpenAPINodeType {
|
||||
public static func genericOpenAPINode(using encoder: JSONEncoder) throws -> JSONNode {
|
||||
return try RawValue.genericOpenAPINode(using: encoder)
|
||||
}
|
||||
}
|
||||
|
||||
extension Attribute: DateOpenAPINodeType where RawValue: DateOpenAPINodeType {
|
||||
public static func dateOpenAPINodeGuess(using encoder: JSONEncoder) -> JSONNode? {
|
||||
return RawValue.dateOpenAPINodeGuess(using: encoder)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,6 +89,8 @@ extension TransformedAttribute: OpenAPINodeType where RawValue: OpenAPINodeType
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: conform TransformedAttribute to all of the above protocols that Attribute conforms to.
|
||||
|
||||
extension RelationshipType {
|
||||
static func relationshipNode(nullable: Bool, jsonType: String) -> JSONNode {
|
||||
let propertiesDict: [String: JSONNode] = [
|
||||
@@ -121,8 +141,8 @@ extension ToManyRelationship: OpenAPINodeType {
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity: OpenAPINodeType where Description.Attributes: Sampleable, Description.Relationships: Sampleable {
|
||||
public static func openAPINode() throws -> JSONNode {
|
||||
extension Entity: OpenAPIEncodedNodeType where Description.Attributes: Sampleable, Description.Relationships: Sampleable {
|
||||
public static func openAPINode(using encoder: JSONEncoder) throws -> JSONNode {
|
||||
// NOTE: const for json `type` not supported by OpenAPI 3.0
|
||||
// Will use "enum" with one possible value for now.
|
||||
|
||||
@@ -141,13 +161,13 @@ extension Entity: OpenAPINodeType where Description.Attributes: Sampleable, Desc
|
||||
|
||||
let attributesNode: JSONNode? = Description.Attributes.self == NoAttributes.self
|
||||
? nil
|
||||
: try Description.Attributes.genericObjectOpenAPINode()
|
||||
: try Description.Attributes.genericOpenAPINode(using: encoder)
|
||||
|
||||
let attributesProperty = attributesNode.map { ("attributes", $0) }
|
||||
|
||||
let relationshipsNode: JSONNode? = Description.Relationships.self == NoRelationships.self
|
||||
? nil
|
||||
: try Description.Relationships.genericObjectOpenAPINode()
|
||||
: try Description.Relationships.genericOpenAPINode(using: encoder)
|
||||
|
||||
let relationshipsProperty = relationshipsNode.map { ("relationships", $0) }
|
||||
|
||||
@@ -164,26 +184,26 @@ extension Entity: OpenAPINodeType where Description.Attributes: Sampleable, Desc
|
||||
}
|
||||
}
|
||||
|
||||
extension SingleResourceBody: OpenAPINodeType where Entity: OpenAPINodeType {
|
||||
public static func openAPINode() throws -> JSONNode {
|
||||
return try Entity.openAPINode()
|
||||
extension SingleResourceBody: OpenAPIEncodedNodeType where Entity: OpenAPIEncodedNodeType {
|
||||
public static func openAPINode(using encoder: JSONEncoder) throws -> JSONNode {
|
||||
return try Entity.openAPINode(using: encoder)
|
||||
}
|
||||
}
|
||||
|
||||
extension ManyResourceBody: OpenAPINodeType where Entity: OpenAPINodeType {
|
||||
public static func openAPINode() throws -> JSONNode {
|
||||
extension ManyResourceBody: OpenAPIEncodedNodeType where Entity: OpenAPIEncodedNodeType {
|
||||
public static func openAPINode(using encoder: JSONEncoder) throws -> JSONNode {
|
||||
return .array(.init(format: .generic,
|
||||
required: true),
|
||||
.init(items: try Entity.openAPINode()))
|
||||
.init(items: try Entity.openAPINode(using: encoder)))
|
||||
}
|
||||
}
|
||||
|
||||
extension Document: OpenAPINodeType where PrimaryResourceBody: OpenAPINodeType, IncludeType: OpenAPINodeType {
|
||||
public static func openAPINode() throws -> JSONNode {
|
||||
extension Document: OpenAPIEncodedNodeType where PrimaryResourceBody: OpenAPIEncodedNodeType, IncludeType: OpenAPINodeType {
|
||||
public static func openAPINode(using encoder: JSONEncoder) throws -> JSONNode {
|
||||
// TODO: metadata, links, api description, errors
|
||||
// TODO: represent data and errors as the two distinct possible outcomes
|
||||
|
||||
let primaryDataNode: JSONNode? = try PrimaryResourceBody.openAPINode()
|
||||
let primaryDataNode: JSONNode? = try PrimaryResourceBody.openAPINode(using: encoder)
|
||||
|
||||
let primaryDataProperty = primaryDataNode.map { ("data", $0) }
|
||||
|
||||
@@ -191,7 +211,7 @@ extension Document: OpenAPINodeType where PrimaryResourceBody: OpenAPINodeType,
|
||||
do {
|
||||
includeNode = try Includes<Include>.openAPINode()
|
||||
} catch let err as OpenAPITypeError {
|
||||
guard err == .invalidNode else {
|
||||
guard case .invalidNode = err else {
|
||||
throw err
|
||||
}
|
||||
includeNode = nil
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// Date+OpenAPI.swift
|
||||
// JSONAPIOpenAPI
|
||||
//
|
||||
// Created by Mathew Polzin on 1/24/19.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension Date: DateOpenAPINodeType {
|
||||
public static func dateOpenAPINodeGuess(using encoder: JSONEncoder) -> JSONNode? {
|
||||
|
||||
switch encoder.dateEncodingStrategy {
|
||||
case .deferredToDate, .custom:
|
||||
// I don't know if we can say anything about this case without
|
||||
// encoding the Date and looking at it, which is what `primitiveGuess()`
|
||||
// does.
|
||||
return nil
|
||||
|
||||
case .secondsSince1970,
|
||||
.millisecondsSince1970:
|
||||
return .number(.init(format: .double,
|
||||
required: true),
|
||||
.init())
|
||||
|
||||
case .iso8601:
|
||||
return .string(.init(format: .dateTime,
|
||||
required: true),
|
||||
.init())
|
||||
|
||||
case .formatted(let formatter):
|
||||
let hasTime = formatter.timeStyle != .none
|
||||
let format: JSONTypeFormat.StringFormat = hasTime ? .dateTime : .date
|
||||
|
||||
return .string(.init(format: format,
|
||||
required: true),
|
||||
.init())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -155,6 +155,7 @@ extension JSONNode: Encodable {
|
||||
case oneOf
|
||||
case anyOf
|
||||
case not
|
||||
case reference = "$ref"
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
@@ -189,6 +190,172 @@ extension JSONNode: Encodable {
|
||||
var container = encoder.container(keyedBy: SubschemaCodingKeys.self)
|
||||
|
||||
try container.encode(node, forKey: .not)
|
||||
|
||||
case .reference(let reference):
|
||||
var container = encoder.container(keyedBy: SubschemaCodingKeys.self)
|
||||
|
||||
try container.encode(reference, forKey: .reference)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension JSONReference: Encodable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
|
||||
let referenceString: String = {
|
||||
switch self {
|
||||
case .file(let reference):
|
||||
return reference
|
||||
case .node(let reference):
|
||||
return "#/\(Root.refName)/\(reference.refName)/\(reference.selector)"
|
||||
}
|
||||
}()
|
||||
|
||||
try container.encode(referenceString)
|
||||
}
|
||||
}
|
||||
|
||||
extension RefDict: Encodable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
|
||||
try container.encode(dict)
|
||||
}
|
||||
}
|
||||
|
||||
extension OpenAPIResponse.Code: Encodable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
|
||||
let string: String
|
||||
switch self {
|
||||
case .`default`:
|
||||
string = "default"
|
||||
|
||||
case .status(code: let code):
|
||||
string = String(code)
|
||||
}
|
||||
|
||||
try container.encode(string)
|
||||
}
|
||||
}
|
||||
|
||||
extension OpenAPIPathItem.PathProperties.Operation: Encodable {
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case tags
|
||||
case summary
|
||||
case description
|
||||
case externalDocs
|
||||
case operationId
|
||||
case parameters
|
||||
case requestBody
|
||||
case responses
|
||||
case callbacks
|
||||
case deprecated
|
||||
case security
|
||||
case servers
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
|
||||
if tags != nil {
|
||||
try container.encode(tags, forKey: .tags)
|
||||
}
|
||||
|
||||
if summary != nil {
|
||||
try container.encode(summary, forKey: .summary)
|
||||
}
|
||||
|
||||
if description != nil {
|
||||
try container.encode(description, forKey: .description)
|
||||
}
|
||||
|
||||
try container.encode(operationId, forKey: .operationId)
|
||||
|
||||
try container.encode(parameters, forKey: .parameters)
|
||||
|
||||
try container.encode(responses, forKey: .responses)
|
||||
|
||||
try container.encode(deprecated, forKey: .deprecated)
|
||||
}
|
||||
}
|
||||
|
||||
extension OpenAPIPathItem.PathProperties: Encodable {
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case summary
|
||||
case description
|
||||
case servers
|
||||
case parameters
|
||||
|
||||
case get
|
||||
case put
|
||||
case post
|
||||
case delete
|
||||
case options
|
||||
case head
|
||||
case patch
|
||||
case trace
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
|
||||
if summary != nil {
|
||||
try container.encode(summary, forKey: .summary)
|
||||
}
|
||||
|
||||
if description != nil {
|
||||
try container.encode(description, forKey: .description)
|
||||
}
|
||||
|
||||
try container.encode(parameters, forKey: .parameters)
|
||||
|
||||
if get != nil {
|
||||
try container.encode(get, forKey: .get)
|
||||
}
|
||||
|
||||
if put != nil {
|
||||
try container.encode(put, forKey: .put)
|
||||
}
|
||||
|
||||
if post != nil {
|
||||
try container.encode(post, forKey: .post)
|
||||
}
|
||||
|
||||
if delete != nil {
|
||||
try container.encode(delete, forKey: .delete)
|
||||
}
|
||||
|
||||
if options != nil {
|
||||
try container.encode(options, forKey: .options)
|
||||
}
|
||||
|
||||
if head != nil {
|
||||
try container.encode(head, forKey: .head)
|
||||
}
|
||||
|
||||
if patch != nil {
|
||||
try container.encode(patch, forKey: .patch)
|
||||
}
|
||||
|
||||
if trace != nil {
|
||||
try container.encode(trace, forKey: .trace)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension OpenAPIPathItem: Encodable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
|
||||
switch self {
|
||||
case .reference(let reference):
|
||||
try container.encode(reference)
|
||||
|
||||
case .operations(let operations):
|
||||
try container.encode(operations)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -6,6 +6,7 @@
|
||||
//
|
||||
|
||||
import AnyCodable
|
||||
import Foundation
|
||||
|
||||
/**
|
||||
|
||||
@@ -56,8 +57,8 @@ extension Optional: DoubleWrappedRawOpenAPIType where Wrapped: WrappedRawOpenAPI
|
||||
}
|
||||
|
||||
extension Optional: AnyJSONCaseIterable where Wrapped: CaseIterable, Wrapped: Codable {
|
||||
public static var allCases: [AnyCodable] {
|
||||
return (try? allCases(from: Array(Wrapped.allCases))) ?? []
|
||||
public static func allCases(using encoder: JSONEncoder) -> [AnyCodable] {
|
||||
return (try? allCases(from: Array(Wrapped.allCases), using: encoder)) ?? []
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
//
|
||||
// Sampleable.swift
|
||||
// JSONAPIOpenAPI
|
||||
//
|
||||
// Created by Mathew Polzin on 1/15/19.
|
||||
//
|
||||
|
||||
import JSONAPI
|
||||
import AnyCodable
|
||||
|
||||
/// A Sampleable type can provide a sample value.
|
||||
/// This is useful for reflection.
|
||||
public protocol Sampleable {
|
||||
/// Get a sample value of type Self. This can be the
|
||||
/// same value every time, or it can be an arbitrarily random
|
||||
/// value each time.
|
||||
static var sample: Self { get }
|
||||
|
||||
/// Get an example of success, if that is meaningful and
|
||||
/// available. If not, will be nil.
|
||||
static var successSample: Self? { get }
|
||||
|
||||
/// Get an example of failure, if that is meaningful and
|
||||
/// available. If not, will be nil.
|
||||
static var failureSample: Self? { get }
|
||||
}
|
||||
|
||||
public extension Sampleable {
|
||||
public static var successSample: Self? { return nil }
|
||||
|
||||
public static var failureSample: Self? { return nil }
|
||||
}
|
||||
|
||||
extension Sampleable {
|
||||
public static func genericObjectOpenAPINode() throws -> JSONNode {
|
||||
let mirror = Mirror(reflecting: Self.sample)
|
||||
let properties: [(String, JSONNode)] = try mirror.children.compactMap { child in
|
||||
|
||||
// see if we can enumerate the possible values
|
||||
let maybeAllCases: [AnyCodable]? = {
|
||||
switch type(of: child.value) {
|
||||
case let valType as AnyJSONCaseIterable.Type:
|
||||
return valType.allCases
|
||||
case let valType as AnyWrappedJSONCaseIterable.Type:
|
||||
return valType.allCases
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}()
|
||||
|
||||
// try to snag an OpenAPI Node
|
||||
let maybeOpenAPINode: JSONNode? = try {
|
||||
switch type(of: child.value) {
|
||||
case let valType as OpenAPINodeType.Type:
|
||||
return try valType.openAPINode()
|
||||
|
||||
case let valType as RawOpenAPINodeType.Type:
|
||||
return try valType.rawOpenAPINode()
|
||||
|
||||
case let valType as WrappedRawOpenAPIType.Type:
|
||||
return try valType.wrappedOpenAPINode()
|
||||
|
||||
case let valType as DoubleWrappedRawOpenAPIType.Type:
|
||||
return try valType.wrappedOpenAPINode()
|
||||
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}()
|
||||
|
||||
// put it all together
|
||||
let newNode: JSONNode?
|
||||
if let allCases = maybeAllCases,
|
||||
let openAPINode = maybeOpenAPINode {
|
||||
newNode = try openAPINode.with(allowedValues: allCases)
|
||||
} else {
|
||||
newNode = maybeOpenAPINode
|
||||
}
|
||||
|
||||
return zip(child.label, newNode) { ($0, $1) }
|
||||
}
|
||||
|
||||
// There should not be any duplication of keys since these are
|
||||
// property names, but rather than risk runtime exception, we just
|
||||
// fail to the newer value arbitrarily
|
||||
let propertiesDict = Dictionary(properties) { _, value2 in value2 }
|
||||
|
||||
return .object(.init(format: .generic,
|
||||
required: true),
|
||||
.init(properties: propertiesDict))
|
||||
}
|
||||
}
|
||||
|
||||
extension NoAttributes: Sampleable {
|
||||
public static var sample: NoAttributes {
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
extension NoRelationships: Sampleable {
|
||||
public static var sample: NoRelationships {
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
extension NoMetadata: Sampleable {
|
||||
public static var sample: NoMetadata {
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
extension NoLinks: Sampleable {
|
||||
public static var sample: NoLinks {
|
||||
return .none
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
//
|
||||
// Include+Sampleable.swift
|
||||
// JSONAPIOpenAPI
|
||||
//
|
||||
// Created by Mathew Polzin on 1/23/19.
|
||||
//
|
||||
|
||||
import JSONAPI
|
||||
|
||||
extension Includes: Sampleable where I: Sampleable {
|
||||
public static var sample: Includes<I> {
|
||||
guard I.self != NoIncludes.self else {
|
||||
return .none
|
||||
}
|
||||
|
||||
return .init(values: I.samples)
|
||||
}
|
||||
}
|
||||
|
||||
extension NoIncludes: Sampleable {
|
||||
public static var sample: NoIncludes {
|
||||
return NoIncludes()
|
||||
}
|
||||
}
|
||||
|
||||
extension Include1: Sampleable where A: Sampleable {
|
||||
public static var sample: Include1<A> {
|
||||
return .init(A.sample)
|
||||
}
|
||||
|
||||
public static var samples: [Include1<A>] {
|
||||
return A.samples.map(Include1<A>.init)
|
||||
}
|
||||
}
|
||||
|
||||
extension Include2: Sampleable where A: Sampleable, B: Sampleable {
|
||||
public static var sample: Include2<A, B> {
|
||||
let randomChoice = Int.random(in: 0..<samples.count)
|
||||
|
||||
return samples[randomChoice]
|
||||
}
|
||||
|
||||
public static var samples: [Include2<A, B>] {
|
||||
return A.samples.map(Include2<A, B>.init)
|
||||
+ B.samples.map(Include2<A, B>.init)
|
||||
}
|
||||
}
|
||||
|
||||
extension Include3: Sampleable where A: Sampleable, B: Sampleable, C: Sampleable {
|
||||
public static var sample: Include3<A, B, C> {
|
||||
let randomChoice = Int.random(in: 0..<samples.count)
|
||||
|
||||
return samples[randomChoice]
|
||||
}
|
||||
|
||||
public static var samples: [Include3<A, B, C>] {
|
||||
return A.samples.map(Include3<A, B, C>.init)
|
||||
+ B.samples.map(Include3<A, B, C>.init)
|
||||
+ C.samples.map(Include3<A, B, C>.init)
|
||||
}
|
||||
}
|
||||
|
||||
extension Include4: Sampleable where A: Sampleable, B: Sampleable, C: Sampleable, D: Sampleable {
|
||||
public static var sample: Include4<A, B, C, D> {
|
||||
let randomChoice = Int.random(in: 0..<samples.count)
|
||||
|
||||
return samples[randomChoice]
|
||||
}
|
||||
|
||||
public static var samples: [Include4<A, B, C, D>] {
|
||||
return A.samples.map(Include4<A, B, C, D>.init)
|
||||
+ B.samples.map(Include4<A, B, C, D>.init)
|
||||
+ C.samples.map(Include4<A, B, C, D>.init)
|
||||
+ D.samples.map(Include4<A, B, C, D>.init)
|
||||
}
|
||||
}
|
||||
|
||||
extension Include5: Sampleable where A: Sampleable, B: Sampleable, C: Sampleable, D: Sampleable, E: Sampleable {
|
||||
public static var sample: Include5<A, B, C, D, E> {
|
||||
let randomChoice = Int.random(in: 0..<samples.count)
|
||||
|
||||
return samples[randomChoice]
|
||||
}
|
||||
|
||||
public static var samples: [Include5<A, B, C, D, E>] {
|
||||
let set1: [Include5<A, B, C, D, E>] = A.samples.map(Include5<A, B, C, D, E>.init)
|
||||
+ B.samples.map(Include5<A, B, C, D, E>.init)
|
||||
+ C.samples.map(Include5<A, B, C, D, E>.init)
|
||||
|
||||
let set2: [Include5<A, B, C, D, E>] = D.samples.map(Include5<A, B, C, D, E>.init)
|
||||
+ E.samples.map(Include5<A, B, C, D, E>.init)
|
||||
|
||||
return set1 + set2
|
||||
}
|
||||
}
|
||||
|
||||
extension Include6: Sampleable where A: Sampleable, B: Sampleable, C: Sampleable, D: Sampleable, E: Sampleable, F: Sampleable {
|
||||
public static var sample: Include6<A, B, C, D, E, F> {
|
||||
let randomChoice = Int.random(in: 0..<samples.count)
|
||||
|
||||
return samples[randomChoice]
|
||||
}
|
||||
|
||||
public static var samples: [Include6<A, B, C, D, E, F>] {
|
||||
let set1: [Include6<A, B, C, D, E, F>] = A.samples.map(Include6<A, B, C, D, E, F>.init)
|
||||
+ B.samples.map(Include6<A, B, C, D, E, F>.init)
|
||||
+ C.samples.map(Include6<A, B, C, D, E, F>.init)
|
||||
|
||||
let set2: [Include6<A, B, C, D, E, F>] = D.samples.map(Include6<A, B, C, D, E, F>.init)
|
||||
+ E.samples.map(Include6<A, B, C, D, E, F>.init)
|
||||
+ F.samples.map(Include6<A, B, C, D, E, F>.init)
|
||||
|
||||
return set1 + set2
|
||||
}
|
||||
}
|
||||
|
||||
extension Include7: Sampleable where A: Sampleable, B: Sampleable, C: Sampleable, D: Sampleable, E: Sampleable, F: Sampleable, G: Sampleable {
|
||||
public static var sample: Include7<A, B, C, D, E, F, G> {
|
||||
let randomChoice = Int.random(in: 0..<samples.count)
|
||||
|
||||
return samples[randomChoice]
|
||||
}
|
||||
|
||||
public static var samples: [Include7<A, B, C, D, E, F, G>] {
|
||||
let set1: [Include7<A, B, C, D, E, F, G>] = A.samples.map(Include7<A, B, C, D, E, F, G>.init)
|
||||
+ B.samples.map(Include7<A, B, C, D, E, F, G>.init)
|
||||
+ C.samples.map(Include7<A, B, C, D, E, F, G>.init)
|
||||
|
||||
let set2: [Include7<A, B, C, D, E, F, G>] = D.samples.map(Include7<A, B, C, D, E, F, G>.init)
|
||||
+ E.samples.map(Include7<A, B, C, D, E, F, G>.init)
|
||||
+ F.samples.map(Include7<A, B, C, D, E, F, G>.init)
|
||||
|
||||
let set3: [Include7<A, B, C, D, E, F, G>] = G.samples.map(Include7<A, B, C, D, E, F, G>.init)
|
||||
|
||||
return set1 + set2 + set3
|
||||
}
|
||||
}
|
||||
|
||||
extension Include8: Sampleable where A: Sampleable, B: Sampleable, C: Sampleable, D: Sampleable, E: Sampleable, F: Sampleable, G: Sampleable, H: Sampleable {
|
||||
public static var sample: Include8<A, B, C, D, E, F, G, H> {
|
||||
let randomChoice = Int.random(in: 0..<samples.count)
|
||||
|
||||
return samples[randomChoice]
|
||||
}
|
||||
|
||||
public static var samples: [Include8<A, B, C, D, E, F, G, H>] {
|
||||
let set1: [Include8<A, B, C, D, E, F, G, H>] = A.samples.map(Include8<A, B, C, D, E, F, G, H>.init)
|
||||
+ B.samples.map(Include8<A, B, C, D, E, F, G, H>.init)
|
||||
+ C.samples.map(Include8<A, B, C, D, E, F, G, H>.init)
|
||||
|
||||
let set2: [Include8<A, B, C, D, E, F, G, H>] = D.samples.map(Include8<A, B, C, D, E, F, G, H>.init)
|
||||
+ E.samples.map(Include8<A, B, C, D, E, F, G, H>.init)
|
||||
+ F.samples.map(Include8<A, B, C, D, E, F, G, H>.init)
|
||||
|
||||
let set3: [Include8<A, B, C, D, E, F, G, H>] = G.samples.map(Include8<A, B, C, D, E, F, G, H>.init)
|
||||
+ H.samples.map(Include8<A, B, C, D, E, F, G, H>.init)
|
||||
|
||||
return set1 + set2 + set3
|
||||
}
|
||||
}
|
||||
|
||||
extension Include9: Sampleable where A: Sampleable, B: Sampleable, C: Sampleable, D: Sampleable, E: Sampleable, F: Sampleable, G: Sampleable, H: Sampleable, I: Sampleable {
|
||||
public static var sample: Include9<A, B, C, D, E, F, G, H, I> {
|
||||
let randomChoice = Int.random(in: 0..<samples.count)
|
||||
|
||||
return samples[randomChoice]
|
||||
}
|
||||
|
||||
public static var samples: [Include9<A, B, C, D, E, F, G, H, I>] {
|
||||
let set1: [Include9<A, B, C, D, E, F, G, H, I>] = A.samples.map(Include9<A, B, C, D, E, F, G, H, I>.init)
|
||||
+ B.samples.map(Include9<A, B, C, D, E, F, G, H, I>.init)
|
||||
+ C.samples.map(Include9<A, B, C, D, E, F, G, H, I>.init)
|
||||
|
||||
let set2: [Include9<A, B, C, D, E, F, G, H, I>] = D.samples.map(Include9<A, B, C, D, E, F, G, H, I>.init)
|
||||
+ E.samples.map(Include9<A, B, C, D, E, F, G, H, I>.init)
|
||||
+ F.samples.map(Include9<A, B, C, D, E, F, G, H, I>.init)
|
||||
|
||||
let set3: [Include9<A, B, C, D, E, F, G, H, I>] = G.samples.map(Include9<A, B, C, D, E, F, G, H, I>.init)
|
||||
+ H.samples.map(Include9<A, B, C, D, E, F, G, H, I>.init)
|
||||
+ I.samples.map(Include9<A, B, C, D, E, F, G, H, I>.init)
|
||||
|
||||
return set1 + set2 + set3
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// JSONAPI+Sampleable.swift
|
||||
// JSONAPIOpenAPI
|
||||
//
|
||||
// Created by Mathew Polzin on 1/24/19.
|
||||
//
|
||||
|
||||
import JSONAPI
|
||||
|
||||
extension NoAttributes: Sampleable {
|
||||
public static var sample: NoAttributes {
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
extension NoRelationships: Sampleable {
|
||||
public static var sample: NoRelationships {
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
extension NoMetadata: Sampleable {
|
||||
public static var sample: NoMetadata {
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
extension NoLinks: Sampleable {
|
||||
public static var sample: NoLinks {
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
extension NoAPIDescription: Sampleable {
|
||||
public static var sample: NoAPIDescription {
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
extension UnknownJSONAPIError: Sampleable {
|
||||
public static var sample: UnknownJSONAPIError {
|
||||
return .unknownError
|
||||
}
|
||||
}
|
||||
|
||||
extension Unidentified: Sampleable {
|
||||
public static var sample: Unidentified {
|
||||
return Unidentified()
|
||||
}
|
||||
}
|
||||
|
||||
extension Attribute: Sampleable where RawValue: Sampleable {
|
||||
public static var sample: Attribute<RawValue> {
|
||||
return .init(value: RawValue.sample)
|
||||
}
|
||||
}
|
||||
|
||||
extension SingleResourceBody: Sampleable where Entity: Sampleable {
|
||||
public static var sample: SingleResourceBody<Entity> {
|
||||
return .init(entity: Entity.sample)
|
||||
}
|
||||
}
|
||||
|
||||
extension ManyResourceBody: Sampleable where Entity: Sampleable {
|
||||
public static var sample: ManyResourceBody<Entity> {
|
||||
return .init(entities: Entity.samples)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
//
|
||||
// Sampleable+OpenAPI.swift
|
||||
// JSONAPIOpenAPI
|
||||
//
|
||||
// Created by Mathew Polzin on 1/24/19.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import AnyCodable
|
||||
|
||||
public typealias SampleableOpenAPIType = Sampleable & GenericOpenAPINodeType
|
||||
|
||||
extension Sampleable where Self: Encodable {
|
||||
public static func genericOpenAPINode(using encoder: JSONEncoder) throws -> JSONNode {
|
||||
|
||||
// short circuit for dates
|
||||
if let dateType = self as? Date.Type,
|
||||
let node = try dateType.dateOpenAPINodeGuess(using: encoder) ?? primitiveGuess(using: encoder) {
|
||||
return node
|
||||
}
|
||||
|
||||
let mirror = Mirror(reflecting: Self.sample)
|
||||
let properties: [(String, JSONNode)] = try mirror.children.compactMap { child in
|
||||
|
||||
// see if we can enumerate the possible values
|
||||
let maybeAllCases: [AnyCodable]? = {
|
||||
switch type(of: child.value) {
|
||||
case let valType as AnyJSONCaseIterable.Type:
|
||||
return valType.allCases(using: encoder)
|
||||
case let valType as AnyWrappedJSONCaseIterable.Type:
|
||||
return valType.allCases(using: encoder)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}()
|
||||
|
||||
// try to snag an OpenAPI Node
|
||||
let maybeOpenAPINode: JSONNode? = try {
|
||||
switch type(of: child.value) {
|
||||
case let valType as OpenAPINodeType.Type:
|
||||
return try valType.openAPINode()
|
||||
|
||||
case let valType as RawOpenAPINodeType.Type:
|
||||
return try valType.rawOpenAPINode()
|
||||
|
||||
case let valType as WrappedRawOpenAPIType.Type:
|
||||
return try valType.wrappedOpenAPINode()
|
||||
|
||||
case let valType as DoubleWrappedRawOpenAPIType.Type:
|
||||
return try valType.wrappedOpenAPINode()
|
||||
|
||||
case let valType as GenericOpenAPINodeType.Type:
|
||||
return try valType.genericOpenAPINode(using: encoder)
|
||||
|
||||
case let valType as DateOpenAPINodeType.Type:
|
||||
return valType.dateOpenAPINodeGuess(using: encoder)
|
||||
|
||||
default:
|
||||
throw OpenAPITypeError.unknownNodeType(self)
|
||||
// return nil
|
||||
}
|
||||
}()
|
||||
|
||||
// put it all together
|
||||
let newNode: JSONNode?
|
||||
if let allCases = maybeAllCases,
|
||||
let openAPINode = maybeOpenAPINode {
|
||||
newNode = try openAPINode.with(allowedValues: allCases)
|
||||
} else {
|
||||
newNode = maybeOpenAPINode
|
||||
}
|
||||
|
||||
return zip(child.label, newNode) { ($0, $1) }
|
||||
}
|
||||
|
||||
// if there are no properties, let's see if we are dealing
|
||||
// with a primitive.
|
||||
if properties.count == 0,
|
||||
let primitive = try primitiveGuess(using: encoder) {
|
||||
return primitive
|
||||
}
|
||||
|
||||
// There should not be any duplication of keys since these are
|
||||
// property names, but rather than risk runtime exception, we just
|
||||
// fail to the newer value arbitrarily
|
||||
let propertiesDict = Dictionary(properties) { _, value2 in value2 }
|
||||
|
||||
return .object(.init(format: .generic,
|
||||
required: true),
|
||||
.init(properties: propertiesDict))
|
||||
}
|
||||
|
||||
private static func primitiveGuess(using encoder: JSONEncoder) throws -> JSONNode? {
|
||||
|
||||
let data = try encoder.encode(PrimitiveWrapper(primitive: Self.sample))
|
||||
let wrappedValue = try JSONSerialization.jsonObject(with: data, options: [.allowFragments])
|
||||
|
||||
guard let wrapperDict = wrappedValue as? [String: Any],
|
||||
wrapperDict.contains(where: { $0.key == "primitive" }) else {
|
||||
throw OpenAPICodableError.primitiveGuessFailed
|
||||
}
|
||||
|
||||
let value = (wrappedValue as! [String: Any])["primitive"]!
|
||||
|
||||
return try {
|
||||
switch type(of: value) {
|
||||
case let valType as OpenAPINodeType.Type:
|
||||
return try valType.openAPINode()
|
||||
|
||||
case let valType as RawOpenAPINodeType.Type:
|
||||
return try valType.rawOpenAPINode()
|
||||
|
||||
case let valType as WrappedRawOpenAPIType.Type:
|
||||
return try valType.wrappedOpenAPINode()
|
||||
|
||||
case let valType as DoubleWrappedRawOpenAPIType.Type:
|
||||
return try valType.wrappedOpenAPINode()
|
||||
|
||||
case let valType as GenericOpenAPINodeType.Type:
|
||||
return try valType.genericOpenAPINode(using: encoder)
|
||||
|
||||
case let valType as DateOpenAPINodeType.Type:
|
||||
return valType.dateOpenAPINodeGuess(using: encoder)
|
||||
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}() ?? {
|
||||
switch value {
|
||||
case is String:
|
||||
return .string(.init(format: .generic,
|
||||
required: true),
|
||||
.init())
|
||||
|
||||
case is Int:
|
||||
return .integer(.init(format: .generic,
|
||||
required: true),
|
||||
.init())
|
||||
|
||||
case is Double:
|
||||
return .number(.init(format: .double,
|
||||
required: true),
|
||||
.init())
|
||||
|
||||
case is Bool:
|
||||
return .boolean(.init(format: .generic,
|
||||
required: true))
|
||||
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
// The following wrapper is only needed because JSONEncoder cannot yet encode
|
||||
// JSON fragments. It is a very unfortunate limitation that requires silly
|
||||
// workarounds in edge cases like this.
|
||||
private struct PrimitiveWrapper<Wrapped: Encodable>: Encodable {
|
||||
let primitive: Wrapped
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
//
|
||||
// Sampleable.swift
|
||||
// JSONAPIOpenAPI
|
||||
//
|
||||
// Created by Mathew Polzin on 1/15/19.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// A Sampleable type can provide a sample value.
|
||||
/// This is useful for reflection.
|
||||
public protocol Sampleable {
|
||||
/// Get a sample value of type Self. This can be the
|
||||
/// same value every time, or it can be an arbitrarily random
|
||||
/// value each time.
|
||||
static var sample: Self { get }
|
||||
|
||||
/// Get an example of success, if that is meaningful and
|
||||
/// available. If not, will be nil.
|
||||
///
|
||||
/// The default implementation returns `nil`.
|
||||
static var successSample: Self? { get }
|
||||
|
||||
/// Get an example of failure, if that is meaningful and
|
||||
/// available. If not, will be nil.
|
||||
///
|
||||
/// The default implementation returns `nil`.
|
||||
static var failureSample: Self? { get }
|
||||
|
||||
/// An array of samples of this Type.
|
||||
///
|
||||
/// The default implementation returns
|
||||
/// an array with just the result of
|
||||
/// `Self.sample` in it.
|
||||
static var samples: [Self] { get }
|
||||
}
|
||||
|
||||
public extension Sampleable {
|
||||
// default implementation:
|
||||
public static var successSample: Self? { return nil }
|
||||
|
||||
// default implementation:
|
||||
public static var failureSample: Self? { return nil }
|
||||
|
||||
// default implementation:
|
||||
public static var samples: [Self] { return [Self.sample] }
|
||||
}
|
||||
|
||||
extension Sampleable {
|
||||
public static func samples<S1: Sampleable>(using s1: S1.Type, with constructor: (S1) -> Self) -> [Self] {
|
||||
return S1.samples.map(constructor)
|
||||
}
|
||||
|
||||
public static func samples<S1: Sampleable, S2: Sampleable>(using s1: S1.Type, _ s2: S2.Type, with constructor: (S1, S2) -> Self) -> [Self] {
|
||||
return zip(S1.samples, S2.samples).map(constructor)
|
||||
}
|
||||
|
||||
public static func samples<S1: Sampleable, S2: Sampleable, S3: Sampleable>(using s1: S1.Type, _ s2: S2.Type, _ s3: S3.Type, with constructor: (S1, S2, S3) -> Self) -> [Self] {
|
||||
return zip3(S1.samples, S2.samples, S3.samples).map(constructor)
|
||||
}
|
||||
|
||||
public static func samples<S1: Sampleable, S2: Sampleable, S3: Sampleable, S4: Sampleable>(using s1: S1.Type, _ s2: S2.Type, _ s3: S3.Type, _ s4: S4.Type, with constructor: (S1, S2, S3, S4) -> Self) -> [Self] {
|
||||
return zip4(S1.samples, S2.samples, S3.samples, S4.samples).map(constructor)
|
||||
}
|
||||
|
||||
public static func samples<S1: Sampleable, S2: Sampleable, S3: Sampleable, S4: Sampleable, S5: Sampleable>(using s1: S1.Type, _ s2: S2.Type, _ s3: S3.Type, _ s4: S4.Type, _ s5: S5.Type, with constructor: (S1, S2, S3, S4, S5) -> Self) -> [Self] {
|
||||
return zip5(S1.samples, S2.samples, S3.samples, S4.samples, S5.samples).map(constructor)
|
||||
}
|
||||
|
||||
public static func samples<S1: Sampleable, S2: Sampleable, S3: Sampleable, S4: Sampleable, S5: Sampleable, S6: Sampleable>(using s1: S1.Type, _ s2: S2.Type, _ s3: S3.Type, _ s4: S4.Type, _ s5: S5.Type, _ s6: S6.Type, with constructor: (S1, S2, S3, S4, S5, S6) -> Self) -> [Self] {
|
||||
// the compiler craps out at zip6. breaking it down makes the difference.
|
||||
let firstZip = zip3(S1.samples, S2.samples, S3.samples)
|
||||
let secondZip = zip3(S4.samples, S5.samples, S6.samples)
|
||||
return zip(firstZip, secondZip).map { arg in (arg.0.0, arg.0.1, arg.0.2, arg.1.0, arg.1.1, arg.1.2) }.map(constructor)
|
||||
}
|
||||
|
||||
public static func samples<S1: Sampleable, S2: Sampleable, S3: Sampleable, S4: Sampleable, S5: Sampleable, S6: Sampleable, S7: Sampleable>(using s1: S1.Type, _ s2: S2.Type, _ s3: S3.Type, _ s4: S4.Type, _ s5: S5.Type, _ s6: S6.Type, _ s7: S7.Type, with constructor: (S1, S2, S3, S4, S5, S6, S7) -> Self) -> [Self] {
|
||||
// the compiler craps out at zip6. breaking it down makes the difference.
|
||||
let firstZip = zip3(S1.samples, S2.samples, S3.samples)
|
||||
let secondZip = zip4(S4.samples, S5.samples, S6.samples, S7.samples)
|
||||
return zip(firstZip, secondZip).map { arg in (arg.0.0, arg.0.1, arg.0.2, arg.1.0, arg.1.1, arg.1.2, arg.1.3) }.map(constructor)
|
||||
}
|
||||
|
||||
public static func samples<S1: Sampleable, S2: Sampleable, S3: Sampleable, S4: Sampleable, S5: Sampleable, S6: Sampleable, S7: Sampleable, S8: Sampleable>(using s1: S1.Type, _ s2: S2.Type, _ s3: S3.Type, _ s4: S4.Type, _ s5: S5.Type, _ s6: S6.Type, _ s7: S7.Type, _ s8: S8.Type, with constructor: (S1, S2, S3, S4, S5, S6, S7, S8) -> Self) -> [Self] {
|
||||
// the compiler craps out at zip6. breaking it down makes the difference.
|
||||
let firstZip = zip4(S1.samples, S2.samples, S3.samples, S4.samples)
|
||||
let secondZip = zip4(S5.samples, S6.samples, S7.samples, S8.samples)
|
||||
return zip(firstZip, secondZip).map { arg in (arg.0.0, arg.0.1, arg.0.2, arg.0.3, arg.1.0, arg.1.1, arg.1.2, arg.1.3) }.map(constructor)
|
||||
}
|
||||
|
||||
@inlinable static func zip3<A: Sequence, B: Sequence, C: Sequence>(_ a: A, _ b: B, _ c: C) -> [(A.Element, B.Element, C.Element)] {
|
||||
return zip(a, zip(b, c)).map { arg in (arg.0, arg.1.0, arg.1.1) }
|
||||
}
|
||||
|
||||
@inlinable static func zip4<A: Sequence, B: Sequence, C: Sequence, D: Sequence>(_ a: A, _ b: B, _ c: C, _ d: D) -> [(A.Element, B.Element, C.Element, D.Element)] {
|
||||
return zip(a, zip(b, zip(c, d))).map { arg in (arg.0, arg.1.0, arg.1.1.0, arg.1.1.1) }
|
||||
}
|
||||
|
||||
@inlinable static func zip5<A: Sequence, B: Sequence, C: Sequence, D: Sequence, E: Sequence>(_ a: A, _ b: B, _ c: C, _ d: D, _ e: E) -> [(A.Element, B.Element, C.Element, D.Element, E.Element)] {
|
||||
return zip(a, zip(b, zip(c, zip(d, e)))).map { arg in (arg.0, arg.1.0, arg.1.1.0, arg.1.1.1.0, arg.1.1.1.1) }
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
import XCTest
|
||||
import JSONAPI
|
||||
import JSONAPIOpenAPI
|
||||
import SwiftCheck
|
||||
import AnyCodable
|
||||
|
||||
class JSONAPIAttributeOpenAPITests: XCTestCase {
|
||||
@@ -504,6 +505,361 @@ extension JSONAPIAttributeOpenAPITests {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Date
|
||||
extension JSONAPIAttributeOpenAPITests {
|
||||
func test_DateStringAttribute() {
|
||||
// TEST:
|
||||
// Encoder is set to use
|
||||
// formatter with date
|
||||
// with no time.
|
||||
|
||||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.dateStyle = .medium
|
||||
dateFormatter.timeStyle = .none
|
||||
dateFormatter.locale = Locale(identifier: "en_US")
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
encoder.dateEncodingStrategy = .formatted(dateFormatter)
|
||||
|
||||
let node = Attribute<Date>.dateOpenAPINodeGuess(using: encoder)
|
||||
|
||||
XCTAssertNotNil(node)
|
||||
|
||||
XCTAssertTrue(node?.required ?? false)
|
||||
XCTAssertEqual(node?.jsonTypeFormat, .string(.date))
|
||||
|
||||
guard case .string(let contextA, let stringContext)? = node else {
|
||||
XCTFail("Expected string Node")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(contextA, .init(format: .date,
|
||||
required: true,
|
||||
nullable: false,
|
||||
allowedValues: nil))
|
||||
|
||||
XCTAssertEqual(stringContext, .init())
|
||||
}
|
||||
|
||||
func test_DateStringAttribute_Sampleable() {
|
||||
// TEST:
|
||||
// Encoder is set to use
|
||||
// formatter with date
|
||||
// with no time.
|
||||
|
||||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.dateStyle = .medium
|
||||
dateFormatter.timeStyle = .none
|
||||
dateFormatter.locale = Locale(identifier: "en_US")
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
encoder.dateEncodingStrategy = .formatted(dateFormatter)
|
||||
|
||||
let node = try! Attribute<Date>.genericOpenAPINode(using: encoder)
|
||||
|
||||
XCTAssertTrue(node.required)
|
||||
XCTAssertEqual(node.jsonTypeFormat, .string(.date))
|
||||
|
||||
guard case .string(let contextA, let stringContext) = node else {
|
||||
XCTFail("Expected string Node")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(contextA, .init(format: .date,
|
||||
required: true,
|
||||
nullable: false,
|
||||
allowedValues: nil))
|
||||
|
||||
XCTAssertEqual(stringContext, .init())
|
||||
}
|
||||
|
||||
func test_DateTimeStringAttribute() {
|
||||
// TEST:
|
||||
// Encoder is set to use
|
||||
// formatter with date
|
||||
// with time.
|
||||
|
||||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.dateStyle = .medium
|
||||
dateFormatter.timeStyle = .short
|
||||
dateFormatter.locale = Locale(identifier: "en_US")
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
encoder.dateEncodingStrategy = .formatted(dateFormatter)
|
||||
|
||||
let node = Attribute<Date>.dateOpenAPINodeGuess(using: encoder)
|
||||
|
||||
XCTAssertNotNil(node)
|
||||
|
||||
XCTAssertTrue(node?.required ?? false)
|
||||
XCTAssertEqual(node?.jsonTypeFormat, .string(.dateTime))
|
||||
|
||||
guard case .string(let contextA, let stringContext)? = node else {
|
||||
XCTFail("Expected string Node")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(contextA, .init(format: .dateTime,
|
||||
required: true,
|
||||
nullable: false,
|
||||
allowedValues: nil))
|
||||
|
||||
XCTAssertEqual(stringContext, .init())
|
||||
}
|
||||
|
||||
func test_DateTimeStringAttribute_Sampleable() {
|
||||
// TEST:
|
||||
// Encoder is set to use
|
||||
// formatter with date
|
||||
// with time.
|
||||
|
||||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.dateStyle = .medium
|
||||
dateFormatter.timeStyle = .short
|
||||
dateFormatter.locale = Locale(identifier: "en_US")
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
encoder.dateEncodingStrategy = .formatted(dateFormatter)
|
||||
|
||||
let node = try! Attribute<Date>.genericOpenAPINode(using: encoder)
|
||||
|
||||
XCTAssertTrue(node.required)
|
||||
XCTAssertEqual(node.jsonTypeFormat, .string(.dateTime))
|
||||
|
||||
guard case .string(let contextA, let stringContext) = node else {
|
||||
XCTFail("Expected string Node")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(contextA, .init(format: .dateTime,
|
||||
required: true,
|
||||
nullable: false,
|
||||
allowedValues: nil))
|
||||
|
||||
XCTAssertEqual(stringContext, .init())
|
||||
}
|
||||
|
||||
func test_8601DateStringAttribute() {
|
||||
if #available(OSX 10.12, *) {
|
||||
// TEST:
|
||||
// Encoder is set to use
|
||||
// iso8601 date format
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
encoder.dateEncodingStrategy = .iso8601
|
||||
|
||||
let node = Attribute<Date>.dateOpenAPINodeGuess(using: encoder)
|
||||
|
||||
XCTAssertNotNil(node)
|
||||
|
||||
XCTAssertTrue(node?.required ?? false)
|
||||
XCTAssertEqual(node?.jsonTypeFormat, .string(.dateTime))
|
||||
|
||||
guard case .string(let contextA, let stringContext)? = node else {
|
||||
XCTFail("Expected string Node")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(contextA, .init(format: .dateTime,
|
||||
required: true,
|
||||
nullable: false,
|
||||
allowedValues: nil))
|
||||
|
||||
XCTAssertEqual(stringContext, .init())
|
||||
}
|
||||
}
|
||||
|
||||
func test_8601DateStringAttribute_Sampleable() {
|
||||
if #available(OSX 10.12, *) {
|
||||
// TEST:
|
||||
// Encoder is set to use
|
||||
// iso8601 date format
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
encoder.dateEncodingStrategy = .iso8601
|
||||
|
||||
let node = try! Attribute<Date>.genericOpenAPINode(using: encoder)
|
||||
|
||||
XCTAssertTrue(node.required)
|
||||
XCTAssertEqual(node.jsonTypeFormat, .string(.dateTime))
|
||||
|
||||
guard case .string(let contextA, let stringContext) = node else {
|
||||
XCTFail("Expected string Node")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(contextA, .init(format: .dateTime,
|
||||
required: true,
|
||||
nullable: false,
|
||||
allowedValues: nil))
|
||||
|
||||
XCTAssertEqual(stringContext, .init())
|
||||
}
|
||||
}
|
||||
|
||||
func test_DateNumberAttribute() {
|
||||
// TEST:
|
||||
// Encoder is set to use
|
||||
// seconds since 1970 as
|
||||
// date format
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
encoder.dateEncodingStrategy = .secondsSince1970
|
||||
|
||||
let node = Attribute<Date>.dateOpenAPINodeGuess(using: encoder)
|
||||
|
||||
XCTAssertNotNil(node)
|
||||
|
||||
XCTAssertTrue(node?.required ?? false)
|
||||
XCTAssertEqual(node?.jsonTypeFormat, .number(.double))
|
||||
|
||||
guard case .number(let contextA, let numberContext)? = node else {
|
||||
XCTFail("Expected string Node")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(contextA, .init(format: .double,
|
||||
required: true,
|
||||
nullable: false,
|
||||
allowedValues: nil))
|
||||
|
||||
XCTAssertEqual(numberContext, .init())
|
||||
}
|
||||
|
||||
func test_DateNumberAttribute_Sampleable() {
|
||||
// TEST:
|
||||
// Encoder is set to use
|
||||
// seconds since 1970 as
|
||||
// date format
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
encoder.dateEncodingStrategy = .secondsSince1970
|
||||
|
||||
let node = try! Attribute<Date>.genericOpenAPINode(using: encoder)
|
||||
|
||||
XCTAssertTrue(node.required)
|
||||
XCTAssertEqual(node.jsonTypeFormat, .number(.double))
|
||||
|
||||
guard case .number(let contextA, let numberContext) = node else {
|
||||
XCTFail("Expected string Node")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(contextA, .init(format: .double,
|
||||
required: true,
|
||||
nullable: false,
|
||||
allowedValues: nil))
|
||||
|
||||
XCTAssertEqual(numberContext, .init())
|
||||
}
|
||||
|
||||
func test_DateDeferredAttribute() {
|
||||
// TEST:
|
||||
// Encoder is set to use
|
||||
// Date default encoding
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
encoder.dateEncodingStrategy = .deferredToDate
|
||||
|
||||
let node = Attribute<Date>.dateOpenAPINodeGuess(using: encoder)
|
||||
|
||||
XCTAssertNil(node)
|
||||
}
|
||||
|
||||
func test_DateDeferredAttribute_Sampleable() {
|
||||
// TEST:
|
||||
// Encoder is set to use
|
||||
// Date default encoding
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
encoder.dateEncodingStrategy = .deferredToDate
|
||||
|
||||
let node = try! Attribute<Date>.genericOpenAPINode(using: encoder)
|
||||
|
||||
XCTAssertTrue(node.required)
|
||||
XCTAssertEqual(node.jsonTypeFormat, .number(.double))
|
||||
|
||||
guard case .number(let contextA, let numberContext) = node else {
|
||||
XCTFail("Expected string Node")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(contextA, .init(format: .double,
|
||||
required: true,
|
||||
nullable: false,
|
||||
allowedValues: nil))
|
||||
|
||||
XCTAssertEqual(numberContext, .init())
|
||||
}
|
||||
|
||||
// func test_NullableEnumAttribute() {
|
||||
// let node = try! Attribute<EnumAttribute?>.wrappedOpenAPINode()
|
||||
//
|
||||
// XCTAssertTrue(node.required)
|
||||
// XCTAssertEqual(node.jsonTypeFormat, .string(.generic))
|
||||
//
|
||||
// guard case .string(let contextA, let stringContext) = node else {
|
||||
// XCTFail("Expected string Node")
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// XCTAssertEqual(contextA, .init(format: .generic,
|
||||
// required: true,
|
||||
// nullable: true,
|
||||
// allowedValues: nil))
|
||||
//
|
||||
// XCTAssertEqual(stringContext, .init())
|
||||
// }
|
||||
//
|
||||
// func test_OptionalEnumAttribute() {
|
||||
// let node = try! Attribute<EnumAttribute>?.wrappedOpenAPINode()
|
||||
//
|
||||
// XCTAssertFalse(node.required)
|
||||
// XCTAssertEqual(node.jsonTypeFormat, .string(.generic))
|
||||
//
|
||||
// guard case .string(let contextA, let stringContext) = node else {
|
||||
// XCTFail("Expected string Node")
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// XCTAssertEqual(contextA, .init(format: .generic,
|
||||
// required: false,
|
||||
// nullable: false,
|
||||
// allowedValues: nil))
|
||||
//
|
||||
// XCTAssertEqual(stringContext, .init())
|
||||
// }
|
||||
//
|
||||
// func test_OptionalNullableEnumAttribute() {
|
||||
// let node = try! Attribute<EnumAttribute?>?.wrappedOpenAPINode()
|
||||
//
|
||||
// XCTAssertFalse(node.required)
|
||||
// XCTAssertEqual(node.jsonTypeFormat, .string(.generic))
|
||||
//
|
||||
// guard case .string(let contextA, let stringContext) = node else {
|
||||
// XCTFail("Expected string Node")
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// XCTAssertEqual(contextA, .init(format: .generic,
|
||||
// required: false,
|
||||
// nullable: true,
|
||||
// allowedValues: nil))
|
||||
//
|
||||
// XCTAssertEqual(stringContext, .init())
|
||||
// }
|
||||
}
|
||||
|
||||
// MARK: - Test Types
|
||||
extension JSONAPIAttributeOpenAPITests {
|
||||
enum EnumAttribute: String, Codable, CaseIterable {
|
||||
@@ -511,3 +867,9 @@ extension JSONAPIAttributeOpenAPITests {
|
||||
case two
|
||||
}
|
||||
}
|
||||
|
||||
extension Date: SampleableOpenAPIType {
|
||||
public static var sample: Date {
|
||||
return TimeInterval.arbitrary.map { Date(timeIntervalSince1970: $0) }.generate
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,15 +6,23 @@
|
||||
//
|
||||
|
||||
import XCTest
|
||||
import SwiftCheck
|
||||
import JSONAPI
|
||||
import JSONAPIOpenAPI
|
||||
|
||||
class JSONAPIDocumentOpenAPITests: XCTestCase {
|
||||
func test_SingleResourceDocument() {
|
||||
let node = try! SingleEntityDocument.openAPINode()
|
||||
|
||||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.dateStyle = .medium
|
||||
dateFormatter.timeStyle = .none
|
||||
dateFormatter.locale = Locale(identifier: "en_US")
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
encoder.dateEncodingStrategy = .formatted(dateFormatter)
|
||||
|
||||
let node = try! SingleEntityDocument.openAPINodeWithExample(using: encoder)
|
||||
|
||||
print(String(data: try! encoder.encode(node), encoding: .utf8)!)
|
||||
}
|
||||
@@ -27,9 +35,11 @@ extension JSONAPIDocumentOpenAPITests {
|
||||
|
||||
struct Attributes: JSONAPI.Attributes, Sampleable {
|
||||
let name: Attribute<String>
|
||||
let date: Attribute<Date>
|
||||
|
||||
static var sample: Attributes {
|
||||
return .init(name: "hello world")
|
||||
return .init(name: "hello world",
|
||||
date: .init(value: Date()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,3 +50,29 @@ extension JSONAPIDocumentOpenAPITests {
|
||||
|
||||
typealias SingleEntityDocument = Document<SingleResourceBody<TestEntity>, NoMetadata, NoLinks, NoIncludes, NoAPIDescription, UnknownJSONAPIError>
|
||||
}
|
||||
|
||||
extension Id: Sampleable where RawType == String {
|
||||
public static var sample: Id<RawType, IdentifiableType> {
|
||||
return .init(rawValue: String.arbitrary.generate)
|
||||
}
|
||||
}
|
||||
|
||||
extension JSONAPI.Entity: Sampleable where Description.Attributes: Sampleable, Description.Relationships: Sampleable, MetaType: Sampleable, LinksType: Sampleable, EntityRawIdType == String {
|
||||
public static var sample: JSONAPI.Entity<Description, MetaType, LinksType, EntityRawIdType> {
|
||||
return JSONAPI.Entity(id: .sample,
|
||||
attributes: .sample,
|
||||
relationships: .sample,
|
||||
meta: .sample,
|
||||
links: .sample)
|
||||
}
|
||||
}
|
||||
|
||||
extension Document: Sampleable where PrimaryResourceBody: Sampleable, MetaType: Sampleable, LinksType: Sampleable, IncludeType: Sampleable, APIDescription: Sampleable, Error: Sampleable {
|
||||
public static var sample: Document<PrimaryResourceBody, MetaType, LinksType, IncludeType, APIDescription, Error> {
|
||||
return Document(apiDescription: .sample,
|
||||
body: .sample,
|
||||
includes: .sample,
|
||||
meta: .sample,
|
||||
links: .sample)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import AnyCodable
|
||||
|
||||
class JSONAPIEntityOpenAPITests: XCTestCase {
|
||||
func test_EmptyEntity() {
|
||||
let node = try! TestType1.openAPINode()
|
||||
let node = try! TestType1.openAPINode(using: JSONEncoder())
|
||||
|
||||
XCTAssertTrue(node.required)
|
||||
XCTAssertEqual(node.jsonTypeFormat, .object(.generic))
|
||||
@@ -40,7 +40,16 @@ class JSONAPIEntityOpenAPITests: XCTestCase {
|
||||
}
|
||||
|
||||
func test_AttributesEntity() {
|
||||
let node = try! TestType2.openAPINode()
|
||||
|
||||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.dateStyle = .medium
|
||||
dateFormatter.timeStyle = .short
|
||||
dateFormatter.locale = Locale(identifier: "en_US")
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .formatted(dateFormatter)
|
||||
|
||||
let node = try! TestType2.openAPINode(using: encoder)
|
||||
|
||||
XCTAssertTrue(node.required)
|
||||
XCTAssertEqual(node.jsonTypeFormat, .object(.generic))
|
||||
@@ -83,9 +92,9 @@ class JSONAPIEntityOpenAPITests: XCTestCase {
|
||||
nullable: false,
|
||||
allowedValues: nil))
|
||||
|
||||
XCTAssertEqual(attributesContext.minProperties, 3)
|
||||
XCTAssertEqual(Set(attributesContext.requiredProperties), Set(["stringProperty", "enumProperty", "nullableProperty"]))
|
||||
XCTAssertEqual(Set(attributesContext.properties.keys), Set(["stringProperty", "enumProperty", "optionalProperty", "nullableProperty", "nullableOptionalProperty"]))
|
||||
XCTAssertEqual(attributesContext.minProperties, 4)
|
||||
XCTAssertEqual(Set(attributesContext.requiredProperties), Set(["stringProperty", "enumProperty", "dateProperty", "nullableProperty"]))
|
||||
XCTAssertEqual(Set(attributesContext.properties.keys), Set(["stringProperty", "enumProperty", "dateProperty", "optionalProperty", "nullableProperty", "nullableOptionalProperty"]))
|
||||
|
||||
XCTAssertEqual(attributesContext.properties["stringProperty"],
|
||||
.string(.init(format: .generic,
|
||||
@@ -99,6 +108,13 @@ class JSONAPIEntityOpenAPITests: XCTestCase {
|
||||
allowedValues: ["one", "two"].map(AnyCodable.init)),
|
||||
.init()))
|
||||
|
||||
XCTAssertEqual(attributesContext.properties["dateProperty"],
|
||||
.string(.init(format: .dateTime,
|
||||
required: true,
|
||||
nullable: false,
|
||||
allowedValues: nil),
|
||||
.init()))
|
||||
|
||||
XCTAssertEqual(attributesContext.properties["optionalProperty"],
|
||||
.string(.init(format: .generic,
|
||||
required: false,
|
||||
@@ -122,7 +138,7 @@ class JSONAPIEntityOpenAPITests: XCTestCase {
|
||||
}
|
||||
|
||||
func test_RelationshipsEntity() {
|
||||
let node = try! TestType3.openAPINode()
|
||||
let node = try! TestType3.openAPINode(using: JSONEncoder())
|
||||
|
||||
XCTAssertTrue(node.required)
|
||||
XCTAssertEqual(node.jsonTypeFormat, .object(.generic))
|
||||
@@ -268,6 +284,7 @@ extension JSONAPIEntityOpenAPITests {
|
||||
public struct Attributes: JSONAPI.Attributes, Sampleable {
|
||||
let stringProperty: Attribute<String>
|
||||
let enumProperty: Attribute<EnumType>
|
||||
let dateProperty: Attribute<Date>
|
||||
let optionalProperty: Attribute<String>?
|
||||
let nullableProperty: Attribute<String?>
|
||||
let nullableOptionalProperty: Attribute<String?>?
|
||||
@@ -278,6 +295,7 @@ extension JSONAPIEntityOpenAPITests {
|
||||
public static var sample: Attributes {
|
||||
return Attributes(stringProperty: .init(value: "hello"),
|
||||
enumProperty: .init(value: .one),
|
||||
dateProperty: .init(value: Date()),
|
||||
optionalProperty: nil,
|
||||
nullableProperty: .init(value: nil),
|
||||
nullableOptionalProperty: nil)
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// OpenAPITests.swift
|
||||
// JSONAPIOpenAPITests
|
||||
//
|
||||
// Created by Mathew Polzin on 1/25/19.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
import JSONAPI
|
||||
import JSONAPIOpenAPI
|
||||
|
||||
class OpenAPITests: XCTestCase {
|
||||
|
||||
func test_placeholder() {
|
||||
|
||||
let schemaInfo = OpenAPISchema.Info(title: "Cool API", version: "0.1.0")
|
||||
|
||||
let personResponse = OpenAPIResponse(description: "Successfully created a Person",
|
||||
content: [
|
||||
.json: .init(schema: .init(JSONReference.node(.init(type: \.schemas, selector: "person"))))
|
||||
])
|
||||
|
||||
let schemaPaths: [OpenAPISchema.PathComponents: OpenAPIPathItem] = [
|
||||
.init(["api","people"]):
|
||||
.operations(
|
||||
.init(parameters: [],
|
||||
post: OpenAPIPathItem.PathProperties.Operation(
|
||||
summary: "",
|
||||
operationId: "createPerson",
|
||||
parameters: [],
|
||||
responses: [
|
||||
.status(code: 200): .init(personResponse)
|
||||
]
|
||||
)
|
||||
)
|
||||
)
|
||||
]
|
||||
|
||||
let schemaComponents = OpenAPIComponents(schemas: ["person": .reference(.file("person.json"))],
|
||||
parameters: [:])
|
||||
|
||||
let openAPISchema = OpenAPISchema(info: schemaInfo,
|
||||
paths: schemaPaths,
|
||||
components: schemaComponents)
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
|
||||
print(String(data: try! encoder.encode(openAPISchema), encoding: .utf8)!)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user