Skip to content

Commit ace1ef3

Browse files
committed
change implementation to not support decoding unknown new OAS versions
1 parent dca99b7 commit ace1ef3

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

Sources/OpenAPIKit/Document/Document.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,12 @@ extension OpenAPI.Document {
424424
/// OpenAPIKit only explicitly supports versions that can be found in
425425
/// this enum. Other versions may or may not be decodable by
426426
/// OpenAPIKit to a certain extent.
427+
///
428+
///**IMPORTANT**: Although the `v3_1_x` case supports arbitrary
429+
/// patch versions, only _known_ patch versions are decodable. That is, if the OpenAPI
430+
/// specification releases a new patch version, OpenAPIKit will see a patch version release
431+
/// explicitly supports decoding documents of that new patch version before said version will
432+
/// succesfully decode as the `v3_1_x` case.
427433
public enum Version: RawRepresentable, Equatable, Codable {
428434
case v3_1_0
429435
case v3_1_1
@@ -444,6 +450,12 @@ extension OpenAPI.Document {
444450
guard let patchVersion = Int(components[2], radix: 10) else {
445451
return nil
446452
}
453+
// to support newer versions released in the future without a breaking
454+
// change to the enumeration, bump the upper limit here to e.g. 2 or 3
455+
// or 6:
456+
guard patchVersion > 1 && patchVersion <= 1 else {
457+
return nil
458+
}
447459
self = .v3_1_x(x: patchVersion)
448460
}
449461
}

Sources/OpenAPIKit30/Document/Document.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,13 @@ extension OpenAPI.Document {
408408
/// OpenAPIKit only explicitly supports versions that can be found in
409409
/// this enum. Other versions may or may not be decodable by
410410
/// OpenAPIKit to a certain extent.
411-
public enum Version: RawRepresentable, Equatable, Codable {
411+
///
412+
///**IMPORTANT**: Although the `v3_0_x` case supports arbitrary
413+
/// patch versions, only _known_ patch versions are decodable. That is, if the OpenAPI
414+
/// specification releases a new patch version, OpenAPIKit will see a patch version release
415+
/// explicitly supports decoding documents of that new patch version before said version will
416+
/// succesfully decode as the `v3_0_x` case.
417+
public enum Version: RawRepresentable, Equatable, Codable {
412418
case v3_0_0
413419
case v3_0_1
414420
case v3_0_2
@@ -434,6 +440,12 @@ extension OpenAPI.Document {
434440
guard let patchVersion = Int(components[2], radix: 10) else {
435441
return nil
436442
}
443+
// to support newer versions released in the future without a breaking
444+
// change to the enumeration, bump the upper limit here to e.g. 5 or 6
445+
// or 9:
446+
guard patchVersion > 4 && patchVersion <= 4 else {
447+
return nil
448+
}
437449
self = .v3_0_x(x: patchVersion)
438450
}
439451
}

Tests/OpenAPIKit30Tests/Document/DocumentTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ final class DocumentTests: XCTestCase {
7575
let t11 = OpenAPI.Document.Version(rawValue: "3.0.4")
7676
XCTAssertEqual(t11, .v3_0_4)
7777

78+
// not a known version:
7879
let t12 = OpenAPI.Document.Version(rawValue: "3.0.8")
79-
XCTAssertEqual(t12, .v3_0_x(x: 8))
80+
XCTAssertNil(t12)
8081
}
8182

8283
func test_getRoutes() {

Tests/OpenAPIKitTests/Document/DocumentTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ final class DocumentTests: XCTestCase {
5757
let t5 = OpenAPI.Document.Version(rawValue: "3.1.1")
5858
XCTAssertEqual(t5, .v3_1_1)
5959

60+
// not a known version:
6061
let t6 = OpenAPI.Document.Version(rawValue: "3.1.8")
61-
XCTAssertEqual(t6, .v3_1_x(x: 8))
62+
XCTAssertNil(t6)
6263
}
6364

6465
func test_getRoutes() {

0 commit comments

Comments
 (0)