Skip to content

Commit edaec9c

Browse files
authored
Fix HTTPBody length calculation for utf8 strings. (#60)
This PR aims to fix the byte length calculation for utf8 strings.
1 parent c48f166 commit edaec9c

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

Sources/OpenAPIRuntime/Interface/HTTPBody.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public final class HTTPBody: @unchecked Sendable {
142142
public let iterationBehavior: IterationBehavior
143143

144144
/// Describes the total length of the body, if known.
145-
public enum Length: Sendable {
145+
public enum Length: Sendable, Equatable {
146146

147147
/// Total length not known yet.
148148
case unknown
@@ -542,10 +542,7 @@ extension HTTPBody {
542542
@inlinable public convenience init(
543543
_ string: some StringProtocol & Sendable
544544
) {
545-
self.init(
546-
ByteChunk(string),
547-
length: .known(string.count)
548-
)
545+
self.init(ByteChunk(string))
549546
}
550547

551548
/// Creates a new body with the provided async throwing stream of strings.

Tests/OpenAPIRuntimeTests/Interface/Test_HTTPBody.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ final class Test_Body: Test_Runtime {
185185
XCTAssertEqual(chunks, ["hel", "lo"].map { Array($0.utf8)[...] })
186186
}
187187

188+
func testUTF8String() async throws {
189+
XCTAssertEqual(HTTPBody("abc").length, .known(3))
190+
XCTAssertEqual(HTTPBody("🤘").length, .known(4))
191+
XCTAssertEqual(HTTPBody("\u{1f603}").length, .known(4))
192+
XCTAssertEqual(HTTPBody("árvíztűrő tükörfúrógép").length, .known(31))
193+
}
194+
188195
func testIterationBehavior_single() async throws {
189196
let sequence = AsyncStream(
190197
String.self,

0 commit comments

Comments
 (0)