Skip to content

Commit 2daac57

Browse files
authored
Merge pull request #388 from mattpolzin/disable-vendor-extensions-via-userInfo
Disable vendor extensions via user info
2 parents bc1d339 + 6641ac3 commit 2daac57

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+207
-68
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,16 @@ Many OpenAPIKit types support [Specification Extensions](https://spec.openapis.o
293293

294294
You can get or set specification extensions via the `vendorExtensions` property on any object that supports this feature. The keys are `Strings` beginning with the aforementioned "x-" prefix and the values are `AnyCodable`. If you set an extension without using the "x-" prefix, the prefix will be added upon encoding.
295295

296+
If you wish to disable decoding/encoding of vendor extensions for performance reasons, you can configure the Encoder and Decoder using their `userInfo`:
297+
```swift
298+
let userInfo = [VendorExtensionsConfiguration.enabledKey: false]
299+
let encoder = JSONEncoder()
300+
encoder.userInfo = userInfo
301+
302+
let decoder = JSONDecoder()
303+
decoder.userInfo = userInfo
304+
```
305+
296306
#### AnyCodable
297307
OpenAPIKit uses the `AnyCodable` type for vendor extensions and constructing examples for JSON Schemas. OpenAPIKit's `AnyCodable` type is an adaptation of the Flight School library that can be found [here](https://github.com/Flight-School/AnyCodable).
298308

Sources/OpenAPIKit/CodableVendorExtendable.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,21 @@ public protocol VendorExtendable {
2121
var vendorExtensions: VendorExtensions { get set }
2222
}
2323

24+
/// OpenAPIKit supports some additional Encoder/Decoder configuration above and beyond
25+
/// what the Encoder or Decoder support out of box.
26+
///
27+
/// To _disable_ encoding or decoding of Vendor Extensions (by default these are _enabled),
28+
/// set `userInfo[VendorExtensionsConfiguration.enabledKey] = false` for your encoder or decoder.
2429
public enum VendorExtensionsConfiguration {
25-
public static var isEnabled = true
30+
public static let enabledKey: CodingUserInfoKey = .init(rawValue: "vendor-extensions-enabled")!
31+
32+
static func isEnabled(for decoder: Decoder) -> Bool {
33+
decoder.userInfo[enabledKey] as? Bool ?? true
34+
}
35+
36+
static func isEnabled(for encoder: Encoder) -> Bool {
37+
encoder.userInfo[enabledKey] as? Bool ?? true
38+
}
2639
}
2740

2841
internal protocol ExtendableCodingKey: CodingKey, Equatable {
@@ -75,7 +88,7 @@ internal enum VendorExtensionDecodingError: Swift.Error, CustomStringConvertible
7588
extension CodableVendorExtendable {
7689

7790
internal static func extensions(from decoder: Decoder) throws -> VendorExtensions {
78-
guard VendorExtensionsConfiguration.isEnabled else {
91+
guard VendorExtensionsConfiguration.isEnabled(for: decoder) else {
7992
return [:]
8093
}
8194

@@ -109,9 +122,6 @@ extension CodableVendorExtendable {
109122
}
110123

111124
internal func encodeExtensions<T: KeyedEncodingContainerProtocol>(to container: inout T) throws where T.Key == Self.CodingKeys {
112-
guard VendorExtensionsConfiguration.isEnabled else {
113-
return
114-
}
115125
for (key, value) in vendorExtensions {
116126
let xKey = key.starts(with: "x-") ? key : "x-\(key)"
117127
try container.encode(value, forKey: .extendedKey(for: xKey))

Sources/OpenAPIKit/Components Object/Components.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ extension OpenAPI.Components: Encodable {
180180
try container.encode(pathItems, forKey: .pathItems)
181181
}
182182

183-
try encodeExtensions(to: &container)
183+
if VendorExtensionsConfiguration.isEnabled(for: encoder) {
184+
try encodeExtensions(to: &container)
185+
}
184186
}
185187
}
186188

Sources/OpenAPIKit/Content/Content.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ extension OpenAPI.Content: Encodable {
161161

162162
try container.encodeIfPresent(encoding, forKey: .encoding)
163163

164-
try encodeExtensions(to: &container)
164+
if VendorExtensionsConfiguration.isEnabled(for: encoder) {
165+
try encodeExtensions(to: &container)
166+
}
165167
}
166168
}
167169

Sources/OpenAPIKit/Document/Document.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,9 @@ extension OpenAPI.Document: Encodable {
456456
try container.encode(paths, forKey: .paths)
457457
}
458458

459-
try encodeExtensions(to: &container)
459+
if VendorExtensionsConfiguration.isEnabled(for: encoder) {
460+
try encodeExtensions(to: &container)
461+
}
460462

461463
if !components.isEmpty {
462464
try container.encode(components, forKey: .components)

Sources/OpenAPIKit/Document/DocumentInfo.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ extension OpenAPI.Document.Info.License: Encodable {
191191
}
192192
}
193193

194-
try encodeExtensions(to: &container)
194+
if VendorExtensionsConfiguration.isEnabled(for: encoder) {
195+
try encodeExtensions(to: &container)
196+
}
195197
}
196198
}
197199

@@ -269,7 +271,9 @@ extension OpenAPI.Document.Info.Contact: Encodable {
269271
try container.encodeIfPresent(url?.absoluteString, forKey: .url)
270272
try container.encodeIfPresent(email, forKey: .email)
271273

272-
try encodeExtensions(to: &container)
274+
if VendorExtensionsConfiguration.isEnabled(for: encoder) {
275+
try encodeExtensions(to: &container)
276+
}
273277
}
274278
}
275279

@@ -345,7 +349,9 @@ extension OpenAPI.Document.Info: Encodable {
345349
try container.encodeIfPresent(license, forKey: .license)
346350
try container.encode(version, forKey: .version)
347351

348-
try encodeExtensions(to: &container)
352+
if VendorExtensionsConfiguration.isEnabled(for: encoder) {
353+
try encodeExtensions(to: &container)
354+
}
349355
}
350356
}
351357

