mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
Fix bug causing a supplied encoder to be used for generating an example but not for helping determine the correct Date formatting.
This commit is contained in:
@@ -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())
|
||||
|
||||
@@ -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<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)
|
||||
|
||||
Reference in New Issue
Block a user