Skip to content

test: add regression tests for scalar value casting with asRoot - #831

Merged
romm merged 2 commits into
CuyZ:masterfrom
devnix:test/as-root-scalar-value-casting
Aug 10, 2026
Merged

test: add regression tests for scalar value casting with asRoot#831
romm merged 2 commits into
CuyZ:masterfrom
devnix:test/as-root-scalar-value-casting

Conversation

@devnix

@devnix devnix commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Route and query parameters from an HTTP request are always strings, and the mapper enables scalar value casting for them, as documented:

Route and query parameter values coming from an HTTP request are typically strings. The mapper automatically handles scalar value casting for these parameters: a string "42" will be properly mapped to an int parameter.

The existing asRoot test cases pass native values ('anotherQueryParameter' => 42); that path is never exercised with the strings a real framework provides. This adds two cases: one mapping all query parameters to a shaped array, one to an object alongside a #[FromRoute] parameter.

This is not a bug report: master already behaves correctly. The bug existed on 2.5.1 and was fixed incidentally by 578bd58 (chore: refactor HTTP request mapping), a commit not yet in any release. These tests pin the fix down.

Reproduction on 2.5.1:

$request = new HttpRequest(queryParameters: ['page' => '42']);

// Works.
(new MapperBuilder())->argumentsMapper()->mapArguments(
    fn (#[FromQuery] int $page) => [],
    $request,
); // => ['page' => 42]

// Fails.
(new MapperBuilder())->argumentsMapper()->mapArguments(
    /** @param array{page: int} $query */
    fn (#[FromQuery(asRoot: true)] array $query) => [],
    $request,
); // => MappingError: An error occurred at path page: Value '42' is not a valid integer.

These tests guard the fix. Reverting the change in HttpRequestNodeBuilder (excluding the asRoot key from allowScalarValueCastingForChildren()) makes both fail with Value '42' is not a valid integer; the other 56 cases in the file pass.

Happy to adjust naming or placement if you prefer them elsewhere.

devnix and others added 2 commits July 31, 2026 16:20
Route and query parameters coming from an HTTP request are strings, and the
mapper enables scalar value casting for them. The existing `asRoot` test cases
only use native values (`42` instead of `'42'`), so that path is never
exercised with the string values a framework actually provides.

These tests cover mapping all query parameters to a shaped array and to an
object, both with a string value mapped to an `int`.
@romm
romm merged commit 605ea8c into CuyZ:master Aug 10, 2026
16 checks passed
@romm

romm commented Aug 10, 2026

Copy link
Copy Markdown
Member

Thank you!

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.

2 participants