Skip to content

Support vendor extensions in anyOf allOf oneOf for OAS 3.1 #409

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
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
45 changes: 29 additions & 16 deletions Sources/OpenAPIKit/Schema Object/JSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1971,7 +1971,10 @@ extension JSONSchema: Decodable {
schema = schema.nullableSchemaObject()
}

self = schema
// Ad-hoc vendor extension support since JSONSchema does coding keys differently.
let extensions = try Self.decodeVenderExtensions(from: decoder)

self = schema.with(vendorExtensions: extensions)
return
}

Expand All @@ -1988,7 +1991,10 @@ extension JSONSchema: Decodable {
schema = schema.nullableSchemaObject()
}

self = schema
// Ad-hoc vendor extension support since JSONSchema does coding keys differently.
let extensions = try Self.decodeVenderExtensions(from: decoder)

self = schema.with(vendorExtensions: extensions)
return
}

Expand All @@ -2004,7 +2010,10 @@ extension JSONSchema: Decodable {
schema = schema.nullableSchemaObject()
}

self = schema
// Ad-hoc vendor extension support since JSONSchema does coding keys differently.
let extensions = try Self.decodeVenderExtensions(from: decoder)

self = schema.with(vendorExtensions: extensions)
return
}

Expand All @@ -2016,8 +2025,11 @@ extension JSONSchema: Decodable {
core: coreContext
)
)

self = schema

// Ad-hoc vendor extension support since JSONSchema does coding keys differently.
let extensions = try Self.decodeVenderExtensions(from: decoder)

self = schema.with(vendorExtensions: extensions)
return
}

Expand Down Expand Up @@ -2132,29 +2144,30 @@ extension JSONSchema: Decodable {

self.warnings = _warnings

// Ad-hoc vendor extension support since JSONSchema does coding keys differently.
let extensions: [String: AnyCodable]
// Ad-hoc vendor extension support since JSONSchema does coding keys differently.
let extensions = try Self.decodeVenderExtensions(from: decoder)

self.value = value.with(vendorExtensions: extensions)
}

private static func decodeVenderExtensions(from decoder: Decoder) throws -> [String: AnyCodable] {
guard VendorExtensionsConfiguration.isEnabled else {
self.value = value
return
return [:]
}

let decoded = try AnyCodable(from: decoder).value

guard (decoded as? [Any]) == nil else {
throw VendorExtensionDecodingError.selfIsArrayNotDict
}

guard let decodedAny = decoded as? [String: Any] else {
throw VendorExtensionDecodingError.foundNonStringKeys
}

extensions = decodedAny
return decodedAny
.filter { $0.key.lowercased().starts(with: "x-") }
.mapValues(AnyCodable.init)

self.value = value.with(vendorExtensions: extensions)
}

private static func decodeTypes(from container: KeyedDecodingContainer<JSONSchema.HintCodingKeys>) throws -> [JSONType] {
Expand Down
51 changes: 51 additions & 0 deletions Tests/OpenAPIKitTests/Schema Object/JSONSchemaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1918,6 +1918,57 @@ extension SchemaObjectTests {
JSONSchema.fragment(.init(examples: ["hello"])).with(vendorExtensions: ["x-hello": "hello"])
)
}

func test_decodeAnyOfWithVendorExtension() throws {
let extensionSchema = """
{
"anyOf" : [
{ "type": "string" },
{ "type": "number" }
],
"x-hello" : "hello"
}
""".data(using: .utf8)!

XCTAssertEqual(
try orderUnstableDecode(JSONSchema.self, from: extensionSchema),
JSONSchema.any(of: [JSONSchema.string, JSONSchema.number]).with(vendorExtensions: ["x-hello": "hello"])
)
}

func test_decodeAllOfWithVendorExtension() throws {
let extensionSchema = """
{
"allOf" : [
{ "type": "string" },
{ "type": "number" }
],
"x-hello" : "hello"
}
""".data(using: .utf8)!

XCTAssertEqual(
try orderUnstableDecode(JSONSchema.self, from: extensionSchema),
JSONSchema.all(of: [JSONSchema.string, JSONSchema.number]).with(vendorExtensions: ["x-hello": "hello"])
)
}

func test_decodeOneOfWithVendorExtension() throws {
let extensionSchema = """
{
"oneOf" : [
{ "type": "string" },
{ "type": "number" }
],
"x-hello" : "hello"
}
""".data(using: .utf8)!

XCTAssertEqual(
try orderUnstableDecode(JSONSchema.self, from: extensionSchema),
JSONSchema.one(of: [JSONSchema.string, JSONSchema.number]).with(vendorExtensions: ["x-hello": "hello"])
)
}

func test_encodeExamplesVendorExtension() throws {
let fragment = JSONSchema.fragment(.init(examples: ["hello"])).with(vendorExtensions: ["x-hello": "hello"])
Expand Down