Skip to content

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

@timbms

Description

@timbms

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

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
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugFeature doesn't work as expected.status/triageCollecting information required to triage the issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions