Skip to content

Commit

Permalink
Fix bug causing a supplied encoder to be used for generating an examp…
Browse files Browse the repository at this point in the history
…le but not for helping determine the correct Date formatting.
  • Loading branch information
mattpolzin committed Jan 25, 2019
1 parent 58a7c82 commit 2b59f54
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
12 changes: 6 additions & 6 deletions Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ public protocol OpenAPINodeType {
static func openAPINode() throws -> JSONNode
}

extension OpenAPINodeType where Self: Sampleable, Self: Encodable {
public static func openAPINodeWithExample(using encoder: JSONEncoder = JSONEncoder()) throws -> JSONNode {
return try openAPINode().with(example: Self.successSample ?? Self.sample, using: encoder)
}
}

/// Anything conforming to `OpenAPIEncodedNodeType` can provide an
/// OpenAPI schema representing itself but it may need an Encoder
/// to do its job.
public protocol OpenAPIEncodedNodeType: OpenAPINodeType {
static func openAPINode(using encoder: JSONEncoder) throws -> JSONNode
}

extension OpenAPIEncodedNodeType where Self: Sampleable, Self: Encodable {
public static func openAPINodeWithExample(using encoder: JSONEncoder = JSONEncoder()) throws -> JSONNode {
return try openAPINode(using: encoder).with(example: Self.successSample ?? Self.sample, using: encoder)
}
}

extension OpenAPIEncodedNodeType {
public static func openAPINode() throws -> JSONNode {
return try openAPINode(using: JSONEncoder())
Expand Down
26 changes: 22 additions & 4 deletions Tests/JSONAPIOpenAPITests/JSONAPIEntityOpenAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ class JSONAPIEntityOpenAPITests: XCTestCase {
}

func test_AttributesEntity() {
let node = try! TestType2.openAPINode(using: JSONEncoder())

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))
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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?>?
Expand All @@ -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)
Expand Down

0 comments on commit 2b59f54

Please sign in to comment.