Skip to content

fix(middleware): log responses that are not JSON - #47

Merged
jfrosorio merged 2 commits into
mainfrom
fix-45
Oct 10, 2026
Merged

jfrosorio merged 2 commits into
mainfrom
fix-45

Conversation

@jfrosorio

Copy link
Copy Markdown
Contributor

Closes #45.

Requests whose response body did not decode to a non-null JSON value were never logged: null was written to the non-nullable response_data column and the insert failed. This hit HTML error pages (a 403/404 for an Accept: */* client), empty 204 bodies, and streamed and file responses.

LogApiRequest now stores a JSON array redacted as before, any other non-null JSON value as is, and any other body as a string with invalid UTF-8 scrubbed so the json cast cannot fail on it. Streamed and file responses, which have no content to read, are stored as an empty string. No schema change.

The redaction docs now state that key redaction applies to JSON responses only. Feature tests cover an HTML response, a 204, a streamed response and a body that is not valid UTF-8.

Copilot AI balanced review requested due to automatic review settings October 10, 2026 14:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

mb_scrub() is only available on PHP 8.3+, so the middleware will fatally error on PHP 8.2, which the package still declares as supported and tests in CI.

1 open finding
What changed in this PR

This PR fixes a bug in the codetech/laravel-api-logs package where responses whose body did not decode to a non-null JSON value were never logged: null was written to the non-nullable response_data column, causing the insert to fail (issue #45). This affected HTML error pages, empty 204 responses, and streamed/file responses.

The middleware now stores a redacted JSON array as before, any other non-null JSON value as-is, streamed/file responses (no readable content) as an empty string, and any other body as a UTF-8-scrubbed string so the json cast cannot fail. Documentation and feature tests were updated accordingly.

Changes:

  • Extract response handling into a new responseData() method that never returns null, falling back to an empty string (no content) or a scrubbed raw body.
  • Document that key redaction applies to JSON responses only.
  • Add feature tests for HTML, 204, streamed, and non-UTF-8 responses.
File Description
src/​Http/​Middleware/​LogApiRequest.php Adds responseData() to safely store non-JSON/empty/streamed bodies instead of writing null.
tests/​Feature/​LogApiRequestMiddlewareTest.php Adds routes and tests covering HTML, 204, streamed, and binary responses.
docs/​redaction.md Clarifies that key redaction applies only to JSON responses.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/Http/Middleware/LogApiRequest.php
@jfrosorio
jfrosorio merged commit f27d63b into main Oct 10, 2026
10 checks passed
@jfrosorio
jfrosorio deleted the fix-45 branch October 10, 2026 15:00
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.

Log responses that are not JSON on the 3.x line

2 participants