Skip to content

Commit

Permalink
Finish re-introducing the optional validation test for paths count. U…
Browse files Browse the repository at this point in the history
…pdate documentation around the changes.
  • Loading branch information
mattpolzin committed Sep 4, 2023
1 parent 942ea4a commit f5879f0
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 46 deletions.
4 changes: 2 additions & 2 deletions Sources/OpenAPIKit/Validator/Validation+Builtins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ extension Validation {
///
/// The OpenAPI Specifcation does not require that the document
/// contain any paths for [security reasons](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#security-filtering)
/// but documentation that is public in nature might only ever have
/// an empty `PathItem.Map` in error.
/// or even because it only contains webhooks, but authors may still
/// want to protect against an empty `PathItem.Map` in some cases.
///
/// - Important: This is not an included validation by default.
public static var documentContainsPaths: Validation<OpenAPI.Document> {
Expand Down
1 change: 1 addition & 0 deletions Sources/OpenAPIKit/Validator/Validator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ extension OpenAPI.Document {
///
/// let document = OpenAPI.Document(...)
/// let validator = Validator()
/// .validating(.documentContainsPaths)
/// .validating(.pathsContainOperations)
/// try document.validate(using: validator)
///
Expand Down
12 changes: 0 additions & 12 deletions Tests/OpenAPIKitErrorReportingTests/PathsErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@ import OpenAPIKit
import Yams

final class PathsErrorTests: XCTestCase {
func test_missingPaths() {
let documentYML =
"""
openapi: "3.1.0"
info:
title: test
version: 1.0
"""

XCTAssertNoThrow(try testDecoder.decode(OpenAPI.Document.self, from: documentYML))
}

func test_badPathReference() {
let documentYML =
"""
Expand Down
32 changes: 0 additions & 32 deletions Tests/OpenAPIKitTests/Document/DocumentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1052,44 +1052,28 @@ extension DocumentTests {
"webhooks" : {
"webhook-test" : {
"delete" : {
"responses" : {
}
},
"get" : {
"responses" : {
}
},
"head" : {
"responses" : {
}
},
"options" : {
"responses" : {
}
},
"patch" : {
"responses" : {
}
},
"post" : {
"responses" : {
}
},
"put" : {
"responses" : {
}
},
"trace" : {
"responses" : {
}
}
}
}
Expand All @@ -1114,36 +1098,20 @@ extension DocumentTests {
"webhooks": {
"webhook-test": {
"delete": {
"responses": {
}
},
"get": {
"responses": {
}
},
"head": {
"responses": {
}
},
"options": {
"responses": {
}
},
"patch": {
"responses": {
}
},
"post": {
"responses": {
}
},
"put": {
"responses": {
}
},
"trace": {
"responses": {
}
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions Tests/OpenAPIKitTests/Validator/BuiltinValidationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,37 @@ import OpenAPIKit

final class BuiltinValidationTests: XCTestCase {

func test_noPathsOnDocumentFails() {
let document = OpenAPI.Document(
info: .init(title: "test", version: "1.0"),
servers: [],
paths: [:],
components: .noComponents
)

let validator = Validator.blank.validating(.documentContainsPaths)

XCTAssertThrowsError(try document.validate(using: validator)) { error in
let error = error as? ValidationErrorCollection
XCTAssertEqual(error?.values.first?.reason, "Failed to satisfy: Document contains at least one path")
XCTAssertEqual(error?.values.first?.codingPath.map { $0.stringValue }, [])
}
}

func test_onePathOnDocumentSucceeds() throws {
let document = OpenAPI.Document(
info: .init(title: "test", version: "1.0"),
servers: [],
paths: [
"/hello/world": .init()
],
components: .noComponents
)

let validator = Validator.blank.validating(.documentContainsPaths)
try document.validate(using: validator)
}

func test_noOperationsOnPathItemFails() {
let document = OpenAPI.Document(
info: .init(title: "test", version: "1.0"),
Expand Down
1 change: 1 addition & 0 deletions documentation/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ Here's a table of Array/Map types for which this quirk is relevant and which mod
| `OpenAPI.Components.securitySchemes` | `ComponentDictionary` | x | x |
| `OpenAPI.Document.components` | `Components` | x | x |
| `OpenAPI.Document.security` | `[SecurityRequirement]` | x | x |
| `OpenAPI.Document.paths` | `PathItem.Map` | | x |
| `OpenAPI.Document.servers` | `[Server]` | x | x |
| `OpenAPI.Document.webhooks` | `OrderedDictionary` | | x |
| `OpenAPI.Link.parameters` | `OrderedDictionary` | x | x |
Expand Down

0 comments on commit f5879f0

Please sign in to comment.