From 2b59f540676d575d63b82c832b44f0ec32a88a71 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Thu, 24 Jan 2019 17:47:44 -0800 Subject: [PATCH] Fix bug causing a supplied encoder to be used for generating an example but not for helping determine the correct Date formatting. --- .../JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift | 12 ++++----- .../JSONAPIEntityOpenAPITests.swift | 26 ++++++++++++++++--- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift b/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift index 9f283f6..abf2e6e 100644 --- a/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift +++ b/Sources/JSONAPIOpenAPI/OpenAPI/OpenAPITypes.swift @@ -16,12 +16,6 @@ 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. @@ -29,6 +23,12 @@ 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()) diff --git a/Tests/JSONAPIOpenAPITests/JSONAPIEntityOpenAPITests.swift b/Tests/JSONAPIOpenAPITests/JSONAPIEntityOpenAPITests.swift index e2289ba..a9e6b76 100644 --- a/Tests/JSONAPIOpenAPITests/JSONAPIEntityOpenAPITests.swift +++ b/Tests/JSONAPIOpenAPITests/JSONAPIEntityOpenAPITests.swift @@ -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)) @@ -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, @@ -268,6 +284,7 @@ extension JSONAPIEntityOpenAPITests { public struct Attributes: JSONAPI.Attributes, Sampleable { let stringProperty: Attribute let enumProperty: Attribute + let dateProperty: Attribute let optionalProperty: Attribute? let nullableProperty: Attribute let nullableOptionalProperty: Attribute? @@ -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)