Skip to content

Hide strict OpenAPI validation behind a feature flag #162

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/_OpenAPIGeneratorCore/FeatureFlags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public enum FeatureFlag: String, Hashable, Equatable, Codable, CaseIterable {
/// Tracking issues:
/// - https://github.com/apple/swift-openapi-generator/pull/95
case proposal0001

/// Stricted input OpenAPI document validation.
///
/// Check for structural issues and detect cycles proactively.
case strictOpenAPIValidation
}

/// A set of enabled feature flags.
Expand Down
52 changes: 27 additions & 25 deletions Sources/_OpenAPIGeneratorCore/GeneratorPipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,31 +125,33 @@ func makeGeneratorPipeline(
postTransitionHooks: [
{ doc in

// Run OpenAPIKit's built-in validation.
try doc.validate()

// 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)
if config.featureFlags.contains(.strictOpenAPIValidation) {
// Run OpenAPIKit's built-in validation.
try doc.validate()

// 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 doc
Expand Down