Skip to content

Refactor ApiHeaderValidationMiddleware: - #1114

Draft
cn-tools wants to merge 8 commits into
Kovah:2.xfrom
cn-tools:2.x
Draft

Refactor ApiHeaderValidationMiddleware:#1114
cn-tools wants to merge 8 commits into
Kovah:2.xfrom
cn-tools:2.x

Conversation

@cn-tools

@cn-tools cn-tools commented May 27, 2026

Copy link
Copy Markdown

Hi, I made these changes because I'm having a problem with an Android app where a "charset" is also included in the Content-Type header.

For example: Content-Type "application/json; charset=utf-8".

Copilot also confirmed in a code review that the character set can be included here. Android also includes the "charset" by default.

The following changes was made:

  • Enhance ApiHeaderValidationMiddleware to support additional HTTP methods and improve content negotiation logic (RFC9110, RFC8259)
  • update tests for correct header handling

* Cleans up header validation for Content-Type and Accept
* Corrects the logic so that JSON requests are validated correctly
* Encapsulates checks in helper methods for improved readability and maintainability
@codacy-production

codacy-production Bot commented May 27, 2026

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 duplication

Metric Results
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@Kovah

Kovah commented May 27, 2026

Copy link
Copy Markdown
Owner

Hi there, were those changed made because of a real necessity or issues you had with the way it worked before? Your description is a little sparse on that.

@cn-tools

Copy link
Copy Markdown
Author

Hi there, i add more infos in the first comment. ;)

@Kovah

Kovah commented May 27, 2026

Copy link
Copy Markdown
Owner

Alright, sounds good. Could you please extend the tests with your specific use case?

@cn-tools
cn-tools marked this pull request as draft May 27, 2026 13:57
cn-tools added 2 commits May 28, 2026 07:33
…ods and improve content negotiation logic; update tests for correct header handling (RFC9110, RFC8259)
@cn-tools
cn-tools marked this pull request as ready for review May 28, 2026 05:44
@cn-tools

cn-tools commented May 28, 2026

Copy link
Copy Markdown
Author

After an additional research, i made a full rework based on RFC 9110 and RFC 8259. The tests are extended too.

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.

Pull request overview

This PR refactors API header validation to better handle JSON media types with parameters and improve API content negotiation behavior.

Changes:

  • Adds parsing for Content-Type values such as application/json; charset=utf-8.
  • Introduces Accept-header negotiation with q-values and wildcard handling.
  • Adds happy-path middleware tests for charset and q-value Accept headers.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
app/Http/Middleware/ApiHeaderValidationMiddleware.php Refactors API header validation, content negotiation, and error response generation.
tests/Middleware/ContentTypeHeaderValidationMiddlewareTest.php Adds new success cases for charset and Accept-header handling.

Comment thread app/Http/Middleware/ApiHeaderValidationMiddleware.php Outdated
Comment thread tests/Middleware/ContentTypeHeaderValidationMiddlewareTest.php Outdated
Comment thread app/Http/Middleware/ApiHeaderValidationMiddleware.php
@cn-tools
cn-tools marked this pull request as draft May 28, 2026 09:44
…ponse error type codes in the header validation middleware test unit.
@cn-tools
cn-tools marked this pull request as ready for review May 29, 2026 06:15
@Kovah

Kovah commented Jun 5, 2026

Copy link
Copy Markdown
Owner

Most looks good, still unsure about the response format. Will have to think about that.
In the meantime, could you have a look at failing tests?

@Kovah

Kovah commented Jun 21, 2026

Copy link
Copy Markdown
Owner

I have taken another look at this and there are three changes I would like to comment on:

  • As far as I understand 9110 and the related parts, a missing Accept should be handled as */*, but the code currently rejects those requests. This should be changed.
  • The error format, although now compliant to 7807, it diverges from the current standard and I see no significant reason to make this breaking change right now. This should be changed back to the old format.
  • $this->isApiRequest($request) should be removed, as the middleware is applied to API requests only.

@Kovah Kovah added Enhancement Any requests for improvements or new features awaiting Response Issue is awaiting response for a long time already. labels Jun 21, 2026
@cn-tools
cn-tools marked this pull request as draft August 9, 2026 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting Response Issue is awaiting response for a long time already. Enhancement Any requests for improvements or new features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants