Skip to content

Commit a75aa41

Browse files
authored
Merge pull request #411 from mattpolzin/port-vendor-extension-fix-to-OAS30
Port fix from #409 over to the OAS 3.0 module
2 parents a21615e + 621c11f commit a75aa41

File tree

3 files changed

+97
-32
lines changed

3 files changed

+97
-32
lines changed

Sources/OpenAPIKit30/Schema Object/JSONSchema.swift

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,9 @@ extension JSONSchema: Decodable {
17611761
schema = schema.nullableSchemaObject()
17621762
}
17631763

1764-
self = schema
1764+
self = .init(warnings: schema.warnings,
1765+
schema: schema.value,
1766+
vendorExtensions: try Self.decodeVenderExtensions(from: decoder))
17651767
return
17661768
}
17671769

@@ -1774,7 +1776,9 @@ extension JSONSchema: Decodable {
17741776
schema = schema.nullableSchemaObject()
17751777
}
17761778

1777-
self = schema
1779+
self = .init(warnings: schema.warnings,
1780+
schema: schema.value,
1781+
vendorExtensions: try Self.decodeVenderExtensions(from: decoder))
17781782
return
17791783
}
17801784

@@ -1787,7 +1791,9 @@ extension JSONSchema: Decodable {
17871791
schema = schema.nullableSchemaObject()
17881792
}
17891793

1790-
self = schema
1794+
self = .init(warnings: schema.warnings,
1795+
schema: schema.value,
1796+
vendorExtensions: try Self.decodeVenderExtensions(from: decoder))
17911797
return
17921798
}
17931799

@@ -1797,7 +1803,9 @@ extension JSONSchema: Decodable {
17971803
core: try CoreContext<JSONTypeFormat.AnyFormat>(from: decoder)
17981804
)
17991805

1800-
self = schema
1806+
self = .init(warnings: schema.warnings,
1807+
schema: schema.value,
1808+
vendorExtensions: try Self.decodeVenderExtensions(from: decoder))
18011809
return
18021810
}
18031811

@@ -1888,25 +1896,27 @@ extension JSONSchema: Decodable {
18881896

18891897
self.warnings = _warnings
18901898

1891-
// Ad-hoc vendor extension support since JSONSchema does coding keys differently.
1899+
self.vendorExtensions = try Self.decodeVenderExtensions(from: decoder)
1900+
}
1901+
1902+
private static func decodeVenderExtensions(from decoder: Decoder) throws -> [String: AnyCodable] {
18921903
guard VendorExtensionsConfiguration.isEnabled else {
1893-
self.vendorExtensions = [:]
1894-
return
1904+
return [:]
18951905
}
1896-
1906+
18971907
let decoded = try AnyCodable(from: decoder).value
1898-
1908+
18991909
guard (decoded as? [Any]) == nil else {
19001910
throw VendorExtensionDecodingError.selfIsArrayNotDict
19011911
}
1902-
1912+
19031913
guard let decodedAny = decoded as? [String: Any] else {
19041914
throw VendorExtensionDecodingError.foundNonStringKeys
19051915
}
1906-
1907-
let extensions = decodedAny.filter { $0.key.lowercased().starts(with: "x-") }
1908-
1909-
self.vendorExtensions = extensions.mapValues(AnyCodable.init)
1916+
1917+
return decodedAny
1918+
.filter { $0.key.lowercased().starts(with: "x-") }
1919+
.mapValues(AnyCodable.init)
19101920
}
19111921
}
19121922

Tests/OpenAPIKit30Tests/Schema Object/JSONSchemaTests.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,61 @@ extension SchemaObjectTests {
15081508
)
15091509
}
15101510

