Skip to content

Commit 7843345

Browse files
committed
prioritize a schema guess based on protocol conformance over a schema guess based on mirroring.
1 parent fd9d0a3 commit 7843345

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

Sources/OpenAPIKit/JSON Utility/Sampleable+OpenAPI.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ extension Sampleable where Self: Encodable {
2323

2424
public func genericOpenAPISchemaGuess<T>(for value: T, using encoder: JSONEncoder) throws -> JSONSchema {
2525

26+
if let schema = try openAPISchemaGuess(for: value, using: encoder) {
27+
return schema
28+
}
29+
2630
let mirror = Mirror(reflecting: value)
2731
let properties: [(String, JSONSchema)] = try mirror.children.compactMap { child in
2832

@@ -55,17 +59,12 @@ public func genericOpenAPISchemaGuess<T>(for value: T, using encoder: JSONEncode
5559
throw OpenAPITypeError.unknownNodeType(type(of: value))
5660
}
5761

58-
let propertylessSchemaGuess: JSONSchema? = properties.count == 0
59-
? try openAPISchemaGuess(for: value, using: encoder)
60-
: nil
61-
6262
// There should not be any duplication of keys since these are
6363
// property names, but rather than risk runtime exception, we just
6464
// fail to the newer value arbitrarily
6565
let propertiesDict = Dictionary(properties) { _, value2 in value2 }
6666

67-
return propertylessSchemaGuess ??
68-
.object(.init(format: .generic,
67+
return .object(.init(format: .generic,
6968
required: true),
7069
.init(properties: propertiesDict))
7170
}
@@ -158,7 +157,7 @@ internal func openAPISchemaGuess(for value: Any, using encoder: JSONEncoder) thr
158157
required: true),
159158
.init())
160159

161-
case is Date:
160+
case is DateOpenAPISchemaType:
162161
// we don't know what Date will end up looking like without
163162
// trying it out. Most likely a `.string` or `.number(format: .double)`
164163
return try OpenAPIKit.reencodedSchemaGuess(for: Date(), using: encoder)

0 commit comments

Comments
 (0)