Skip to content

APIRequest.payloadBytes: the request body exactly as received (v1.36.0) - #212

Merged
gmpassos merged 6 commits into
masterfrom
feature/payload-bytes
Nov 24, 2026
Merged

gmpassos merged 6 commits into
masterfrom
feature/payload-bytes

Conversation

@gmpassos

@gmpassos gmpassos commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What

  • APIRequest.payloadBytes: the body bytes as received, set by APIServer when it reads a body. They are read-only (an unmodifiable view).
  • APIRequest.payloadAsBytes (behavior change, hence 1.36.0): for a body received as text (a String payload, or a JSON or text MIME type), it returns a copy of those received bytes. Before, it re-encoded the decoded payload.

Why

The server decoded a JSON body and dropped its bytes. That left a route unable to verify a signature computed over the raw body, such as a webhook HMAC.

payloadAsBytes re-encoded the decoded payload, and that never gave back the bytes that were sent:

  • a JSON body came back as the toString of its Map ({a: 1}), which is not JSON;
  • a latin1 body came back as UTF-8.

Needed by AllDely's Open Delivery webhook (X-App-Signature).

How

  • _resolvePayload, _resolvePayloadFromString, _loadPayloadString and _resolvePayloadBytes return a named record ({mimeType, payload, bytes}) instead of discarding the bytes. toAPIRequest passes them to APIRequest.payloadBytes.
  • A JSON body that fails to decode has no payload, as before, but keeps its bytes.
  • The bytes are decompressed only when the server decompresses payloads (decompressPayload). They are null for a request built in code, and when the server reads no body.
  • Read-only, so a route can't change what a signature check reads. payloadAsBytes returns a copy for text bodies.
  • payloadAsBytes for a request built in code with a JSON MIME type: the payload is encoded as JSON, not as its toString.
  • A JSON body that fails to decode is logged with its length and its first 256 characters only (it comes from the client: it may be large, or hold credentials).
  • An empty body shares one Uint8List(0).

Behavior change

A route's Uint8List parameter filled from a text body (through APIModule) now gets the body as received. For example, a text/plain; charset=latin1 body keeps its latin1 bytes. The two upload(text:latin1) tests in bones_api_test.dart are updated to match.

Tests

  • test/bones_api_cov_base_test.dart: payloadAsBytes per case:
    • JSON (Map and List), latin1 text, any String;
    • a List<int> payload, a non-text MIME type, and requests built in code (JSON and non-JSON);
    • a body that failed to decode;
    • payloadBytes read-only, and the copy free to change;
    • a binary payload shares its buffer.
  • test/bones_api_cov_server_config_test.dart:
    • payloadBytes through toAPIRequest: JSON with odd spacing, invalid JSON, text, latin1, binary, empty, no body;
    • a gzip JSON body (the decompressed bytes);
    • the truncated log of a large invalid body.
  • test/bones_api_test.dart: over HTTP, a JSON body's payloadAsBytes keeps its spacing.
  • Passing: bones_api_cov_base_test, bones_api_cov_server_config_test, bones_api_cov_server_http_test, bones_api_test, bones_api_module_test, bones_api_platform_vm_test.

Version 1.36.0 (pubspec, BonesAPI.VERSION, CHANGELOG).

🤖 Generated with Claude Code

gmpassos and others added 5 commits November 23, 2026 23:29
The server decoded a JSON body and dropped its bytes, so a route could not
verify a signature computed over the raw body (e.g. a webhook HMAC):
payloadAsBytes re-encodes the decoded payload, which changes whitespace,
key order and number formats.

The payload helpers now return a named record with the bytes, and
toAPIRequest passes them to APIRequest.payloadBytes (also when the JSON
fails to decode).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For a body received as text (a String payload, or a JSON or text MIME type),
payloadAsBytes now returns payloadBytes instead of re-encoding the decoded
payload: a JSON Map's toString isn't JSON, and a latin1 body differed once
re-encoded as UTF-8. A route's Uint8List parameter filled from a text body
now gets the body as received.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Unit: payloadAsBytes for JSON (Map and List), text in latin1, any String,
  a List<int> that is the received bytes, a non-text MIME type, requests
  built in code, and a JSON body that failed to decode.
- A gzip-compressed body: payloadBytes is the decompressed body.
- Over HTTP: a JSON body's payloadAsBytes keeps its spacing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- payloadBytes is read-only (an unmodifiable view), and payloadAsBytes
  returns a copy of it for a text body: a route can no longer change the
  bytes a signature check reads.
- payloadAsBytes copies a List<int> payload, instead of returning the
  received bytes whenever the lengths matched.
- A request built in code with a JSON MIME type: payloadAsBytes encodes the
  payload as JSON, not its toString.
- A JSON body that fails to decode is logged with its length and its first
  256 characters, not all of it.
- Docs: payloadBytes is decompressed only with decompressPayload, null when
  no body is read, and costs memory for the request's life.
- 1.36.0 (minor): payloadAsBytes changes behavior.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gmpassos gmpassos changed the title APIRequest.payloadBytes: the request body exactly as received (v1.35.3) APIRequest.payloadBytes: the request body exactly as received (v1.36.0) Nov 24, 2026
…v1.36.0)

postgres 3.7 picks a prepared statement's session by the current zone: run
outside the runTx callback's zone, a call waits for the transaction to end
(it used to fail). A transaction's operations may run from another zone, so
a cached statement waited for its own transaction (CI hung in 'prepared
statements: reused, in transactions...' since CI's pub upgrade took 3.7.0).
PostgreSQLConnectionTransactionWrapper now runs cached statements in the
runTx callback's zone.

Dependencies: postgres ^3.7.0, sqlite3 ^3.9.2, archive ^4.7.1,
googleapis_auth ^2.3.7, dart_spawner ^1.3.1, coverage ^1.16.2. mysql1 stays
at ^0.22.0 (0.23 breaks the MySQL adapter: DECIMAL as String).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.95%. Comparing base (0463aac) to head (79e0d90).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #212      +/-   ##
==========================================
- Coverage   95.98%   95.95%   -0.04%     
==========================================
  Files          75       75              
  Lines       25943    25964      +21     
==========================================
+ Hits        24901    24913      +12     
- Misses       1042     1051       +9     
Flag Coverage Δ
unittests 95.95% <100.00%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gmpassos
gmpassos merged commit eb8b709 into master Nov 24, 2026
11 checks passed
gmpassos added a commit that referenced this pull request Nov 24, 2026
…ssed bodies, log preview

- payloadAsBytes: a String payload with a JSON MIME type is the JSON text
  (not encoded again, which quoted it); others encoded with the library's
  Json encoder (entities, DateTime, toJson). Documents the copy per call.
- APIServer: reads a chunked body with no Content-Type; passes a compressed
  body that isn't decompressed as bytes (octet-stream), not as garbage text;
  the JSON error log gives the body length before trimming and doesn't cut
  a surrogate pair.
- Docs: _resolvePayloadBytes (shared buffer), _runStatement (who calls it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant