Skip to content

Commit 732251a

Browse files
authored
Merge pull request #70 from siemensikkema/feature/use-encode-if-present
Use encodeIfPresent instead of custom helper function
2 parents 68b8072 + f980ba2 commit 732251a

24 files changed

+114
-189
lines changed

Sources/OpenAPIKit/Component Object/Components.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ extension OpenAPI.Components {
199199
case examples
200200
case requestBodies
201201
case headers
202-
case securitySchemes
202+
case securitySchemes
203203
// case links
204204
// case callbacks
205205

Sources/OpenAPIKit/Content/Content.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ extension OpenAPI.Content: Encodable {
121121
try container.encode(example, forKey: .example)
122122
}
123123

124-
try encoding.encodeIfNotNil(to: &container, forKey: .encoding)
124+
try container.encodeIfPresent(encoding, forKey: .encoding)
125125

126126
try encodeExtensions(to: &container)
127127
}

Sources/OpenAPIKit/Content/ContentEncoding.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ extension OpenAPI.Content.Encoding: Encodable {
5050
public func encode(to encoder: Encoder) throws {
5151
var container = encoder.container(keyedBy: CodingKeys.self)
5252

53-
try contentType.encodeIfNotNil(to: &container, forKey: .contentType)
54-
55-
try headers.encodeIfNotNil(to: &container, forKey: .headers)
53+
try container.encodeIfPresent(contentType, forKey: .contentType)
54+
try container.encodeIfPresent(headers, forKey: .headers)
5655

5756
if style != Self.defaultStyle {
5857
try container.encode(style, forKey: .style)

Sources/OpenAPIKit/Discriminator.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ extension OpenAPI.Discriminator: Encodable {
3030
var container = encoder.container(keyedBy: CodingKeys.self)
3131

3232
try container.encode(propertyName, forKey: .propertyName)
33-
34-
try mapping.encodeIfNotNil(to: &container, forKey: .mapping)
33+
try container.encodeIfPresent(mapping, forKey: .mapping)
3534
}
3635
}
3736

@@ -40,7 +39,6 @@ extension OpenAPI.Discriminator: Decodable {
4039
let container = try decoder.container(keyedBy: CodingKeys.self)
4140

4241
propertyName = try container.decode(String.self, forKey: .propertyName)
43-
4442
mapping = try container.decodeIfPresent([String: String].self, forKey: .mapping)
4543
}
4644
}

Sources/OpenAPIKit/Document.swift

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ extension OpenAPI.Document: Encodable {
7777
var container = encoder.container(keyedBy: CodingKeys.self)
7878

7979
try container.encode(openAPIVersion, forKey: .openAPIVersion)
80-
8180
try container.encode(info, forKey: .info)
8281

8382
if !servers.isEmpty {
@@ -96,9 +95,8 @@ extension OpenAPI.Document: Encodable {
9695
try encodeSecurity(requirements: security, to: &container, forKey: .security)
9796
}
9897

99-
try tags.encodeIfNotNil(to: &container, forKey: .tags)
100-
101-
try externalDocs.encodeIfNotNil(to: &container, forKey: .externalDocs)
98+
try container.encodeIfPresent(tags, forKey: .tags)
99+
try container.encodeIfPresent(externalDocs, forKey: .externalDocs)
102100

103101
try encodeExtensions(to: &container)
104102
}
@@ -110,9 +108,7 @@ extension OpenAPI.Document: Decodable {
110108

111109
do {
112110
openAPIVersion = try container.decode(OpenAPI.Document.Version.self, forKey: .openAPIVersion)
113-
114111
info = try container.decode(OpenAPI.Document.Info.self, forKey: .info)
115-
116112
servers = try container.decodeIfPresent([OpenAPI.Server].self, forKey: .servers) ?? []
117113

118114
let components = try container.decodeIfPresent(OpenAPI.Components.self, forKey: .components) ?? .noComponents
@@ -123,11 +119,8 @@ extension OpenAPI.Document: Decodable {
123119
try validateSecurityRequirements(in: paths, against: components)
124120

125121
security = try decodeSecurityRequirements(from: container, forKey: .security, given: components) ?? []
126-
127122
tags = try container.decodeIfPresent([OpenAPI.Tag].self, forKey: .tags)
128-
129123
externalDocs = try container.decodeIfPresent(OpenAPI.ExternalDocumentation.self, forKey: .externalDocs)
130-
131124
vendorExtensions = try Self.extensions(from: decoder)
132125

133126
} catch let error as OpenAPI.Error.Decoding.Path {

Sources/OpenAPIKit/DocumentInfo.swift

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ extension OpenAPI.Document.Info.License: Encodable {
119119
var container = encoder.container(keyedBy: CodingKeys.self)
120120

121121
try container.encode(name, forKey: .name)
122-
123-
try url.encodeIfNotNil(to: &container, forKey: .url)
122+
try container.encodeIfPresent(url, forKey: .url)
124123

125124
try encodeExtensions(to: &container)
126125
}
@@ -192,11 +191,9 @@ extension OpenAPI.Document.Info.Contact: Encodable {
192191
public func encode(to encoder: Encoder) throws {
193192
var container = encoder.container(keyedBy: CodingKeys.self)
194193

195-
try name.encodeIfNotNil(to: &container, forKey: .name)
196-
197-
try url.encodeIfNotNil(to: &container, forKey: .url)
198-
199-
try email.encodeIfNotNil(to: &container, forKey: .email)
194+
try container.encodeIfPresent(name, forKey: .name)
195+
try container.encodeIfPresent(url, forKey: .url)
196+
try container.encodeIfPresent(email, forKey: .email)
200197

201198
try encodeExtensions(to: &container)
202199
}
@@ -207,9 +204,7 @@ extension OpenAPI.Document.Info.Contact: Decodable {
207204
let container = try decoder.container(keyedBy: CodingKeys.self)
208205

209206
name = try container.decodeIfPresent(String.self, forKey: .name)
210-
211207
url = try container.decodeIfPresent(URL.self, forKey: .url)
212-
213208
email = try container.decodeIfPresent(String.self, forKey: .email)
214209

215210
vendorExtensions = try Self.extensions(from: decoder)
@@ -277,15 +272,10 @@ extension OpenAPI.Document.Info: Encodable {
277272
var container = encoder.container(keyedBy: CodingKeys.self)
278273

279274
try container.encode(title, forKey: .title)
280-
281-
try description.encodeIfNotNil(to: &container, forKey: .description)
282-
283-
try termsOfService.encodeIfNotNil(to: &container, forKey: .termsOfService)
284-
285-
try contact.encodeIfNotNil(to: &container, forKey: .contact)
286-
287-
try license.encodeIfNotNil(to: &container, forKey: .license)
288-
275+
try container.encodeIfPresent(description, forKey: .description)
276+
try container.encodeIfPresent(termsOfService, forKey: .termsOfService)
277+
try container.encodeIfPresent(contact, forKey: .contact)
278+
try container.encodeIfPresent(license, forKey: .license)
289279
try container.encode(version, forKey: .version)
290280

291281
try encodeExtensions(to: &container)
@@ -297,15 +287,10 @@ extension OpenAPI.Document.Info: Decodable {
297287
let container = try decoder.container(keyedBy: CodingKeys.self)
298288

299289
title = try container.decode(String.self, forKey: .title)
300-
301290
description = try container.decodeIfPresent(String.self, forKey: .description)
302-
303291
termsOfService = try container.decodeIfPresent(URL.self, forKey: .termsOfService)
304-
305292
contact = try container.decodeIfPresent(Contact.self, forKey: .contact)
306-
307293
license = try container.decodeIfPresent(License.self, forKey: .license)
308-
309294
version = try container.decode(String.self, forKey: .version)
310295

311296
vendorExtensions = try Self.extensions(from: decoder)

Sources/OpenAPIKit/Example.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ extension OpenAPI.Example: Encodable {
6565
public func encode(to encoder: Encoder) throws {
6666
var container = encoder.container(keyedBy: CodingKeys.self)
6767

68-
try summary.encodeIfNotNil(to: &container, forKey: .summary)
69-
70-
try description.encodeIfNotNil(to: &container, forKey: .description)
68+
try container.encodeIfPresent(summary, forKey: .summary)
69+
try container.encodeIfPresent(description, forKey: .description)
7170

7271
switch value {
7372
case .a(let url):
@@ -95,7 +94,6 @@ extension OpenAPI.Example: Decodable {
9594
let externalValue = try container.decodeIfPresent(URL.self, forKey: .externalValue)
9695

9796
summary = try container.decodeIfPresent(String.self, forKey: .summary)
98-
9997
description = try container.decodeIfPresent(String.self, forKey: .description)
10098

10199
value = try externalValue.map(Either.init)

Sources/OpenAPIKit/ExternalDoc.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ extension OpenAPI.ExternalDocumentation: Encodable {
2929
public func encode(to encoder: Encoder) throws {
3030
var container = encoder.container(keyedBy: CodingKeys.self)
3131

32-
try description.encodeIfNotNil(to: &container, forKey: .description)
33-
32+
try container.encodeIfPresent(description, forKey: .description)
3433
try container.encode(url, forKey: .url)
3534
}
3635
}
@@ -40,7 +39,6 @@ extension OpenAPI.ExternalDocumentation: Decodable {
4039
let container = try decoder.container(keyedBy: CodingKeys.self)
4140

4241
description = try container.decodeIfPresent(String.self, forKey: .description)
43-
4442
url = try container.decode(URL.self, forKey: .url)
4543
}
4644
}

Sources/OpenAPIKit/Header.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ extension OpenAPI.Header: Encodable {
150150
try container.encode(contentMap, forKey: .content)
151151
}
152152

153-
try description.encodeIfNotNil(to: &container, forKey: .description)
153+
try container.encodeIfPresent(description, forKey: .description)
154154

155155
if deprecated {
156156
try container.encode(deprecated, forKey: .deprecated)

Sources/OpenAPIKit/OAuthFlows.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ extension OpenAPI.OAuthFlows: Encodable {
149149
public func encode(to encoder: Encoder) throws {
150150
var container = encoder.container(keyedBy: CodingKeys.self)
151151

152-
try implicit.encodeIfNotNil(to: &container, forKey: .implicit)
153-
try password.encodeIfNotNil(to: &container, forKey: .password)
154-
try clientCredentials.encodeIfNotNil(to: &container, forKey: .clientCredentials)
155-
try authorizationCode.encodeIfNotNil(to: &container, forKey: .authorizationCode)
152+
try container.encodeIfPresent(implicit, forKey: .implicit)
153+
try container.encodeIfPresent(password, forKey: .password)
154+
try container.encodeIfPresent(clientCredentials, forKey: .clientCredentials)
155+
try container.encodeIfPresent(authorizationCode, forKey: .authorizationCode)
156156
}
157157
}
158158

@@ -171,7 +171,7 @@ extension OpenAPI.OAuthFlows.CommonFields: Encodable {
171171
public func encode(to encoder: Encoder) throws {
172172
var container = encoder.container(keyedBy: CodingKeys.self)
173173

174-
try refreshUrl.encodeIfNotNil(to: &container, forKey: .refreshUrl)
174+
try container.encodeIfPresent(refreshUrl, forKey: .refreshUrl)
175175
try container.encode(scopes, forKey: .scopes)
176176
}
177177
}

0 commit comments

Comments
 (0)