apple / apple/swift-openapi-urlsession

Crash when sending empty text/plain request body (precondition(!bytesToWrite.isEmpty)) for generated client operation

Open
#85 10 comments 0 reactions 0 assignees View on GitHub
kind/bug status/triage
Dominant language
Swift
Stars
216
Forks
44
PR merge metrics
No merged PRs in 30d

Description

### Description

### Summary
When a generated Swift OpenAPI client sends an empty string as a `text/plain` request body, the request crashes at runtime with:

`Precondition failed: writePendingBytes(_:) must be called with non-empty bytes`

This appears to come from the OpenAPI runtime/URLSession transport path (`HTTPBodyOutputStreamBridge`).

### Actual behavior
App crashes with precondition failure:
writePendingBytes(_:) must be called with non-empty bytes.

### Reproduction

### OpenAPI snippet
```yaml
paths:
/items/{itemname}:
post:
operationId: sendItemCommand
parameters:
- name: itemname
in: path
required: true
schema:
type: string
requestBody:
required: true
content:
text/plain:
schema:
type: string
responses:
"200":
description: OK
```

```swift
let path = Operations.sendItemCommand.Input.Path(itemname: "MyItem")
let body = Operations.sendItemCommand.Input.Body.plainText(.init(""))
let query = Operations.sendItemCommand.Input.Query()
_ = try await client.sendItemCommand(path: path, query: query, body: body)
```

### Package version(s)

swift-openapi-runtime: 1.8.0
swift-openapi-urlsession: 1.0.2

### Expected behavior

No crash.
Either:

- send a valid request with Content-Length: 0, or
- return a normal thrown error if empty body is unsupported.

### Environment

Xcode: 26.3 (17C529)
swift-openapi-runtime: 1.8.0
swift-openapi-urlsession: 1.0.2
Generated by swift-openapi-generator

### Additional information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the provided sendItemCommand reproduction and trace the OpenAPI runtime/URLSession transport path to HTTPBodyOutputStreamBridge. Verify how an empty text/plain body reaches writePendingBytes(_:), then confirm the completed behavior with the supplied Swift example: no crash and either a zero-length request or a normal thrown error.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, swift
Domain
api, networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.