1511+
func test_decodeAnyOfWithVendorExtension() throws {
1512+
let extensionSchema = """
1513+
{
1514+
"anyOf" : [
1515+
{ "type": "string" },
1516+
{ "type": "number" }
1517+
],
1518+
"x-hello" : "hello"
1519+
}
1520+
""".data(using: .utf8)!
1521+
1522+
let desiredSchema = JSONSchema(schema: .any(of: [JSONSchema.string, JSONSchema.number], core: .init()), vendorExtensions: ["x-hello": "hello"])
1523+
XCTAssertEqual(
1524+
try orderUnstableDecode(JSONSchema.self, from: extensionSchema),
1525+
desiredSchema
1526+
)
1527+
}
1528+
1529+
func test_decodeAllOfWithVendorExtension() throws {
1530+
let extensionSchema = """
1531+
{
1532+
"allOf" : [
1533+
{ "type": "string" },
1534+
{ "type": "number" }
1535+
],
1536+
"x-hello" : "hello"
1537+
}
1538+
""".data(using: .utf8)!
1539+
1540+
let desiredSchema = JSONSchema(schema: .all(of: [JSONSchema.string, JSONSchema.number], core: .init()), vendorExtensions: ["x-hello": "hello"])
1541+
XCTAssertEqual(
1542+
try orderUnstableDecode(JSONSchema.self, from: extensionSchema),
1543+
desiredSchema
1544+
)
1545+
}
1546+
1547+
func test_decodeOneOfWithVendorExtension() throws {
1548+
let extensionSchema = """
1549+
{
1550+
"oneOf" : [
1551+
{ "type": "string" },
1552+
{ "type": "number" }
1553+
],
1554+
"x-hello" : "hello"
1555+
}
1556+
""".data(using: .utf8)!
1557+
1558+
let desiredSchema = JSONSchema(schema: .one(of: [JSONSchema.string, JSONSchema.number], core: .init()), vendorExtensions: ["x-hello": "hello"])
1559+
XCTAssertEqual(
1560+
try orderUnstableDecode(JSONSchema.self, from: extensionSchema),
1561+
desiredSchema
1562+
)
1563+
}
1564+
1565+
15111566
func test_encodeBoolean() {
15121567
let requiredBoolean = JSONSchema.boolean(.init(format: .unspecified, required: true))
15131568
let optionalBoolean = JSONSchema.boolean(.init(format: .unspecified, required: false))

Tests/OpenAPIKitTests/Schema Object/JSONSchemaTests.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,24 @@ extension SchemaObjectTests {
19181918
JSONSchema.fragment(.init(examples: ["hello"])).with(vendorExtensions: ["x-hello": "hello"])
19191919
)
19201920
}
1921+
1922+
func test_encodeExamplesVendorExtension() throws {
1923+
let fragment = JSONSchema.fragment(.init(examples: ["hello"])).with(vendorExtensions: ["x-hello": "hello"])
1924+
1925+
let encoded = try orderUnstableTestStringFromEncoding(of: fragment)
1926+
1927+
assertJSONEquivalent(
1928+
encoded,
1929+
"""
1930+
{
1931+
"examples" : [
1932+
"hello"
1933+
],
1934+
"x-hello" : "hello"
1935+
}
1936+
"""
1937+
)
1938+
}
19211939

19221940
func test_decodeAnyOfWithVendorExtension() throws {
19231941
let extensionSchema = """
@@ -1970,24 +1988,6 @@ extension SchemaObjectTests {
19701988
)
19711989
}
19721990

1973-
func test_encodeExamplesVendorExtension() throws {
1974-
let fragment = JSONSchema.fragment(.init(examples: ["hello"])).with(vendorExtensions: ["x-hello": "hello"])
1975-
1976-
let encoded = try orderUnstableTestStringFromEncoding(of: fragment)
1977-
1978-
assertJSONEquivalent(
1979-
encoded,
1980-
"""
1981-
{
1982-
"examples" : [
1983-
"hello"
1984-
],
1985-
"x-hello" : "hello"
1986-
}
1987-
"""
1988-
)
1989-
}
1990-
19911991
func test_decodeNullType() throws {
19921992
let nullTypeData = """
19931993
{

0 commit comments

Comments
 (0)