-
Notifications
You must be signed in to change notification settings - Fork 132
Stop treating schema warnings as errors #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7b235ab
Stop treating schema warnings as errors
czechboy0 85f7ba6
Merge branch 'main' into hd-schema-warnings-not-errors
czechboy0 fa07903
Refactor + add a unit test
czechboy0 be1233f
Add one more unit test, one that actually throws an error
czechboy0 c0657b3
Also forward warnings from the validator to the diagnostics collector
czechboy0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// Runs validation steps on the incoming OpenAPI document. | ||
/// - Parameters: | ||
/// - doc: The OpenAPI document to validate. | ||
/// - config: The generator config. | ||
/// - Throws: An error if a fatal issue is found. | ||
func validateDoc(_ doc: ParsedOpenAPIRepresentation, config: Config) throws -> [Diagnostic] { | ||
guard config.featureFlags.contains(.strictOpenAPIValidation) else { | ||
return [] | ||
} | ||
// Run OpenAPIKit's built-in validation. | ||
// Pass `false` to `strict`, however, because we don't | ||
// want to turn schema loading warnings into errors. | ||
// We already propagate the warnings to the generator's | ||
// diagnostics, so they get surfaced to the user. | ||
// But the warnings are often too strict and should not | ||
// block the generator from running. | ||
// Validation errors continue to be fatal, such as | ||
// structural issues, like non-unique operationIds, etc. | ||
let warnings = try doc.validate(strict: false) | ||
let diagnostics: [Diagnostic] = warnings.map { warning in | ||
.warning( | ||
message: "Validation warning: \(warning.description)", | ||
context: [ | ||
"codingPath": warning.codingPathString ?? "<none>", | ||
"contextString": warning.contextString ?? "<none>", | ||
"subjectName": warning.subjectName ?? "<none>", | ||
] | ||
) | ||
} | ||
|
||
// Validate that the document is dereferenceable, which | ||
// catches reference cycles, which we don't yet support. | ||
_ = try doc.locallyDereferenced() | ||
|
||
// Also explicitly dereference the parts of components | ||
// that the generator uses. `locallyDereferenced()` above | ||
// only dereferences paths/operations, but not components. | ||
let components = doc.components | ||
try components.schemas.forEach { schema in | ||
_ = try schema.value.dereferenced(in: components) | ||
} | ||
try components.parameters.forEach { schema in | ||
_ = try schema.value.dereferenced(in: components) | ||
} | ||
try components.headers.forEach { schema in | ||
_ = try schema.value.dereferenced(in: components) | ||
} | ||
try components.requestBodies.forEach { schema in | ||
_ = try schema.value.dereferenced(in: components) | ||
} | ||
try components.responses.forEach { schema in | ||
_ = try schema.value.dereferenced(in: components) | ||
} | ||
return diagnostics | ||
} |
79 changes: 79 additions & 0 deletions
79
Tests/OpenAPIGeneratorCoreTests/Parser/Test_validateDoc.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
import XCTest | ||
import OpenAPIKit30 | ||
@testable import _OpenAPIGeneratorCore | ||
|
||
final class Test_validateDoc: Test_Core { | ||
|
||
func testSchemaWarningIsNotFatal() throws { | ||
let schemaWithWarnings = try loadSchemaFromYAML( | ||
#""" | ||
type: string | ||
items: | ||
type: integer | ||
"""# | ||
) | ||
let doc = OpenAPI.Document( | ||
info: .init(title: "Test", version: "1.0.0"), | ||
servers: [], | ||
paths: [:], | ||
components: .init(schemas: [ | ||
"myImperfectSchema": schemaWithWarnings | ||
]) | ||
) | ||
let diagnostics = try validateDoc( | ||
doc, | ||
config: .init( | ||
mode: .types, | ||
featureFlags: [ | ||
.strictOpenAPIValidation | ||
] | ||
) | ||
) | ||
XCTAssertEqual(diagnostics.count, 1) | ||
} | ||
|
||
func testStructuralWarningIsFatal() throws { | ||
let doc = OpenAPI.Document( | ||
info: .init(title: "Test", version: "1.0.0"), | ||
servers: [], | ||
paths: [ | ||
"/foo": .b( | ||
.init( | ||
get: .init( | ||
requestBody: nil, | ||
|
||
// Fatal error: missing at least one response. | ||
responses: [:] | ||
) | ||
) | ||
) | ||
], | ||
components: .noComponents | ||
) | ||
XCTAssertThrowsError( | ||
try validateDoc( | ||
doc, | ||
config: .init( | ||
mode: .types, | ||
featureFlags: [ | ||
.strictOpenAPIValidation | ||
] | ||
) | ||
) | ||
) | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.