Skip to content

Commit 3c21ae3

Browse files
authored
[Runtime] Choose the serialization method based on content type (#12)
[Runtime] Choose the serialization method based on content type ### Motivation Resolves apple/swift-openapi-generator#43. This PR also resolves apple/swift-openapi-generator#42. It's paired with apple/swift-openapi-generator#48. This PR introduces the initial content-type-aware coding strategy, which is likely to get expanded in the future. ### Modifications This PR covers the scope described in issue 43 - handling a `type: string` appropriately, based on whether it's wrapped in `application/json` or `text/plan` content. It introduces new variants of the SPI helper functions, which now has variants per "coding strategy". It's best to read https://github.com/apple/swift-openapi-generator/pull/48/files?short_path=aa92e58#diff-aa92e58fb5a311512cfcd285827a4aa2e6634cae7fdfe0090bd2cfc7b0986140 for details. Deprecated all of the SPI methods that didn't contain the coding strategy parameter, to be removed in the next breaking version. ### Result We now have explicit control over the coding strategy for ambiguous types, such as `String`, which conform to both `Codable` and `LosslessStringConvertible`, to be taken advantage of by the generator (see the paired PR). ### Test Plan Verified that the default behavior now matches version 0.1.0, and when paired with the updated generator, fully resolves issue 43. Used code coverage to ensure that `Converter+{Common,Client,Server}.swift` are now 100% unit tested. Reviewed by: simonjbeaumont Builds: ✔︎ pull request validation (5.8) - Build finished. ✔︎ pull request validation (5.9) - Build finished. ✔︎ pull request validation (nightly) - Build finished. ✔︎ pull request validation (soundness) - Build finished. #12
1 parent 6d7f2e9 commit 3c21ae3

16 files changed

+2100
-1343
lines changed

Sources/OpenAPIRuntime/Base/EncodableBodyContent.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,22 @@
1414

1515
/// A wrapper of a body value with its content type.
1616
@_spi(Generated)
17-
public struct EncodableBodyContent<T: Encodable & Equatable>: Equatable {
17+
public struct EncodableBodyContent<T: Equatable>: Equatable {
1818

1919
/// An encodable body value.
2020
public var value: T
2121

2222
/// The header value of the content type, for example `application/json`.
2323
public var contentType: String
2424

25-
/// Creates a new content wrapper around the specified value and content type.
26-
public init(value: T, contentType: String) {
25+
/// Creates a new content wrapper.
26+
/// - Parameters:
27+
/// - value: An encodable body value.
28+
/// - contentType: The header value of the content type.
29+
public init(
30+
value: T,
31+
contentType: String
32+
) {
2733
self.value = value
2834
self.contentType = contentType
2935
}

0 commit comments

Comments
 (0)