diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index cebd42bea..1a41f29e2 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -13,10 +13,6 @@ jobs: fail-fast: false matrix: image: - - swift:5.8-focal - - swift:5.8-jammy - - swift:5.9-focal - - swift:5.9-jammy - swift:5.10-focal - swift:5.10-jammy - swift:6.0-focal @@ -25,6 +21,8 @@ jobs: - swift:6.1-focal - swift:6.1-jammy - swift:6.1-noble + - swift:6.2-jammy + - swift:6.2-noble - swiftlang/swift:nightly-focal - swiftlang/swift:nightly-jammy container: ${{ matrix.image }} diff --git a/README.md b/README.md index a7c95c4fb..98278a4f3 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![sswg:sandbox|94x20](https://img.shields.io/badge/sswg-sandbox-lightgrey.svg)](https://github.com/swift-server/sswg/blob/master/process/incubation.md#sandbox-level) [![Swift 5.8+](http://img.shields.io/badge/Swift-5.8+-blue.svg)](https://swift.org) +[![sswg:sandbox|94x20](https://img.shields.io/badge/sswg-sandbox-lightgrey.svg)](https://github.com/swift-server/sswg/blob/master/process/incubation.md#sandbox-level) [![Swift 5.10+](http://img.shields.io/badge/Swift-5.10+-blue.svg)](https://swift.org) [![MIT license](http://img.shields.io/badge/license-MIT-lightgrey.svg)](http://opensource.org/licenses/MIT) ![Tests](https://github.com/mattpolzin/OpenAPIKit/workflows/Tests/badge.svg) diff --git a/Sources/OpenAPIKit/Document/Document.swift b/Sources/OpenAPIKit/Document/Document.swift index f7a3bde16..6e8ffdabe 100644 --- a/Sources/OpenAPIKit/Document/Document.swift +++ b/Sources/OpenAPIKit/Document/Document.swift @@ -430,23 +430,28 @@ extension OpenAPI.Document { /// specification releases a new patch version, OpenAPIKit will see a patch version release /// explicitly supports decoding documents of that new patch version before said version will /// succesfully decode as the `v3_1_x` case. - public enum Version: RawRepresentable, Equatable, Codable, Sendable { + public enum Version: RawRepresentable, Equatable, Comparable, Codable, Sendable { case v3_1_0 case v3_1_1 case v3_1_2 case v3_1_x(x: Int) + case v3_2_0 + case v3_2_x(x: Int) + public init?(rawValue: String) { switch rawValue { case "3.1.0": self = .v3_1_0 case "3.1.1": self = .v3_1_1 case "3.1.2": self = .v3_1_2 + case "3.2.0": self = .v3_2_0 default: let components = rawValue.split(separator: ".") guard components.count == 3 else { return nil } - guard components[0] == "3", components[1] == "1" else { + let minorVersion = components[1] + guard components[0] == "3", (minorVersion == "1" || minorVersion == "2") else { return nil } guard let patchVersion = Int(components[2], radix: 10) else { @@ -455,10 +460,17 @@ extension OpenAPI.Document { // to support newer versions released in the future without a breaking // change to the enumeration, bump the upper limit here to e.g. 2 or 3 // or 6: - guard patchVersion > 1 && patchVersion <= 2 else { - return nil + if minorVersion == "2" { + guard patchVersion > 0 && patchVersion <= 0 else { + return nil + } + self = .v3_2_x(x: patchVersion) + } else { + guard patchVersion > 2 && patchVersion <= 2 else { + return nil + } + self = .v3_1_x(x: patchVersion) } - self = .v3_1_x(x: patchVersion) } } @@ -468,6 +480,73 @@ extension OpenAPI.Document { case .v3_1_1: return "3.1.1" case .v3_1_2: return "3.1.2" case .v3_1_x(x: let x): return "3.1.\(x)" + + case .v3_2_0: return "3.2.0" + case .v3_2_x(x: let x): return "3.2.\(x)" + } + } + + public static func < (lhs: Self, rhs: Self) -> Bool { + switch lhs { + case .v3_1_0: + switch rhs { + case .v3_1_0: false + case .v3_1_1: true + case .v3_1_2: true + case .v3_1_x(x: let x): 0 < x + case .v3_2_0: true + case .v3_2_x(x: _): true + } + + case .v3_1_1: + switch rhs { + case .v3_1_0: false + case .v3_1_1: false + case .v3_1_2: true + case .v3_1_x(x: let y): 1 < y + case .v3_2_0: true + case .v3_2_x(x: _): true + } + + case .v3_1_2: + switch rhs { + case .v3_1_0: false + case .v3_1_1: false + case .v3_1_2: false + case .v3_1_x(x: let y): 2 < y + case .v3_2_0: true + case .v3_2_x(x: _): true + } + + case .v3_1_x(x: let x): + switch rhs { + case .v3_1_0: x < 0 + case .v3_1_1: x < 1 + case .v3_1_2: x < 2 + case .v3_1_x(x: let y): x < y + case .v3_2_0: true + case .v3_2_x(x: _): true + } + + case .v3_2_0: + switch rhs { + case .v3_1_0: false + case .v3_1_1: false + case .v3_1_2: false + case .v3_1_x(x: _): false + case .v3_2_0: false + case .v3_2_x(x: let y): 0 < y + } + + case .v3_2_x(x: let x): + switch rhs { + case .v3_1_0: false + case .v3_1_1: false + case .v3_1_2: false + case .v3_1_x(x: _): false + case .v3_2_0: x < 0 + case .v3_2_x(x: let y): x < y + } } } } diff --git a/Sources/OpenAPIKit/OpenAPIConditionalWarnings.swift b/Sources/OpenAPIKit/OpenAPIConditionalWarnings.swift new file mode 100644 index 000000000..ed4092da2 --- /dev/null +++ b/Sources/OpenAPIKit/OpenAPIConditionalWarnings.swift @@ -0,0 +1,55 @@ +public protocol Condition: Equatable, Sendable { + /// Given an entire OpenAPI Document, determine the applicability of the + /// condition. + func applies(to: OpenAPI.Document) -> Bool +} + +public protocol HasConditionalWarnings { + /// Warnings that only apply if the paired condition is met. + /// + /// Among other things, this allows OpenAPIKit to generate a warning in + /// some nested type that only applies if the OpenAPI Standards version of + /// the document is less than a certain version. + var conditionalWarnings: [(any Condition, OpenAPI.Warning)] { get } +} + +extension HasConditionalWarnings { + public func applicableConditionalWarnings(for subject: OpenAPI.Document) -> [OpenAPI.Warning] { + conditionalWarnings.compactMap { (condition, warning) in + guard condition.applies(to: subject) else { return nil } + + return warning + } + } +} + +internal struct DocumentVersionCondition: Sendable, Condition { + enum Comparator: Sendable { + case lessThan + case equal + case greaterThan + } + + let version: OpenAPI.Document.Version + let comparator: Comparator + + func applies(to document: OpenAPI.Document) -> Bool { + switch comparator { + case .lessThan: document.openAPIVersion < version + + case .equal: document.openAPIVersion == version + + case .greaterThan: document.openAPIVersion > version + } + } +} + +internal extension OpenAPI.Document { + struct ConditionalWarnings { + static func version(lessThan version: OpenAPI.Document.Version, doesNotSupport subject: String) -> (any Condition, OpenAPI.Warning) { + let warning = OpenAPI.Warning.message("\(subject) is only supported for OpenAPI document versions \(version.rawValue) and later") + + return (DocumentVersionCondition(version: version, comparator: .lessThan), warning) + } + } +} diff --git a/Sources/OpenAPIKit/Schema Object/JSONSchemaContext.swift b/Sources/OpenAPIKit/Schema Object/JSONSchemaContext.swift index 443a15b42..b340f8557 100644 --- a/Sources/OpenAPIKit/Schema Object/JSONSchemaContext.swift +++ b/Sources/OpenAPIKit/Schema Object/JSONSchemaContext.swift @@ -1042,7 +1042,7 @@ extension JSONSchema.CoreContext: Decodable { .underlyingError( GenericError( subjectName: "OpenAPI Schema", - details: "Found 'nullable' property. This property is not supported by OpenAPI v3.1.x. OpenAPIKit has translated it into 'type: [\"null\", ...]'.", + details: "Found 'nullable' property. This property is not supported by OpenAPI v3.1.x. OpenAPIKit has translated it into 'type: [\"null\", ...]'", codingPath: container.codingPath ) ) diff --git a/Sources/OpenAPIKit/Tag.swift b/Sources/OpenAPIKit/Tag.swift index 47393cecb..4a910bb3e 100644 --- a/Sources/OpenAPIKit/Tag.swift +++ b/Sources/OpenAPIKit/Tag.swift @@ -11,10 +11,20 @@ extension OpenAPI { /// OpenAPI Spec "Tag Object" /// /// See [OpenAPI Tag Object](https://spec.openapis.org/oas/v3.1.1.html#tag-object). - public struct Tag: Equatable, CodableVendorExtendable, Sendable { + public struct Tag: HasConditionalWarnings, CodableVendorExtendable, Sendable { public let name: String + /// Summary of the tag. Available for OAS 3.2.0 and greater. + public let summary: String? public let description: String? public let externalDocs: ExternalDocumentation? + /// The tag this tag is nested under. + public let parent: String? + /// A machine-readable string to categorize what sort of tag this is. + /// Any string value can be used, but some common options are provided + /// on OpenAPIKit's `Tag.Kind` type as static properties and more can + /// be found in the public registry: + /// https://spec.openapis.org/registry/tag-kind + public let kind: Kind? /// Dictionary of vendor extensions. /// @@ -23,17 +33,85 @@ extension OpenAPI { /// where the values are anything codable. public var vendorExtensions: [String: AnyCodable] + public let conditionalWarnings: [(any Condition, Warning)] + public init( name: String, + summary: String? = nil, description: String? = nil, externalDocs: ExternalDocumentation? = nil, + parent: String? = nil, + kind: Kind? = nil, vendorExtensions: [String: AnyCodable] = [:] ) { self.name = name + self.summary = summary self.description = description self.externalDocs = externalDocs + self.parent = parent + self.kind = kind self.vendorExtensions = vendorExtensions + + self.conditionalWarnings = [ + // If summary is non-nil, the document must be OAS version 3.2.0 or greater + nonNilVersionWarning(fieldName: "summary", value: summary, minimumVersion: .v3_2_0), + // If parent is non-nil, the document must be OAS version 3.2.0 or greater + nonNilVersionWarning(fieldName: "parent", value: parent, minimumVersion: .v3_2_0), + // If kind is non-nil, the document must be OAS version 3.2.0 or greater + nonNilVersionWarning(fieldName: "kind", value: kind, minimumVersion: .v3_2_0) + ].compactMap { $0 } + } + } +} + +extension OpenAPI.Tag { + public struct Kind : ExpressibleByStringLiteral, Codable, Equatable, Sendable { + public let rawValue: String + + public init(stringLiteral: String) { + self.rawValue = stringLiteral + } + + public init(from decoder: Decoder) throws { + let container = try decoder.singleValueContainer() + self.rawValue = try container.decode(String.self) } + + public func encode(to encoder: Encoder) throws { + var container = encoder.singleValueContainer() + + try container.encode(rawValue) + } + } +} + +extension OpenAPI.Tag.Kind { + /// See https://spec.openapis.org/registry/tag-kind/audience.html + public static let audience: OpenAPI.Tag.Kind = "audience" + /// See https://spec.openapis.org/registry/tag-kind/badge.html + public static let badge: OpenAPI.Tag.Kind = "badge" + /// See https://spec.openapis.org/registry/tag-kind/nav.html + public static let nav: OpenAPI.Tag.Kind = "nav" +} + +fileprivate func nonNilVersionWarning(fieldName: String, value: Subject?, minimumVersion: OpenAPI.Document.Version) -> (any Condition, OpenAPI.Warning)? { + value.map { _ in + OpenAPI.Document.ConditionalWarnings.version( + lessThan: minimumVersion, + doesNotSupport: "The Tag \(fieldName) field" + ) + } +} + +extension OpenAPI.Tag: Equatable { + public static func == (lhs: Self, rhs: Self) -> Bool { + return lhs.name == rhs.name + && lhs.summary == rhs.summary + && lhs.description == rhs.description + && lhs.externalDocs == rhs.externalDocs + && lhs.parent == rhs.parent + && lhs.kind == rhs.kind + && lhs.vendorExtensions == rhs.vendorExtensions } } @@ -43,15 +121,31 @@ extension OpenAPI.Tag: ExpressibleByStringLiteral { } } -// MARK: - Describable +// MARK: - Describable & Summarizable -extension OpenAPI.Tag : OpenAPIDescribable { +extension OpenAPI.Tag : OpenAPISummarizable { public func overriddenNonNil(description: String?) -> OpenAPI.Tag { guard let description = description else { return self } return OpenAPI.Tag( name: name, + summary: summary, + description: description, + externalDocs: externalDocs, + parent: parent, + kind: kind, + vendorExtensions: vendorExtensions + ) + } + + public func overriddenNonNil(summary: String?) -> OpenAPI.Tag { + guard let summary = summary else { return self } + return OpenAPI.Tag( + name: name, + summary: summary, description: description, externalDocs: externalDocs, + parent: parent, + kind: kind, vendorExtensions: vendorExtensions ) } @@ -65,10 +159,16 @@ extension OpenAPI.Tag: Encodable { try container.encode(name, forKey: .name) + try container.encodeIfPresent(summary, forKey: .summary) + try container.encodeIfPresent(description, forKey: .description) try container.encodeIfPresent(externalDocs, forKey: .externalDocs) + try container.encodeIfPresent(parent, forKey: .parent) + + try container.encodeIfPresent(kind, forKey: .kind) + if VendorExtensionsConfiguration.isEnabled(for: encoder) { try encodeExtensions(to: &container) } @@ -81,26 +181,47 @@ extension OpenAPI.Tag: Decodable { name = try container.decode(String.self, forKey: .name) + summary = try container.decodeIfPresent(String.self, forKey: .summary) + description = try container.decodeIfPresent(String.self, forKey: .description) externalDocs = try container.decodeIfPresent(OpenAPI.ExternalDocumentation.self, forKey: .externalDocs) + parent = try container.decodeIfPresent(String.self, forKey: .parent) + + kind = try container.decodeIfPresent(Kind.self, forKey: .kind) + vendorExtensions = try Self.extensions(from: decoder) + + conditionalWarnings = [ + // If summary is non-nil, the document must be OAS version 3.2.0 or greater + nonNilVersionWarning(fieldName: "summary", value: summary, minimumVersion: .v3_2_0), + // If parent is non-nil, the document must be OAS version 3.2.0 or greater + nonNilVersionWarning(fieldName: "parent", value: parent, minimumVersion: .v3_2_0), + // If kind is non-nil, the document must be OAS version 3.2.0 or greater + nonNilVersionWarning(fieldName: "kind", value: kind, minimumVersion: .v3_2_0) + ].compactMap { $0 } } } extension OpenAPI.Tag { internal enum CodingKeys: ExtendableCodingKey { case name + case summary case description case externalDocs + case parent + case kind case extended(String) static var allBuiltinKeys: [CodingKeys] { return [ .name, + .summary, .description, - .externalDocs + .externalDocs, + .parent, + .kind ] } @@ -112,10 +233,16 @@ extension OpenAPI.Tag { switch stringValue { case "name": self = .name + case "summary": + self = .summary case "description": self = .description case "externalDocs": self = .externalDocs + case "parent": + self = .parent + case "kind": + self = .kind default: self = .extendedKey(for: stringValue) } @@ -125,10 +252,16 @@ extension OpenAPI.Tag { switch self { case .name: return "name" + case .summary: + return "summary" case .description: return "description" case .externalDocs: return "externalDocs" + case .parent: + return "parent" + case .kind: + return "kind" case .extended(let key): return key } diff --git a/Sources/OpenAPIKit/Validator/Validation.swift b/Sources/OpenAPIKit/Validator/Validation.swift index 3e9851d42..b60d88f6f 100644 --- a/Sources/OpenAPIKit/Validator/Validation.swift +++ b/Sources/OpenAPIKit/Validator/Validation.swift @@ -128,10 +128,12 @@ public struct ValidationError: Swift.Error, CustomStringConvertible, PathContext public var localizedDescription: String { description } public var description: String { + let reasonStr: any StringProtocol = + reason.last == "." ? reason.dropLast() : reason guard !codingPath.isEmpty else { - return "\(reason) at root of document" + return "\(reasonStr) at root of document" } - return "\(reason) at path: \(codingPath.stringValue)" + return "\(reasonStr) at path: \(codingPath.stringValue)" } } diff --git a/Sources/OpenAPIKit/Validator/Validator.swift b/Sources/OpenAPIKit/Validator/Validator.swift index 21373a22d..49e1b8fdc 100644 --- a/Sources/OpenAPIKit/Validator/Validator.swift +++ b/Sources/OpenAPIKit/Validator/Validator.swift @@ -545,9 +545,15 @@ extension _Validator: SingleValueEncodingContainer { fileprivate func collectWarnings(from value: Encodable, atKey key: CodingKey? = nil) { let pathTail = key.map { [$0] } ?? [] + var localWarnings = [Warning]() if let warnable = value as? HasWarnings { - warnings += warnable.warnings.map(contextualize(at: codingPath + pathTail)) + localWarnings += warnable.warnings } + if let conditionalWarnable = value as? HasConditionalWarnings { + localWarnings += conditionalWarnable.applicableConditionalWarnings(for: document) + } + + warnings += localWarnings.map(contextualize(at: codingPath + pathTail)) } } diff --git a/Sources/OpenAPIKitCompat/Compat30To31.swift b/Sources/OpenAPIKitCompat/Compat30To31.swift index 88192c352..92928524a 100644 --- a/Sources/OpenAPIKitCompat/Compat30To31.swift +++ b/Sources/OpenAPIKitCompat/Compat30To31.swift @@ -430,8 +430,10 @@ extension OpenAPIKit30.OpenAPI.Tag: To31 { fileprivate func to31() -> OpenAPIKit.OpenAPI.Tag { OpenAPIKit.OpenAPI.Tag( name: name, + summary: nil, description: description, externalDocs: externalDocs?.to31(), + parent: nil, vendorExtensions: vendorExtensions ) } diff --git a/Sources/OpenAPIKitCore/Encoding and Decoding Errors And Warnings/OpenAPIWarning.swift b/Sources/OpenAPIKitCore/Encoding and Decoding Errors And Warnings/OpenAPIWarning.swift index 21aa92406..9ee5df67f 100644 --- a/Sources/OpenAPIKitCore/Encoding and Decoding Errors And Warnings/OpenAPIWarning.swift +++ b/Sources/OpenAPIKitCore/Encoding and Decoding Errors And Warnings/OpenAPIWarning.swift @@ -65,5 +65,6 @@ extension Warning: CustomStringConvertible { } public protocol HasWarnings { + /// Warnings generated while decoding an OpenAPI type. var warnings: [Warning] { get } } diff --git a/Tests/OpenAPIKitErrorReportingTests/SchemaErrorTests.swift b/Tests/OpenAPIKitErrorReportingTests/SchemaErrorTests.swift index deeb1f6cb..4aaca5ff4 100644 --- a/Tests/OpenAPIKitErrorReportingTests/SchemaErrorTests.swift +++ b/Tests/OpenAPIKitErrorReportingTests/SchemaErrorTests.swift @@ -76,7 +76,7 @@ final class SchemaErrorTests: XCTestCase { XCTAssertEqual(openAPIError.localizedDescription, """ - Problem encountered when parsing `OpenAPI Schema`: Found 'nullable' property. This property is not supported by OpenAPI v3.1.x. OpenAPIKit has translated it into 'type: ["null", ...]'.. at path: .paths['/hello/world'].get.responses.200.content['application/json'].schema + Problem encountered when parsing `OpenAPI Schema`: Found 'nullable' property. This property is not supported by OpenAPI v3.1.x. OpenAPIKit has translated it into 'type: ["null", ...]' at path: .paths['/hello/world'].get.responses.200.content['application/json'].schema """) XCTAssertEqual(openAPIError.codingPath.map { $0.stringValue }, [ "paths", diff --git a/Tests/OpenAPIKitTests/Document/DocumentTests.swift b/Tests/OpenAPIKitTests/Document/DocumentTests.swift index cec87dd52..88e462829 100644 --- a/Tests/OpenAPIKitTests/Document/DocumentTests.swift +++ b/Tests/OpenAPIKitTests/Document/DocumentTests.swift @@ -54,18 +54,39 @@ final class DocumentTests: XCTestCase { let t4 = OpenAPI.Document.Version.v3_1_x(x: 8) XCTAssertEqual(t4.rawValue, "3.1.8") - let t5 = OpenAPI.Document.Version(rawValue: "3.1.0") - XCTAssertEqual(t5, .v3_1_0) + let t5 = OpenAPI.Document.Version.v3_2_0 + XCTAssertEqual(t5.rawValue, "3.2.0") - let t6 = OpenAPI.Document.Version(rawValue: "3.1.1") - XCTAssertEqual(t6, .v3_1_1) + let t6 = OpenAPI.Document.Version(rawValue: "3.1.0") + XCTAssertEqual(t6, .v3_1_0) - let t7 = OpenAPI.Document.Version(rawValue: "3.1.2") - XCTAssertEqual(t7, .v3_1_2) + let t7 = OpenAPI.Document.Version(rawValue: "3.1.1") + XCTAssertEqual(t7, .v3_1_1) + + let t8 = OpenAPI.Document.Version(rawValue: "3.1.2") + XCTAssertEqual(t8, .v3_1_2) // not a known version: - let t8 = OpenAPI.Document.Version(rawValue: "3.1.8") - XCTAssertNil(t8) + let t9 = OpenAPI.Document.Version(rawValue: "3.1.8") + XCTAssertNil(t9) + + let t10 = OpenAPI.Document.Version(rawValue: "3.2.8") + XCTAssertNil(t10) + } + + func test_compareOASVersions() { + let versions: [OpenAPI.Document.Version] = [ + .v3_1_0, + .v3_1_1, + .v3_1_2, + .v3_2_0 + ] + + for v1Idx in 0...(versions.count - 2) { + for v2Idx in (v1Idx + 1)...(versions.count - 1) { + XCTAssert(versions[v1Idx] < versions[v2Idx]) + } + } } func test_getRoutes() { diff --git a/Tests/OpenAPIKitTests/TagTests.swift b/Tests/OpenAPIKitTests/TagTests.swift index 52251a3de..892aa2951 100644 --- a/Tests/OpenAPIKitTests/TagTests.swift +++ b/Tests/OpenAPIKitTests/TagTests.swift @@ -11,12 +11,16 @@ import OpenAPIKit final class TagTests: XCTestCase { func test_init() { let t1 = OpenAPI.Tag(name: "hello") + XCTAssertNil(t1.summary) XCTAssertNil(t1.description) XCTAssertNil(t1.externalDocs) + XCTAssertEqual(t1.conditionalWarnings.count, 0) - let t2 = OpenAPI.Tag(name: "hello", description: "world") + let t2 = OpenAPI.Tag(name: "hello", summary: "hi", description: "world") + XCTAssertEqual(t2.summary, "hi") XCTAssertEqual(t2.description, "world") XCTAssertNil(t2.externalDocs) + XCTAssertEqual(t2.conditionalWarnings.count, 1) let t3 = OpenAPI.Tag( name: "hello", @@ -28,11 +32,25 @@ final class TagTests: XCTestCase { let t4 = OpenAPI.Tag( name: "tag", + summary: "first", description: "orig" ).overriddenNonNil(description: "new") - .overriddenNonNil(summary: "no-op") + .overriddenNonNil(summary: "cool") .overriddenNonNil(description: nil) // no effect + XCTAssertEqual(t4.summary, "cool") XCTAssertEqual(t4.description, "new") + + let t5 = OpenAPI.Tag( + name: "hello", + parent: "otherTag" + ) + XCTAssertEqual(t5.parent, "otherTag") + + let t6 = OpenAPI.Tag( + name: "hello", + kind: .nav + ) + XCTAssertEqual(t6.kind, .nav) } } @@ -63,6 +81,40 @@ extension TagTests { let tag = try orderUnstableDecode(OpenAPI.Tag.self, from: tagData) XCTAssertEqual(tag, OpenAPI.Tag(name: "hello")) + XCTAssertEqual(tag.conditionalWarnings.count, 0) + } + + func test_nameAndSummary_encode() throws { + let tag = OpenAPI.Tag( + name: "hello", + summary: "world" + ) + let encodedTag = try orderUnstableTestStringFromEncoding(of: tag) + + assertJSONEquivalent( + encodedTag, + """ + { + "name" : "hello", + "summary" : "world" + } + """ + ) + } + + func test_nameAndSummary_decode() throws { + let tagData = + """ + { + "name": "hello", + "summary": "world" + } + """.data(using: .utf8)! + + let tag = try orderUnstableDecode(OpenAPI.Tag.self, from: tagData) + + XCTAssertEqual(tag, OpenAPI.Tag(name: "hello", summary: "world")) + XCTAssertEqual(tag.conditionalWarnings.count, 1) } func test_nameAndDescription_encode() throws { @@ -95,15 +147,85 @@ extension TagTests { let tag = try orderUnstableDecode(OpenAPI.Tag.self, from: tagData) XCTAssertEqual(tag, OpenAPI.Tag(name: "hello", description: "world")) + XCTAssertEqual(tag.conditionalWarnings.count, 0) + } + + func test_nameAndParent_encode() throws { + let tag = OpenAPI.Tag( + name: "hello", + parent: "otherTag" + ) + let encodedTag = try orderUnstableTestStringFromEncoding(of: tag) + + assertJSONEquivalent( + encodedTag, + """ + { + "name" : "hello", + "parent" : "otherTag" + } + """ + ) + } + + func test_nameAndParent_decode() throws { + let tagData = + """ + { + "name": "hello", + "parent": "otherTag" + } + """.data(using: .utf8)! + + let tag = try orderUnstableDecode(OpenAPI.Tag.self, from: tagData) + + XCTAssertEqual(tag, OpenAPI.Tag(name: "hello", parent: "otherTag")) + XCTAssertEqual(tag.conditionalWarnings.count, 1) + } + + func test_nameAndKind_encode() throws { + let tag = OpenAPI.Tag( + name: "hello", + kind: .badge + ) + let encodedTag = try orderUnstableTestStringFromEncoding(of: tag) + + assertJSONEquivalent( + encodedTag, + """ + { + "kind" : "badge", + "name" : "hello" + } + """ + ) + } + + func test_nameAndKind_decode() throws { + let tagData = + """ + { + "name": "hello", + "kind": "audience" + } + """.data(using: .utf8)! + + let tag = try orderUnstableDecode(OpenAPI.Tag.self, from: tagData) + + XCTAssertEqual(tag, OpenAPI.Tag(name: "hello", kind: .audience)) + XCTAssertEqual(tag.conditionalWarnings.count, 1) } func test_allFields_encode() throws { let tag = OpenAPI.Tag( name: "hello", + summary: "sum", description: "world", externalDocs: .init( url: URL(string: "http://google.com")! ), + parent: "otherTag", + kind: "mytag", vendorExtensions: ["x-specialFeature": false] ) let encodedTag = try orderUnstableTestStringFromEncoding(of: tag) @@ -116,7 +238,10 @@ extension TagTests { "externalDocs" : { "url" : "http:\\/\\/google.com" }, + "kind" : "mytag", "name" : "hello", + "parent" : "otherTag", + "summary" : "sum", "x-specialFeature" : false } """ @@ -128,10 +253,13 @@ extension TagTests { """ { "name": "hello", + "summary": "sum", "description": "world", "externalDocs": { "url": "http://google.com" }, + "parent": "otherTag", + "kind": "mytag", "x-specialFeature" : false } """.data(using: .utf8)! @@ -142,10 +270,14 @@ extension TagTests { tag, OpenAPI.Tag( name: "hello", + summary: "sum", description: "world", externalDocs: .init(url: URL(string: "http://google.com")!), + parent: "otherTag", + kind: "mytag", vendorExtensions: ["x-specialFeature": false] ) ) + XCTAssertEqual(tag.conditionalWarnings.count, 3) } } diff --git a/Tests/OpenAPIKitTests/Validator/ValidatorTests.swift b/Tests/OpenAPIKitTests/Validator/ValidatorTests.swift index e5ed9f59f..2278b8974 100644 --- a/Tests/OpenAPIKitTests/Validator/ValidatorTests.swift +++ b/Tests/OpenAPIKitTests/Validator/ValidatorTests.swift @@ -1484,9 +1484,68 @@ final class ValidatorTests: XCTestCase { XCTAssertEqual(errors?.values.count, 1) XCTAssertEqual( errors?.localizedDescription, - "Problem encountered when parsing ``: \'gzip\' could not be parsed as a Content Type. Content Types should have the format \'/\'. at path: .paths[\'/test\'].get.responses.200.content" + "Problem encountered when parsing ``: \'gzip\' could not be parsed as a Content Type. Content Types should have the format \'/\' at path: .paths[\'/test\'].get.responses.200.content" ) XCTAssertEqual(errors?.values.first?.codingPathString, ".paths[\'/test\'].get.responses.200.content") } } + + func test_collectsConditionalTagWarningNotStrict() throws { + let docData = """ + { + "info": {"title": "test", "version": "1.0"}, + "openapi": "3.1.0", + "tags": [ {"name": "hi", "summary": "sum"} ] + } + """.data(using: .utf8)! + + let doc = try orderUnstableDecode(OpenAPI.Document.self, from: docData) + + XCTAssertEqual( + doc.tags?.first?.applicableConditionalWarnings(for: doc).first?.localizedDescription, + "The Tag summary field is only supported for OpenAPI document versions 3.2.0 and later" + ) + + let warnings = try doc.validate(strict: false) + + XCTAssertEqual(warnings.count, 1) + XCTAssertEqual( + warnings.first?.localizedDescription, + "Problem encountered when parsing ``: The Tag summary field is only supported for OpenAPI document versions 3.2.0 and later." + ) + XCTAssertEqual(warnings.first?.codingPathString, ".tags[0]") + + // now test that the warning does not apply for v3.2.0 and above + var doc2 = doc + doc2.openAPIVersion = .v3_2_0 + + try XCTAssertEqual(doc2.validate(strict: false).count, 0) + } + + func test_collectsConditionalTagWarningStrict() throws { + let docData = """ + { + "info": {"title": "test", "version": "1.0"}, + "openapi": "3.1.0", + "tags": [ {"name": "hi", "summary": "sum"} ] + } + """.data(using: .utf8)! + + let doc = try orderUnstableDecode(OpenAPI.Document.self, from: docData) + + XCTAssertEqual( + doc.tags?.first?.applicableConditionalWarnings(for: doc).first?.localizedDescription, + "The Tag summary field is only supported for OpenAPI document versions 3.2.0 and later" + ) + + XCTAssertThrowsError(try doc.validate(strict: true)) { error in + let errors = error as? ValidationErrorCollection + XCTAssertEqual(errors?.values.count, 1) + XCTAssertEqual( + errors?.localizedDescription, + "Problem encountered when parsing ``: The Tag summary field is only supported for OpenAPI document versions 3.2.0 and later at path: .tags[0]" + ) + XCTAssertEqual(errors?.values.first?.codingPathString, ".tags[0]") + } + } }