Skip to content

Commit c181ed7

Browse files
authored
Hide strict OpenAPI validation behind a feature flag (#162)
Hide strict OpenAPI validation behind a feature flag ### Motivation In #130, we introduced stricted validation of input OpenAPI documents. Reflecting back, since this might start rejecting OpenAPI document that were previously accepted, that is considered a breaking change and should be hidden behind a feature flag, only to be enabled unconditionally in the next breaking version. ### Modifications Hide the new validation behind a feature flag. ### Result Without providing the feature flag explicitly, the generator will maintain its old behavior and not perform strict validation. ### Test Plan N/A - this is a speculative fix to maintain backwards compatibility in the 0.1.x version. Reviewed by: simonjbeaumont Builds: ✔︎ pull request validation (5.8) - Build finished. ✔︎ pull request validation (5.9) - Build finished. ✔︎ pull request validation (docc test) - Build finished. ✔︎ pull request validation (integration test) - Build finished. ✔︎ pull request validation (nightly) - Build finished. ✔︎ pull request validation (soundness) - Build finished. #162
1 parent 11e94cd commit c181ed7

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

Sources/_OpenAPIGeneratorCore/FeatureFlags.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public enum FeatureFlag: String, Hashable, Equatable, Codable, CaseIterable {
3939
/// Tracking issues:
4040
/// - https://github.com/apple/swift-openapi-generator/pull/95
4141
case proposal0001
42+
43+
/// Stricted input OpenAPI document validation.
44+
///
45+
/// Check for structural issues and detect cycles proactively.
46+
case strictOpenAPIValidation
4247
}
4348

4449
/// A set of enabled feature flags.

Sources/_OpenAPIGeneratorCore/GeneratorPipeline.swift

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -125,31 +125,33 @@ func makeGeneratorPipeline(
125125
postTransitionHooks: [
126126
{ doc in
127127

128-
// Run OpenAPIKit's built-in validation.
129-
try doc.validate()
130-
131-
// Validate that the document is dereferenceable, which
132-
// catches reference cycles, which we don't yet support.
133-
_ = try doc.locallyDereferenced()
134-
135-
// Also explicitly dereference the parts of components
136-
// that the generator uses. `locallyDereferenced()` above
137-
// only dereferences paths/operations, but not components.
138-
let components = doc.components
139-
try components.schemas.forEach { schema in
140-
_ = try schema.value.dereferenced(in: components)
141-
}
142-
try components.parameters.forEach { schema in
143-
_ = try schema.value.dereferenced(in: components)
144-
}
145-
try components.headers.forEach { schema in
146-
_ = try schema.value.dereferenced(in: components)
147-
}
148-
try components.requestBodies.forEach { schema in
149-
_ = try schema.value.dereferenced(in: components)
150-
}
151-
try components.responses.forEach { schema in
152-
_ = try schema.value.dereferenced(in: components)
128+
if config.featureFlags.contains(.strictOpenAPIValidation) {
129+
// Run OpenAPIKit's built-in validation.
130+
try doc.validate()
131+
132+
// Validate that the document is dereferenceable, which
133+
// catches reference cycles, which we don't yet support.
134+
_ = try doc.locallyDereferenced()
135+
136+
// Also explicitly dereference the parts of components
137+
// that the generator uses. `locallyDereferenced()` above
138+
// only dereferences paths/operations, but not components.
139+
let components = doc.components
140+
try components.schemas.forEach { schema in
141+
_ = try schema.value.dereferenced(in: components)
142+
}
143+
try components.parameters.forEach { schema in
144+
_ = try schema.value.dereferenced(in: components)
145+
}
146+
try components.headers.forEach { schema in
147+
_ = try schema.value.dereferenced(in: components)
148+
}
149+
try components.requestBodies.forEach { schema in
150+
_ = try schema.value.dereferenced(in: components)
151+
}
152+
try components.responses.forEach { schema in
153+
_ = try schema.value.dereferenced(in: components)
154+
}
153155
}
154156

155157
return doc

0 commit comments

Comments
 (0)