Skip to content

Commit 38a0264

Browse files
committed
Add OAS 3.2.0 warning for PathItems with query endpoints
1 parent d092c12 commit 38a0264

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

Sources/OpenAPIKit/Path Item/PathItem.swift

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extension OpenAPI {
2323
///
2424
/// You can access an array of equatable `HttpMethod`/`Operation` paris with the
2525
/// `endpoints` property.
26-
public struct PathItem: Equatable, CodableVendorExtendable, Sendable {
26+
public struct PathItem: HasConditionalWarnings, CodableVendorExtendable, Sendable {
2727
public var summary: String?
2828
public var description: String?
2929
public var servers: [OpenAPI.Server]?
@@ -64,6 +64,12 @@ extension OpenAPI {
6464
/// where the values are anything codable.
6565
public var vendorExtensions: [String: AnyCodable]
6666

67+
/// Warnings that apply conditionally depending on the OpenAPI Document
68+
/// the PathItem belongs to.
69+
///
70+
/// Check these with the `applicableConditionalWarnings(for:)` method.
71+
public let conditionalWarnings: [(any Condition, OpenAPI.Warning)]
72+
6773
public init(
6874
summary: String? = nil,
6975
description: String? = nil,
@@ -95,6 +101,11 @@ extension OpenAPI {
95101
self.trace = trace
96102
self.query = query
97103
self.vendorExtensions = vendorExtensions
104+
105+
self.conditionalWarnings = [
106+
// If query is non-nil, the document must be OAS version 3.2.0 or greater
107+
nonNilVersionWarning(fieldName: "query", value: query, minimumVersion: .v3_2_0)
108+
].compactMap { $0 }
98109
}
99110

100111
/// Set the `GET` endpoint operation.
@@ -144,6 +155,34 @@ extension OpenAPI {
144155
}
145156
}
146157

158+
extension OpenAPI.PathItem: Equatable {
159+
public static func == (lhs: Self, rhs: Self) -> Bool {
160+
lhs.summary == rhs.summary
161+
&& lhs.description == rhs.description
162+
&& lhs.servers == rhs.servers
163+
&& lhs.parameters == rhs.parameters
164+
&& lhs.get == rhs.get
165+
&& lhs.put == rhs.put
166+
&& lhs.post == rhs.post
167+
&& lhs.delete == rhs.delete
168+
&& lhs.options == rhs.options
169+
&& lhs.head == rhs.head
170+
&& lhs.patch == rhs.patch
171+
&& lhs.trace == rhs.trace
172+
&& lhs.query == rhs.query
173+
&& lhs.vendorExtensions == rhs.vendorExtensions
174+
}
175+
}
176+
177+
fileprivate func nonNilVersionWarning<Subject>(fieldName: String, value: Subject?, minimumVersion: OpenAPI.Document.Version) -> (any Condition, OpenAPI.Warning)? {
178+
value.map { _ in
179+
OpenAPI.Document.ConditionalWarnings.version(
180+
lessThan: minimumVersion,
181+
doesNotSupport: "The PathItem \(fieldName) field"
182+
)
183+
}
184+
}
185+
147186
extension OpenAPI.PathItem {
148187
public typealias Map = OrderedDictionary<OpenAPI.Path, Either<OpenAPI.Reference<OpenAPI.PathItem>, OpenAPI.PathItem>>
149188
}
@@ -300,6 +339,11 @@ extension OpenAPI.PathItem: Decodable {
300339
query = try container.decodeIfPresent(OpenAPI.Operation.self, forKey: .query)
301340

302341
vendorExtensions = try Self.extensions(from: decoder)
342+
343+
self.conditionalWarnings = [
344+
// If query is non-nil, the document must be OAS version 3.2.0 or greater
345+
nonNilVersionWarning(fieldName: "query", value: query, minimumVersion: .v3_2_0)
346+
].compactMap { $0 }
303347
} catch let error as DecodingError {
304348

305349
throw OpenAPI.Error.Decoding.Path(error)

Sources/OpenAPIKit/Tag.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ extension OpenAPI {
3333
/// where the values are anything codable.
3434
public var vendorExtensions: [String: AnyCodable]
3535

36+
/// Warnings that apply conditionally depending on the OpenAPI Document
37+
/// the Tag belongs to.
38+
///
39+
/// Check these with the `applicableConditionalWarnings(for:)` method.
3640
public let conditionalWarnings: [(any Condition, Warning)]
3741

3842
public init(

0 commit comments

Comments
 (0)