Skip to content
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

New AnyCodable implementation #270

Closed
wants to merge 4 commits into from
Closed
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
10 changes: 3 additions & 7 deletions Sources/OpenAPIKit/CodableVendorExtendable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,12 @@ extension CodableVendorExtendable {
return [:]
}

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

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

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

let extensions = decodedAny.filter {
let key = CodingKeys.key(for: $0.key)

Expand All @@ -105,7 +101,7 @@ extension CodableVendorExtendable {
)
}

return extensions.mapValues(AnyCodable.init)
return extensions
}

internal func encodeExtensions<T: KeyedEncodingContainerProtocol>(to container: inout T) throws where T.Key == Self.CodingKeys {
Expand Down
22 changes: 10 additions & 12 deletions Sources/OpenAPIKit/Schema Object/JSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1967,21 +1967,19 @@ extension JSONSchema: Decodable {
return
}

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

guard (decoded as? [Any]) == nil else {
let decoded = try AnyCodable(from: decoder)

switch decoded {
case let .object(dictionary):
extensions = dictionary
.filter { $0.key.lowercased().starts(with: "x-") }

self.value = value.with(vendorExtensions: extensions)
case .array:
throw VendorExtensionDecodingError.selfIsArrayNotDict
}

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

extensions = 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
6 changes: 4 additions & 2 deletions Sources/OpenAPIKit/Schema Object/JSONSchemaContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -858,9 +858,11 @@ extension JSONSchema.CoreContext: Decodable {
externalDocs = try container.decodeIfPresent(OpenAPI.ExternalDocumentation.self, forKey: .externalDocs)
if Format.self == JSONTypeFormat.StringFormat.self {
if nullable {
allowedValues = try Self.decodeAllowedValuesOrConst(String?.self, inContainer: container)?.map(AnyCodable.init)
allowedValues = try Self.decodeAllowedValuesOrConst(String?.self, inContainer: container)?.map {
$0.map(AnyCodable.string) ?? .null
}
} else {
allowedValues = try Self.decodeAllowedValuesOrConst(String.self, inContainer: container)?.map(AnyCodable.init)
allowedValues = try Self.decodeAllowedValuesOrConst(String.self, inContainer: container)?.map(AnyCodable.string)
}
} else {
allowedValues = try Self.decodeAllowedValuesOrConst(AnyCodable.self, inContainer: container)
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit/_CoreReExport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Mathew Polzin on 2/28/21.
//

@_exported import struct OpenAPIKitCore.AnyCodable
@_exported import enum OpenAPIKitCore.AnyCodable
@_exported import struct OpenAPIKitCore.CodingPathError
@_exported import enum OpenAPIKitCore.Either
@_exported import protocol OpenAPIKitCore.OpenAPIError
Expand Down
47 changes: 23 additions & 24 deletions Sources/OpenAPIKit30/CodableVendorExtendable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,32 @@ extension CodableVendorExtendable {
return [:]
}

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

guard (decoded as? [Any]) == nil else {
let decoded = try AnyCodable(from: decoder)

switch decoded {
case .object(let dictionary):
let extensions = dictionary.filter {
let key = CodingKeys.key(for: $0.key)

return !CodingKeys.allBuiltinKeys.contains(key)
}

let invalidKeys = extensions.keys.filter { !$0.lowercased().starts(with: "x-") }
if !invalidKeys.isEmpty {
let invalidKeysList = "[ " + invalidKeys.joined(separator: ", ") + " ]"
throw InconsistencyError(
subjectName: "Vendor Extension",
details: "Found at least one vendor extension property that does not begin with the required 'x-' prefix. Invalid properties: \(invalidKeysList)",
codingPath: decoder.codingPath
)
}

return extensions
case .array:
throw VendorExtensionDecodingError.selfIsArrayNotDict
}

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

let extensions = decodedAny.filter {
let key = CodingKeys.key(for: $0.key)

return !CodingKeys.allBuiltinKeys.contains(key)
}

let invalidKeys = extensions.keys.filter { !$0.lowercased().starts(with: "x-") }
if !invalidKeys.isEmpty {
let invalidKeysList = "[ " + invalidKeys.joined(separator: ", ") + " ]"
throw InconsistencyError(
subjectName: "Vendor Extension",
details: "Found at least one vendor extension property that does not begin with the required 'x-' prefix. Invalid properties: \(invalidKeysList)",
codingPath: decoder.codingPath
)
}

return extensions.mapValues(AnyCodable.init)
}

internal func encodeExtensions<T: KeyedEncodingContainerProtocol>(to container: inout T) throws where T.Key == Self.CodingKeys {
Expand Down
18 changes: 8 additions & 10 deletions Sources/OpenAPIKit30/Schema Object/JSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1894,19 +1894,17 @@ extension JSONSchema: Decodable {
return
}

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

guard (decoded as? [Any]) == nil else {
let decoded = try AnyCodable(from: decoder)

switch decoded {
case .object(let dictionary):
let extensions = dictionary.filter { $0.key.lowercased().starts(with: "x-") }
self.vendorExtensions = extensions
case .array:
throw VendorExtensionDecodingError.selfIsArrayNotDict
}

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

let extensions = decodedAny.filter { $0.key.lowercased().starts(with: "x-") }

self.vendorExtensions = extensions.mapValues(AnyCodable.init)
}
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/OpenAPIKit30/Schema Object/JSONSchemaContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,9 @@ extension JSONSchema.CoreContext: Decodable {
externalDocs = try container.decodeIfPresent(OpenAPI.ExternalDocumentation.self, forKey: .externalDocs)
if Format.self == JSONTypeFormat.StringFormat.self {
if (nullable ?? false) {
allowedValues = try container.decodeIfPresent([String?].self, forKey: .allowedValues)?.map(AnyCodable.init)
allowedValues = try container.decodeIfPresent([String?].self, forKey: .allowedValues)?.map {
$0.map(AnyCodable.string) ?? .null
}
} else {
allowedValues = try container.decodeIfPresent([String].self, forKey: .allowedValues)?.map(AnyCodable.init)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIKit30/_CoreReExport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Mathew Polzin on 2/28/21.
//

@_exported import struct OpenAPIKitCore.AnyCodable
@_exported import enum OpenAPIKitCore.AnyCodable
@_exported import struct OpenAPIKitCore.CodingPathError
@_exported import enum OpenAPIKitCore.Either
@_exported import protocol OpenAPIKitCore.OpenAPIError
Expand Down
Loading
Loading