Compare commits

...

5 Commits

Author SHA1 Message Date
Mathew Polzin 57df6b147e Add Sampleable conformance to UnknownJSONAPIError. Add Sampleable conformance to all Include types and the Includes type. 2019-01-23 11:52:16 -08:00
Mathew Polzin a88844fe5e Switch to Poly 1.0.0 (same commit as previously used but by version rather than branch) 2019-01-23 00:34:14 -08:00
Mathew Polzin 850a713dcb Merge pull request #13 from mattpolzin/feature/OpenAPISchema
Add Include support to OpenAPI schema of JSONAPI Document.
2019-01-22 12:01:40 -08:00
Mathew Polzin e3c637a41e update README and add associated type labels to patterns for clarity 2019-01-22 11:57:18 -08:00
Mathew Polzin 9cbc626410 Add Include support to OpenAPI schema of JSONAPI Document. 2019-01-22 11:53:32 -08:00
12 changed files with 419 additions and 31 deletions
@@ -21,3 +21,10 @@ print("Dog Document Schema")
print("====")
print(dogDocumentSchemaData.map { String(data: $0, encoding: .utf8)! } ?? "Schema Construction Failed")
print("====")
let batchPersonSchemaData = try? encoder.encode(BatchPeopleDocument.openAPINodeWithExample())
print("Batch Person Document Schema")
print("====")
print(batchPersonSchemaData.map { String(data: $0, encoding: .utf8)! } ?? "Schema Construction Failed")
print("====")
@@ -5,13 +5,28 @@ import JSONAPIOpenAPI
import SwiftCheck
import JSONAPIArbitrary
extension PersonDescription.Attributes: Sampleable {
extension PersonDescription.Attributes: Arbitrary, Sampleable {
public static var arbitrary: Gen<PersonDescription.Attributes> {
return Gen.compose { c in
return PersonDescription.Attributes(name: c.generate(),
favoriteColor: c.generate())
}
}
public static var sample: PersonDescription.Attributes {
return .init(name: ["Abbie", "Eibba"], favoriteColor: "Blue")
}
}
extension PersonDescription.Relationships: Sampleable {
extension PersonDescription.Relationships: Arbitrary, Sampleable {
public static var arbitrary: Gen<PersonDescription.Relationships> {
return Gen.compose { c in
return PersonDescription.Relationships(friends: c.generate(),
dogs: c.generate(),
home: c.generate())
}
}
public static var sample: PersonDescription.Relationships {
return .init(friends: ["1", "2"], dogs: ["2"], home: "1")
}
+2 -2
View File
@@ -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
View File
@@ -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")
],
+1 -1
View File
@@ -104,7 +104,7 @@ Note that Playground support for importing non-system Frameworks is still a bit
- `included`
- [x] Encoding/Decoding
- [x] Arbitrary
- [ ] OpenAPI
- [x] OpenAPI
- `errors`
- [x] Encoding/Decoding
- [x] Arbitrary
@@ -0,0 +1,131 @@
//
// JSONAPIInclude+OpenAPI.swift
// JSONAPIOpenAPI
//
// Created by Mathew Polzin on 1/22/19.
//
import JSONAPI
extension Includes: OpenAPINodeType where I: OpenAPINodeType {
public static func openAPINode() throws -> JSONNode {
let includeNode = try I.openAPINode()
return .array(.init(format: .generic,
required: true),
.init(items: includeNode,
uniqueItems: true))
}
}
extension Include0: OpenAPINodeType {
public static func openAPINode() throws -> JSONNode {
throw OpenAPITypeError.invalidNode
}
}
extension Include1: OpenAPINodeType where A: OpenAPINodeType {
public static func openAPINode() throws -> JSONNode {
return try .one(of: [A.openAPINode()])
}
}
extension Include2: OpenAPINodeType where A: OpenAPINodeType, B: OpenAPINodeType {
public static func openAPINode() throws -> JSONNode {
return try .one(of: [
A.openAPINode(),
B.openAPINode()
])
}
}
extension Include3: OpenAPINodeType where A: OpenAPINodeType, B: OpenAPINodeType, C: OpenAPINodeType {
public static func openAPINode() throws -> JSONNode {
return try .one(of: [
A.openAPINode(),
B.openAPINode(),
C.openAPINode()
])
}
}
extension Include4: OpenAPINodeType where A: OpenAPINodeType, B: OpenAPINodeType, C: OpenAPINodeType, D: OpenAPINodeType {
public static func openAPINode() throws -> JSONNode {
return try .one(of: [
A.openAPINode(),
B.openAPINode(),
C.openAPINode(),
D.openAPINode()
])
}
}
extension Include5: OpenAPINodeType where A: OpenAPINodeType, B: OpenAPINodeType, C: OpenAPINodeType, D: OpenAPINodeType, E: OpenAPINodeType {
public static func openAPINode() throws -> JSONNode {
return try .one(of: [
A.openAPINode(),
B.openAPINode(),
C.openAPINode(),
D.openAPINode(),
E.openAPINode()
])
}
}
extension Include6: OpenAPINodeType where A: OpenAPINodeType, B: OpenAPINodeType, C: OpenAPINodeType, D: OpenAPINodeType, E: OpenAPINodeType, F: OpenAPINodeType {
public static func openAPINode() throws -> JSONNode {
return try .one(of: [
A.openAPINode(),
B.openAPINode(),
C.openAPINode(),
D.openAPINode(),
E.openAPINode(),
F.openAPINode()
])
}
}
extension Include7: OpenAPINodeType where A: OpenAPINodeType, B: OpenAPINodeType, C: OpenAPINodeType, D: OpenAPINodeType, E: OpenAPINodeType, F: OpenAPINodeType, G: OpenAPINodeType {
public static func openAPINode() throws -> JSONNode {
return try .one(of: [
A.openAPINode(),
B.openAPINode(),
C.openAPINode(),
D.openAPINode(),
E.openAPINode(),
F.openAPINode(),
G.openAPINode()
])
}
}
extension Include8: OpenAPINodeType where A: OpenAPINodeType, B: OpenAPINodeType, C: OpenAPINodeType, D: OpenAPINodeType, E: OpenAPINodeType, F: OpenAPINodeType, G: OpenAPINodeType, H: OpenAPINodeType {
public static func openAPINode() throws -> JSONNode {
return try .one(of: [
A.openAPINode(),
B.openAPINode(),
C.openAPINode(),
D.openAPINode(),
E.openAPINode(),
F.openAPINode(),
G.openAPINode(),
H.openAPINode()
])
}
}
extension Include9: OpenAPINodeType where A: OpenAPINodeType, B: OpenAPINodeType, C: OpenAPINodeType, D: OpenAPINodeType, E: OpenAPINodeType, F: OpenAPINodeType, G: OpenAPINodeType, H: OpenAPINodeType, I: OpenAPINodeType {
public static func openAPINode() throws -> JSONNode {
return try .one(of: [
A.openAPINode(),
B.openAPINode(),
C.openAPINode(),
D.openAPINode(),
E.openAPINode(),
F.openAPINode(),
G.openAPINode(),
H.openAPINode(),
I.openAPINode()
])
}
}
@@ -178,17 +178,30 @@ extension ManyResourceBody: OpenAPINodeType where Entity: OpenAPINodeType {
}
}
extension Document: OpenAPINodeType where PrimaryResourceBody: OpenAPINodeType {
extension Document: OpenAPINodeType where PrimaryResourceBody: OpenAPINodeType, IncludeType: OpenAPINodeType {
public static func openAPINode() throws -> JSONNode {
// TODO: metadata, links, api description, includes, errors
// TODO: metadata, links, api description, errors
// TODO: represent data and errors as the two distinct possible outcomes
let primaryDataNode: JSONNode? = try PrimaryResourceBody.openAPINode()
let primaryDataProperty = primaryDataNode.map { ("data", $0) }
let includeNode: JSONNode?
do {
includeNode = try Includes<Include>.openAPINode()
} catch let err as OpenAPITypeError {
guard err == .invalidNode else {
throw err
}
includeNode = nil
}
let includeProperty = includeNode.map { ("included", $0) }
let propertiesDict = Dictionary([
primaryDataProperty
primaryDataProperty,
includeProperty
].compactMap { $0 }) { _, value in value }
return .object(.init(format: .generic,
@@ -149,6 +149,14 @@ extension JSONNode.ObjectContext : Encodable {
}
extension JSONNode: Encodable {
private enum SubschemaCodingKeys: String, CodingKey {
case allOf
case oneOf
case anyOf
case not
}
public func encode(to encoder: Encoder) throws {
switch self {
case .boolean(let context):
@@ -162,21 +170,25 @@ extension JSONNode: Encodable {
try contextA.encode(to: encoder)
try contextB.encode(to: encoder)
case .allOf(let nodes):
// TODO
print("TODO")
case .all(of: let nodes):
var container = encoder.container(keyedBy: SubschemaCodingKeys.self)
case .oneOf(let nodes):
// TODO
print("TODO")
try container.encode(nodes, forKey: .allOf)
case .anyOf(let nodes):
// TODO
print("TODO")
case .one(of: let nodes):
var container = encoder.container(keyedBy: SubschemaCodingKeys.self)
try container.encode(nodes, forKey: .oneOf)
case .any(of: let nodes):
var container = encoder.container(keyedBy: SubschemaCodingKeys.self)
try container.encode(nodes, forKey: .anyOf)
case .not(let node):
// TODO
print("TODO")
var container = encoder.container(keyedBy: SubschemaCodingKeys.self)
try container.encode(node, forKey: .not)
}
}
}
@@ -245,9 +245,9 @@ public enum JSONNode: Equatable {
case number(Context<JSONTypeFormat.NumberFormat>, NumericContext)
case integer(Context<JSONTypeFormat.IntegerFormat>, NumericContext)
case string(Context<JSONTypeFormat.StringFormat>, StringContext)
indirect case allOf([JSONNode])
indirect case oneOf([JSONNode])
indirect case anyOf([JSONNode])
indirect case all(of: [JSONNode])
indirect case one(of: [JSONNode])
indirect case any(of: [JSONNode])
indirect case not(JSONNode)
public struct Context<Format: OpenAPIFormat>: JSONNodeContext, Equatable {
@@ -451,7 +451,7 @@ public enum JSONNode: Equatable {
return .integer(context.format)
case .string(let context, _):
return .string(context.format)
case .allOf, .oneOf, .anyOf, .not:
case .all, .one, .any, .not:
return nil
}
}
@@ -465,7 +465,7 @@ public enum JSONNode: Equatable {
.integer(let contextA as JSONNodeContext, _),
.string(let contextA as JSONNodeContext, _):
return contextA.required
case .allOf, .oneOf, .anyOf, .not:
case .all, .one, .any, .not:
return true
}
}
@@ -485,7 +485,7 @@ public enum JSONNode: Equatable {
return .integer(context.optionalContext(), contextB)
case .string(let context, let contextB):
return .string(context.optionalContext(), contextB)
case .allOf, .oneOf, .anyOf, .not:
case .all, .one, .any, .not:
return self
}
}
@@ -505,7 +505,7 @@ public enum JSONNode: Equatable {
return .integer(context.requiredContext(), contextB)
case .string(let context, let contextB):
return .string(context.requiredContext(), contextB)
case .allOf, .oneOf, .anyOf, .not:
case .all, .one, .any, .not:
return self
}
}
@@ -525,7 +525,7 @@ public enum JSONNode: Equatable {
return .integer(context.nullableContext(), contextB)
case .string(let context, let contextB):
return .string(context.nullableContext(), contextB)
case .allOf, .oneOf, .anyOf, .not:
case .all, .one, .any, .not:
return self
}
}
@@ -545,7 +545,7 @@ public enum JSONNode: Equatable {
return .integer(context.with(allowedValues: allowedValues), contextB)
case .string(let context, let contextB):
return .string(context.with(allowedValues: allowedValues), contextB)
case .allOf, .oneOf, .anyOf, .not:
case .all, .one, .any, .not:
return self
}
}
@@ -571,13 +571,17 @@ public enum JSONNode: Equatable {
return .integer(context.with(example: example), contextB)
case .string(let context, let contextB):
return .string(context.with(example: example), contextB)
case .allOf, .oneOf, .anyOf, .not:
case .all, .one, .any, .not:
return self
}
}
}
public enum OpenAPICodableError: Swift.Error {
public enum OpenAPICodableError: Swift.Error, Equatable {
case allCasesArrayNotCodable
case exampleNotCodable
}
public enum OpenAPITypeError: Swift.Error, Equatable {
case invalidNode
}
@@ -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
}
}
@@ -18,17 +18,33 @@ public protocol Sampleable {
/// 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 {
@@ -114,3 +130,9 @@ extension NoLinks: Sampleable {
return .none
}
}
extension UnknownJSONAPIError: Sampleable {
public static var sample: UnknownJSONAPIError {
return .unknownError
}
}