Skip to content

Commit 259f7b1

Browse files
authored
Merge pull request #414 from true-false-maybe/add-prefixItems
add prefixItems to array
2 parents a75aa41 + 9d9686f commit 259f7b1

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

Sources/OpenAPIKit/Schema Object/JSONSchema.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import OpenAPIKitCore
99

1010
/// OpenAPI "Schema Object"
11-
///
11+
///
1212
/// See [OpenAPI Schema Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#schema-object).
1313
public struct JSONSchema: JSONSchemaContext, HasWarnings {
1414

@@ -475,7 +475,7 @@ extension JSONSchema: VendorExtendable {
475475

476476
public func with(vendorExtensions: [String: AnyCodable]) -> JSONSchema {
477477
.init(
478-
warnings: warnings,
478+
warnings: warnings,
479479
schema: value.with(vendorExtensions: vendorExtensions)
480480
)
481481
}
@@ -1634,6 +1634,7 @@ extension JSONSchema {
16341634
minItems: Int? = nil,
16351635
maxItems: Int? = nil,
16361636
uniqueItems: Bool? = nil,
1637+
prefixItems: [JSONSchema]? = nil,
16371638
items: JSONSchema? = nil,
16381639
allowedValues: [AnyCodable]? = nil,
16391640
defaultValue: AnyCodable? = nil,
@@ -1664,6 +1665,7 @@ extension JSONSchema {
16641665
items: items,
16651666
maxItems: maxItems,
16661667
minItems: minItems,
1668+
prefixItems: prefixItems,
16671669
uniqueItems: uniqueItems
16681670
)
16691671
return .array(coreContext, arrayContext)
@@ -2149,22 +2151,22 @@ extension JSONSchema: Decodable {
21492151

21502152
self.value = value.with(vendorExtensions: extensions)
21512153
}
2152-
2154+
21532155
private static func decodeVenderExtensions(from decoder: Decoder) throws -> [String: AnyCodable] {
21542156
guard VendorExtensionsConfiguration.isEnabled else {
21552157
return [:]
21562158
}
2157-
2159+
21582160
let decoded = try AnyCodable(from: decoder).value
2159-
2161+
21602162
guard (decoded as? [Any]) == nil else {
21612163
throw VendorExtensionDecodingError.selfIsArrayNotDict
21622164
}
2163-
2165+
21642166
guard let decodedAny = decoded as? [String: Any] else {
21652167
throw VendorExtensionDecodingError.foundNonStringKeys
21662168
}
2167-
2169+
21682170
return decodedAny
21692171
.filter { $0.key.lowercased().starts(with: "x-") }
21702172
.mapValues(AnyCodable.init)

Sources/OpenAPIKit/Schema Object/JSONSchemaContext.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// JSONSchemaContext.swift
3-
//
3+
//
44
//
55
// Created by Mathew Polzin on 6/22/19.
66
//
@@ -334,7 +334,7 @@ extension JSONSchema.CoreContext: Equatable {
334334
&& lhs.externalDocs == rhs.externalDocs
335335
&& lhs.discriminator == rhs.discriminator
336336

337-
return step1
337+
return step1
338338
&& lhs.allowedValues == rhs.allowedValues
339339
&& lhs.defaultValue == rhs.defaultValue
340340
&& lhs.examples == rhs.examples
@@ -725,6 +725,11 @@ extension JSONSchema {
725725
/// Defaults to 0.
726726
public var minItems: Int { _minItems ?? 0 }
727727

728+
/// Multiple JSON Type Node that describe
729+
/// the types of the first few elements in the array.
730+
/// Useful for describing tuples.
731+
public let prefixItems: [JSONSchema]?
732+
728733
let _uniqueItems: Bool?
729734
/// Setting to true indicates all
730735
/// elements of the array are expected
@@ -735,11 +740,13 @@ extension JSONSchema {
735740
items: JSONSchema? = nil,
736741
maxItems: Int? = nil,
737742
minItems: Int? = nil,
743+
prefixItems: [JSONSchema]? = nil,
738744
uniqueItems: Bool? = nil
739745
) {
740746
self.items = items
741747
self.maxItems = maxItems
742748
self._minItems = minItems
749+
self.prefixItems = prefixItems
743750
self._uniqueItems = uniqueItems
744751
}
745752
}
@@ -1056,7 +1063,6 @@ extension JSONSchema.CoreContext: Decodable {
10561063
)
10571064
)
10581065
)
1059-
10601066
}
10611067
else if let types = try? container.decodeIfPresent([JSONType].self, forKey: .type) {
10621068
nullable = types.contains(JSONType.null)
@@ -1209,6 +1215,7 @@ extension JSONSchema.ArrayContext {
12091215
case maxItems
12101216
case minItems
12111217
case uniqueItems
1218+
case prefixItems
12121219
}
12131220
}
12141221

@@ -1219,6 +1226,7 @@ extension JSONSchema.ArrayContext: Encodable {
12191226
try container.encodeIfPresent(items, forKey: .items)
12201227
try container.encodeIfPresent(maxItems, forKey: .maxItems)
12211228
try container.encodeIfPresent(_minItems, forKey: .minItems)
1229+
try container.encodeIfPresent(prefixItems, forKey: .prefixItems)
12221230
try container.encodeIfPresent(_uniqueItems, forKey: .uniqueItems)
12231231
}
12241232
}
@@ -1230,6 +1238,7 @@ extension JSONSchema.ArrayContext: Decodable {
12301238
items = try container.decodeIfPresent(JSONSchema.self, forKey: .items)
12311239
maxItems = try container.decodeIfPresent(Int.self, forKey: .maxItems)
12321240
_minItems = try container.decodeIfPresent(Int.self, forKey: .minItems)
1241+
prefixItems = try container.decodeIfPresent([JSONSchema].self, forKey: .prefixItems)
12331242
_uniqueItems = try container.decodeIfPresent(Bool.self, forKey: .uniqueItems)
12341243
}
12351244
}