Sources/OpenAPIKit/Example.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ extension OpenAPI.Example: Encodable {
106106
break
107107
}
108108

109-
try encodeExtensions(to: &container)
109+
if VendorExtensionsConfiguration.isEnabled(for: encoder) {
110+
try encodeExtensions(to: &container)
111+
}
110112
}
111113
}
112114

Sources/OpenAPIKit/ExternalDocumentation.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ extension OpenAPI.ExternalDocumentation: Encodable {
5757
try container.encodeIfPresent(description, forKey: .description)
5858
try container.encode(url.absoluteString, forKey: .url)
5959

60-
try encodeExtensions(to: &container)
60+
if VendorExtensionsConfiguration.isEnabled(for: encoder) {
61+
try encodeExtensions(to: &container)
62+
}
6163
}
6264
}
6365

Sources/OpenAPIKit/Header/Header.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,9 @@ extension OpenAPI.Header: Encodable {
289289
try container.encode(deprecated, forKey: .deprecated)
290290
}
291291

292-
try encodeExtensions(to: &container)
292+
if VendorExtensionsConfiguration.isEnabled(for: encoder) {
293+
try encodeExtensions(to: &container)
294+
}
293295
}
294296
}
295297

Sources/OpenAPIKit/Link.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ extension OpenAPI.Link: Encodable {
174174
try container.encodeIfPresent(description, forKey: .description)
175175
try container.encodeIfPresent(server, forKey: .server)
176176

177-
try encodeExtensions(to: &container)
177+
if VendorExtensionsConfiguration.isEnabled(for: encoder) {
178+
try encodeExtensions(to: &container)
179+
}
178180
}
179181
}
180182

0 commit comments

Comments
 (0)