Return HTTP 400 for malformed date-range filter input - #42
Merged
Conversation
DateRangeFilter threw PHP's native \InvalidArgumentException for a
malformed `between` value and a raw \Error for an unknown operator
(DateLimit::{unknownKey}). Neither is mapped in exception_to_status, so
both leaked as HTTP 500.
Resolve the operator via a name lookup and throw ApiPlatform's
InvalidArgumentException on both invalid paths, honouring the
per-field `throwOnInvalid` flag (skip the clause when false). Fix the
stale exception_to_status mapping to the current
ApiPlatform\Metadata\Exception\InvalidArgumentException namespace so it
maps to 400. Unskip FilterErrorTest and extend it with the
unknown-operator and non-date-value (Elasticsearch-rejected) cases.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #42 +/- ##
=============================================
+ Coverage 71.89% 72.68% +0.78%
- Complexity 226 232 +6
=============================================
Files 29 29
Lines 637 648 +11
=============================================
+ Hits 458 471 +13
+ Misses 179 177 -2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Malformed date-range filter input now returns HTTP 400 instead of leaking as a 500.
Changes
DateRangeFilterresolves the client operator via a name lookup and throwsApiPlatform\Metadata\Exception\InvalidArgumentExceptionon both invalid paths — a malformedbetweenvalue and an unknown operator (field[foo]=…). The per-fieldthrowOnInvalidflag is now honoured: whenfalsethe clause is skipped instead of throwing.config/packages/api_platform.yaml: fix the staleexception_to_statusmapping to the currentApiPlatform\Metadata\Exception\InvalidArgumentExceptionnamespace (the oldApiPlatform\Exception\InvalidArgumentExceptionclass no longer exists, so the mapping was inert).classConstant.nameTypeentry from the PHPStan baseline (the dynamicDateLimit::{…}fetch is gone).FilterErrorTestand extend it with the unknown-operator and non-date-value (Elasticsearch-rejected →ElasticIndexException→ 400) cases.DateRangeFilterTestunit pins to the new behaviour.Why
Two filter error paths leaked as HTTP 500: a malformed
betweenthrew PHP's native\InvalidArgumentException, and an unknown operator hit a raw\ErrorfromDateLimit::{unknownKey}. Neither type was inexception_to_status. This is a prerequisite hardening step before the API Platform upgrade — malformed client input must surface as a 4xx, and the change is backwards-compatible (500 → 400 only).