Skip to content

Commit

Permalink
moar nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpolzin committed Dec 15, 2019
1 parent 6bfe143 commit 7a0b0e1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 28 deletions.
14 changes: 5 additions & 9 deletions Sources/OpenAPIKit/JSON Utility/Sampleable+OpenAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension Sampleable where Self: Encodable {
}
}

public func genericOpenAPINode<T: Encodable>(for value: T, using encoder: JSONEncoder) throws -> JSONSchema {
public func genericOpenAPINode<T>(for value: T, using encoder: JSONEncoder) throws -> JSONSchema {

let mirror = Mirror(reflecting: value)
let properties: [(String, JSONSchema)] = try mirror.children.compactMap { child in
Expand All @@ -41,9 +41,8 @@ public func genericOpenAPINode<T: Encodable>(for value: T, using encoder: JSONEn
}()

// try to snag an OpenAPI Node
guard let openAPINode: JSONSchema = try openAPINodeGuess(for: child.value, using: encoder) else {
throw OpenAPITypeError.unknownNodeType(type(of: value))
}
let openAPINode: JSONSchema = try openAPINodeGuess(for: child.value, using: encoder)
?? genericOpenAPINode(for: child.value, using: encoder)

// put it all together
let newNode: JSONSchema
Expand All @@ -56,11 +55,8 @@ public func genericOpenAPINode<T: Encodable>(for value: T, using encoder: JSONEn
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(for: value, using: encoder) {
return primitive
if properties.count != mirror.children.count {
throw OpenAPITypeError.unknownNodeType(type(of: value))
}

// There should not be any duplication of keys since these are
Expand Down
69 changes: 50 additions & 19 deletions Tests/OpenAPIKitTests/GenericOpenAPINodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,27 @@ import XCTest
import OpenAPIKit

final class GenericOpenAPINodeTests: XCTestCase {
func test_failsAsUnknown() {
XCTAssertThrowsError(try FailsAsUnknown.genericOpenAPINode(using: JSONEncoder())) { error in
guard let err = error as? OpenAPITypeError,
case .unknownNodeType = err else {
XCTFail("Expected unknown node type error")
return
}
}
// func test_failsAsUnknown() {
// XCTAssertThrowsError(try FailsAsUnknown.genericOpenAPINode(using: JSONEncoder())) { error in
// guard let err = error as? OpenAPITypeError,
// case .unknownNodeType = err else {
// XCTFail("Expected unknown node type error")
// return
// }
// }
// }

func test_emptyObject() throws {
let node = try EmptyObjectType.genericOpenAPINode(using: JSONEncoder())

XCTAssertEqual(
node,
JSONSchema.object(
properties: [
"empty": .object
]
)
)
}

func test_basicTypes() throws {
Expand Down Expand Up @@ -139,6 +152,15 @@ final class GenericOpenAPINodeTests: XCTestCase {
items: .object(
additionalProperties: .init(.number(format: .double))
)
),
"structure": .object(
properties: [
"bool": .boolean,
"array": .array(items: .string),
"dict": .object(
additionalProperties: .init(.integer)
)
]
)
]
)
Expand Down Expand Up @@ -244,14 +266,23 @@ extension GenericOpenAPINodeTests {

let arrayDict: [[String: Date]]

let structure: Structure

struct Structure: Codable {
let bool: Bool
let array: [String]
let dict: [String: Int]
}

static let sample: Nested = .init(
array1: ["hello"],
array2: [10.5],
array3: [Date()],
dict1: ["hello": "world"],
dict2: ["is it?" : true],
dictArray: ["this" : [1234]],
arrayDict: [["now" : Date()]]
array1: [],
array2: [],
array3: [],
dict1: [:],
dict2: [:],
dictArray: [:],
arrayDict: [],
structure: .init(bool: true, array: [], dict: [:])
)
}

Expand Down Expand Up @@ -348,12 +379,12 @@ extension GenericOpenAPINodeTests {
)
}

struct FailsAsUnknown: Codable, SampleableOpenAPIType {
let unknown: Unknown
struct EmptyObjectType: Codable, SampleableOpenAPIType {
let empty: EmptyObject

struct Unknown: Codable {}
struct EmptyObject: Codable {}

static let sample: FailsAsUnknown = .init(unknown: .init())
static let sample: EmptyObjectType = .init(empty: .init())
}

// struct EncodesAsPrimitive: Codable, SampleableOpenAPIType {
Expand Down

0 comments on commit 7a0b0e1

Please sign in to comment.