Skip to content

Commit 5dd0da1

Browse files
Disable OpenAPIKit parameterStyleAndLocationAreCompatible validator (#903)
### Motivation After updating to OpenAPIKit 6.0 (#875), the generator fails on OpenAPI documents that use `style: simple` on query parameters. While this is technically invalid per the OpenAPI specification, it was previously tolerated -- the generator would skip the unsupported parameter with a warning diagnostic and continue generation. The new OpenAPIKit 6.0 brings a new default built-in validation (`.parameterStyleAndLocationAreCompatible`) that rejects documents with this combination at validation time. Because this fires as a validation error (not a warning), it is thrown even with `strict: false`, before the translator gets a chance to handle the parameter gracefully. This is a regression for users with documents that have this common spec violation, which we do have examples of in our extended compatibility test suite). ### Modifications Replace the use of `Validator()` (which includes all OpenAPIKit defaults) with a custom validator built from `Validator.blank`. This explicitly lists all default validations from OpenAPIKit 6.0.0 except `.parameterStyleAndLocationAreCompatible`, restoring the previous behaviour where the generator tolerates these documents. We might want to revisit how lenient we are for this, but this patch is the most expeditious way to getting a release out with the OpenAPIKit update. ### Result Documents with `style: simple` on query parameters are no longer rejected at validation time. The translator continues to emit an "unsupported" diagnostic and skip the parameter, as it did before the OpenAPIKit upgrade: ``` 'Feature "Query params of style simple, explode: false" is not supported, skipping' ```
1 parent d154389 commit 5dd0da1

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

Sources/_OpenAPIGeneratorCore/Parser/validateDoc.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func validateDoc(_ doc: ParsedOpenAPIRepresentation, config: Config) throws -> [
303303
// block the generator from running.
304304
// Validation errors continue to be fatal, such as
305305
// structural issues, like non-unique operationIds, etc.
306-
let warnings = try doc.validate(using: Validator().validating(.operationsContainResponses), strict: false)
306+
let warnings = try doc.validate(using: .swiftOpenAPICustomValidator, strict: false)
307307
let diagnostics: [Diagnostic] = warnings.map { warning in
308308
.warning(
309309
message: "Validation warning: \(warning.description)",
@@ -315,3 +315,27 @@ func validateDoc(_ doc: ParsedOpenAPIRepresentation, config: Config) throws -> [
315315
}
316316
return typeOverrideDiagnostics + diagnostics
317317
}
318+
319+
extension OpenAPIKit.Validator {
320+
static var swiftOpenAPICustomValidator: Validator {
321+
// Start with blank.
322+
.blank
323+
// Add defaults as of OpenAPIKit 6.0.0
324+
// https://github.com/mattpolzin/OpenAPIKit/blob/118d039/Sources/OpenAPIKit/Validator/Validator.swift#L184-L186
325+
.validating(.documentTagNamesAreUnique).validating(.documentServerNamesAreUnique)
326+
.validating(.pathItemParametersAreUnique).validating(.operationParametersAreUnique)
327+
.validating(.querystringParametersAreCompatible).validating(.operationIdsAreUnique)
328+
.validating(.serverVariableEnumIsValid).validating(.serverVariableDefaultExistsInEnum)
329+
// Without this one to be backwards compatible with previous versions of Swift OpenAPI Generator.
330+
// Even when run with strict=false, this one will cause OpenAPIKit to throw an error. Previous verions were more
331+
// lenient and Swift OpenAPI Generator would later emit a warning that it's unsupported.
332+
// .validating(.parameterStyleAndLocationAreCompatible)
333+
.validating(.schemaReferencesAreValid).validating(.jsonSchemaReferencesAreValid)
334+
.validating(.responseReferencesAreValid).validating(.parameterReferencesAreValid)
335+
.validating(.exampleReferencesAreValid).validating(.requestReferencesAreValid)
336+
.validating(.headerReferencesAreValid).validating(.linkReferencesAreValid)
337+
.validating(.callbacksReferencesAreValid).validating(.pathItemReferencesAreValid)
338+
// And also add in this one.
339+
.validating(.operationsContainResponses)
340+
}
341+
}

Tests/OpenAPIGeneratorCoreTests/Parser/Test_validateDoc.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,38 @@ final class Test_validateDoc: Test_Core {
480480
)
481481
}
482482
}
483+
func testParameterStyleLocationMismatchIsNotFatal() throws {
484+
let yaml = """
485+
openapi: "3.0.0"
486+
info:
487+
title: "Test"
488+
version: "1.0.0"
489+
paths:
490+
/foo:
491+
parameters:
492+
- name: foo
493+
in: query
494+
style: simple
495+
explode: false
496+
schema:
497+
type: string
498+
"""
499+
let doc = try YamsParser.parseOpenAPIDocument(
500+
.init(absolutePath: URL(fileURLWithPath: "/foo.yaml"), contents: Data(yaml.utf8)),
501+
diagnostics: PrintingDiagnosticCollector()
502+
)
503+
XCTAssertNoThrow(
504+
try validateDoc(
505+
doc,
506+
config: .init(
507+
mode: .types,
508+
access: Config.defaultAccessModifier,
509+
namingStrategy: Config.defaultNamingStrategy
510+
)
511+
)
512+
)
513+
}
514+
483515
func testValidateTypeOverrides() throws {
484516
let schema = try loadSchemaFromYAML(
485517
#"""

0 commit comments

Comments
 (0)