Skip to content

Commit 5b5b5ef

Browse files
authored
[Runtime] Design away EncodableBodyContent (#30)
[Runtime] Design away EncodableBodyContent ### Motivation Until #12 (Choose the serialization method based on content type), we were still looking for the right way to spell the various encoding/decoding methods that take into account both the type of the underlying schema, and the desired content type. In #12, we found something decent, but we could have simplified the spelling even further, getting rid of a whole closure invocation and a specialized type. ### Modifications In this PR (in preparation for multiple content type support), we do the simplification by completely removing `EncodableBodyContent` and the associated closure trampoline that we used to propoagate the content type string. We now pass the string directly, and simplified all the helper functions that have to do with encoding body content. Now, this is done in a backwards compatible way, so we really just deprecated all the existing methods, and the `EncodableBodyContent` type, but it'll all be cleaned up when we prepare for tagging the next breaking version. ### Result The helper functions have a simpler spelling, which simplifies the generator and reduces the lines of code we have to generate. (The associated generator PR will be coming in shortly.) ### Test Plan I preserved all the tests for the deprecated variants, and added new unit tests for the new variants. The deprecated tests will also be cleaned up in the next breaking version. Reviewed by: simonjbeaumont Builds: ✔︎ pull request validation (5.8) - Build finished. ✔︎ pull request validation (5.9) - Build finished. ✔︎ pull request validation (api breakage) - Build finished. ✔︎ pull request validation (docc test) - Build finished. ✔︎ pull request validation (integration test) - Build finished. ✔︎ pull request validation (nightly) - Build finished. ✔︎ pull request validation (soundness) - Build finished. #30
1 parent e37d38d commit 5b5b5ef

File tree

8 files changed

+643
-180
lines changed

8 files changed

+643
-180
lines changed

Sources/OpenAPIRuntime/Base/EncodableBodyContent.swift

Lines changed: 0 additions & 36 deletions
This file was deleted.

Sources/OpenAPIRuntime/Conversion/Converter+Client.swift

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -90,113 +90,113 @@ extension Converter {
9090
}
9191

9292
// | client | set | request body | text | string-convertible | optional | setOptionalRequestBodyAsText |
93-
public func setOptionalRequestBodyAsText<T: _StringConvertible, C>(
94-
_ value: C?,
93+
public func setOptionalRequestBodyAsText<T: _StringConvertible>(
94+
_ value: T?,
9595
headerFields: inout [HeaderField],
96-
transforming transform: (C) -> EncodableBodyContent<T>
96+
contentType: String
9797
) throws -> Data? {
9898
try setOptionalRequestBody(
9999
value,
100100
headerFields: &headerFields,
101-
transforming: transform,
101+
contentType: contentType,
102102
convert: convertStringConvertibleToTextData
103103
)
104104
}
105105

106106
// | client | set | request body | text | string-convertible | required | setRequiredRequestBodyAsText |
107-
public func setRequiredRequestBodyAsText<T: _StringConvertible, C>(
108-
_ value: C,
107+
public func setRequiredRequestBodyAsText<T: _StringConvertible>(
108+
_ value: T,
109109
headerFields: inout [HeaderField],
110-
transforming transform: (C) -> EncodableBodyContent<T>
110+
contentType: String
111111
) throws -> Data {
112112
try setRequiredRequestBody(
113113
value,
114114
headerFields: &headerFields,
115-
transforming: transform,
115+
contentType: contentType,
116116
convert: convertStringConvertibleToTextData
117117
)
118118
}
119119

120120
// | client | set | request body | text | date | optional | setOptionalRequestBodyAsText |
121-
public func setOptionalRequestBodyAsText<C>(
122-
_ value: C?,
121+
public func setOptionalRequestBodyAsText(
122+
_ value: Date?,
123123
headerFields: inout [HeaderField],
124-
transforming transform: (C) -> EncodableBodyContent<Date>
124+
contentType: String
125125
) throws -> Data? {
126126
try setOptionalRequestBody(
127127
value,
128128
headerFields: &headerFields,
129-
transforming: transform,
129+
contentType: contentType,
130130
convert: convertDateToTextData
131131
)
132132
}
133133

134134
// | client | set | request body | text | date | required | setRequiredRequestBodyAsText |
135-
public func setRequiredRequestBodyAsText<C>(
136-
_ value: C,
135+
public func setRequiredRequestBodyAsText(
136+
_ value: Date,
137137
headerFields: inout [HeaderField],
138-
transforming transform: (C) -> EncodableBodyContent<Date>
138+
contentType: String
139139
) throws -> Data {
140140
try setRequiredRequestBody(
141141
value,
142142
headerFields: &headerFields,
143-
transforming: transform,
143+
contentType: contentType,
144144
convert: convertDateToTextData
145145
)
146146
}
147147

148148
// | client | set | request body | JSON | codable | optional | setOptionalRequestBodyAsJSON |
149-
public func setOptionalRequestBodyAsJSON<T: Encodable, C>(
150-
_ value: C?,
149+
public func setOptionalRequestBodyAsJSON<T: Encodable>(
150+
_ value: T?,
151151
headerFields: inout [HeaderField],
152-
transforming transform: (C) -> EncodableBodyContent<T>
152+
contentType: String
153153
) throws -> Data? {
154154
try setOptionalRequestBody(
155155
value,
156156
headerFields: &headerFields,
157-
transforming: transform,
157+
contentType: contentType,
158158
convert: convertBodyCodableToJSON
159159
)
160160
}
161161

162162
// | client | set | request body | JSON | codable | required | setRequiredRequestBodyAsJSON |
163-
public func setRequiredRequestBodyAsJSON<T: Encodable, C>(
164-
_ value: C,
163+
public func setRequiredRequestBodyAsJSON<T: Encodable>(
164+
_ value: T,
165165
headerFields: inout [HeaderField],
166-
transforming transform: (C) -> EncodableBodyContent<T>
166+
contentType: String
167167
) throws -> Data {
168168
try setRequiredRequestBody(
169169
value,
170170
headerFields: &headerFields,
171-
transforming: transform,
171+
contentType: contentType,
172172
convert: convertBodyCodableToJSON
173173
)
174174
}
175175

176176
// | client | set | request body | binary | data | optional | setOptionalRequestBodyAsBinary |
177-
public func setOptionalRequestBodyAsBinary<C>(
178-
_ value: C?,
177+
public func setOptionalRequestBodyAsBinary(
178+
_ value: Data?,
179179
headerFields: inout [HeaderField],
180-
transforming transform: (C) -> EncodableBodyContent<Data>
180+
contentType: String
181181
) throws -> Data? {
182182
try setOptionalRequestBody(
183183
value,
184184
headerFields: &headerFields,
185-
transforming: transform,
185+
contentType: contentType,
186186
convert: convertDataToBinary
187187
)
188188
}
189189

190190
// | client | set | request body | binary | data | required | setRequiredRequestBodyAsBinary |
191-
public func setRequiredRequestBodyAsBinary<C>(
192-
_ value: C,
191+
public func setRequiredRequestBodyAsBinary(
192+
_ value: Data,
193193
headerFields: inout [HeaderField],
194-
transforming transform: (C) -> EncodableBodyContent<Data>
194+
contentType: String
195195
) throws -> Data {
196196
try setRequiredRequestBody(
197197
value,
198198
headerFields: &headerFields,
199-
transforming: transform,
199+
contentType: contentType,
200200
convert: convertDataToBinary
201201
)
202202
}

Sources/OpenAPIRuntime/Conversion/Converter+Server.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -296,57 +296,57 @@ public extension Converter {
296296
}
297297

298298
// | server | set | response body | text | string-convertible | required | setResponseBodyAsText |
299-
func setResponseBodyAsText<T: _StringConvertible, C>(
300-
_ value: C,
299+
func setResponseBodyAsText<T: _StringConvertible>(
300+
_ value: T,
301301
headerFields: inout [HeaderField],
302-
transforming transform: (C) -> EncodableBodyContent<T>
302+
contentType: String
303303
) throws -> Data {
304304
try setResponseBody(
305305
value,
306306
headerFields: &headerFields,
307-
transforming: transform,
307+
contentType: contentType,
308308
convert: convertStringConvertibleToTextData
309309
)
310310
}
311311

312312
// | server | set | response body | text | date | required | setResponseBodyAsText |
313-
func setResponseBodyAsText<C>(
314-
_ value: C,
313+
func setResponseBodyAsText(
314+
_ value: Date,
315315
headerFields: inout [HeaderField],
316-
transforming transform: (C) -> EncodableBodyContent<Date>
316+
contentType: String
317317
) throws -> Data {
318318
try setResponseBody(
319319
value,
320320
headerFields: &headerFields,
321-
transforming: transform,
321+
contentType: contentType,
322322
convert: convertDateToTextData
323323
)
324324
}
325325

326326
// | server | set | response body | JSON | codable | required | setResponseBodyAsJSON |
327-
func setResponseBodyAsJSON<T: Encodable, C>(
328-
_ value: C,
327+
func setResponseBodyAsJSON<T: Encodable>(
328+
_ value: T,
329329
headerFields: inout [HeaderField],
330-
transforming transform: (C) -> EncodableBodyContent<T>
330+
contentType: String
331331
) throws -> Data {
332332
try setResponseBody(
333333
value,
334334
headerFields: &headerFields,
335-
transforming: transform,
335+
contentType: contentType,
336336
convert: convertBodyCodableToJSON
337337
)
338338
}
339339

340340
// | server | set | response body | binary | data | required | setResponseBodyAsBinary |
341-
func setResponseBodyAsBinary<C>(
342-
_ value: C,
341+
func setResponseBodyAsBinary(
342+
_ value: Data,
343343
headerFields: inout [HeaderField],
344-
transforming transform: (C) -> EncodableBodyContent<Data>
344+
contentType: String
345345
) throws -> Data {
346346
try setResponseBody(
347347
value,
348348
headerFields: &headerFields,
349-
transforming: transform,
349+
contentType: contentType,
350350
convert: convertDataToBinary
351351
)
352352
}

Sources/OpenAPIRuntime/Conversion/CurrencyExtensions.swift

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -347,22 +347,20 @@ extension Converter {
347347
return values
348348
}
349349

350-
func setRequiredRequestBody<T, C>(
351-
_ value: C,
350+
func setRequiredRequestBody<T>(
351+
_ value: T,
352352
headerFields: inout [HeaderField],
353-
transforming transform: (C) -> EncodableBodyContent<T>,
353+
contentType: String,
354354
convert: (T) throws -> Data
355355
) throws -> Data {
356-
let body = transform(value)
357-
headerFields.add(name: "content-type", value: body.contentType)
358-
let convertibleValue = body.value
359-
return try convert(convertibleValue)
356+
headerFields.add(name: "content-type", value: contentType)
357+
return try convert(value)
360358
}
361359

362-
func setOptionalRequestBody<T, C>(
363-
_ value: C?,
360+
func setOptionalRequestBody<T>(
361+
_ value: T?,
364362
headerFields: inout [HeaderField],
365-
transforming transform: (C) -> EncodableBodyContent<T>,
363+
contentType: String,
366364
convert: (T) throws -> Data
367365
) throws -> Data? {
368366
guard let value else {
@@ -371,7 +369,7 @@ extension Converter {
371369
return try setRequiredRequestBody(
372370
value,
373371
headerFields: &headerFields,
374-
transforming: transform,
372+
contentType: contentType,
375373
convert: convert
376374
)
377375
}
@@ -419,16 +417,14 @@ extension Converter {
419417
return transformedValue
420418
}
421419

422-
func setResponseBody<T, C>(
423-
_ value: C,
420+
func setResponseBody<T>(
421+
_ value: T,
424422
headerFields: inout [HeaderField],
425-
transforming transform: (C) -> EncodableBodyContent<T>,
423+
contentType: String,
426424
convert: (T) throws -> Data
427425
) throws -> Data {
428-
let body = transform(value)
429-
headerFields.add(name: "content-type", value: body.contentType)
430-
let convertibleValue = body.value
431-
return try convert(convertibleValue)
426+
headerFields.add(name: "content-type", value: contentType)
427+
return try convert(value)
432428
}
433429

434430
func convertBinaryToData(

0 commit comments

Comments
 (0)