Tests/OpenAPIKitTests/Schema Object/SchemaFragmentTests.swift

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// SchemaFragmentTests.swift
3-
//
3+
//
44
//
55
// Created by Mathew Polzin on 4/21/20.
66
//
@@ -663,6 +663,30 @@ extension SchemaFragmentTests {
663663
}
664664
"""
665665
)
666+
667+
let t5 = JSONSchema.array(.init(), .init(items: .string, prefixItems: [.integer, .boolean]))
668+
669+
let encoded5 = try orderUnstableTestStringFromEncoding(of: t5)
670+
671+
assertJSONEquivalent(
672+
encoded5,
673+
"""
674+
{
675+
"items" : {
676+
"type" : "string"
677+
},
678+
"prefixItems" : [
679+
{
680+
"type" : "integer"
681+
},
682+
{
683+
"type" : "boolean"
684+
}
685+
],
686+
"type" : "array"
687+
}
688+
"""
689+
)
666690
}
667691

668692
func test_arrayDecode() throws {
@@ -724,6 +748,24 @@ extension SchemaFragmentTests {
724748
let decoded5 = try orderUnstableDecode(JSONSchema.self, from: t5)
725749

726750
XCTAssertEqual(decoded5, JSONSchema.array(.init(), .init(items: .string)))
751+
752+
let t6 =
753+
"""
754+
{
755+
"items" : {
756+
"type" : "string"
757+
},
758+
"prefixItems": [
759+
{
760+
"type" : "integer"
761+
}
762+
]
763+
}
764+
""".data(using: .utf8)!
765+
766+
let decoded6 = try orderUnstableDecode(JSONSchema.self, from: t6)
767+
768+
XCTAssertEqual(decoded6, JSONSchema.array(.init(), .init(items: .string, prefixItems: [.integer])))
727769
}
728770

729771
func test_objectEncode() throws {

0 commit comments

Comments
 (0)