fix(templates): nest the REST endpoint so 'schema' survives register_rest_route()'s upgrade step (C) - #16
Merged
Conversation
…rest_route()'s upgrade step (C) register_rest_route() upgrades a flat args array to multiple endpoints whenever it sees a top-level 'callback' key -- wrapping the *entire* array as a single numerically-indexed entry. Passing 'schema' as a sibling of 'callback' put it inside that entry too, where core's get_data_for_route() never looks for it: OPTIONS requests still returned no schema despite get_item_schema() being fully implemented. Fix: nest the endpoint definition one level deeper and lift 'schema' out as a true route-level option, matching core's own controllers (WP_REST_Settings_Controller et al). Added a regression test that spies on the actual register_rest_route() call and asserts 'schema' is a route option (not nested inside the endpoint) and the endpoint itself is present at the numeric key -- the existing test only asserted the third arg is *an* array, which passes regardless of nesting and could not have caught this. Verified end-to-end against a real --modules rest_api,caching scaffold: composer test -> 13/13 PHPUnit tests (19 assertions, was 12/12 before the regression test), composer lint -> 15/15 files, 0 errors/0 warnings (one reserved-keyword warning from the new test's $namespace closure param, caught by the same lint run, renamed to $route_namespace before landing).
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.
Closes the remaining half of item C from the open-issues report — the previous PR (#15) implemented
get_item_schema()correctly but wired it intoregister_routes()at the wrong nesting level, so core silently dropped it.The bug
register_rest_route()upgrades a flat args array to multiple endpoints whenever it sees a top-level'callback'key — wrapping the entire array as a single numerically-indexed entry:'schema'was a sibling of'callback'in that same flat array, so it got swallowed into that numeric entry along with everything else.get_data_for_route()looks forschemaamong the route's string keys, where it no longer existed —OPTIONS /wp-json/.../itemskept returning no schema, the exact gap #15 was meant to close.The fix
Nest the endpoint definition one level deeper and lift
'schema'out as a genuine route-level option, matching how core's own controllers do it (WP_REST_Settings_Controlleret al):get_item_schema()and thetests/bootstrap.phpstub extension from #15 were both already correct and are untouched.Regression test
The existing
test_register_routesonly asserts the third argument is an array (Mockery::type('array')), which passes regardless of nesting — it could not have caught this. Added a test that spies on the actual call:Verification
npm test(CLI's own suite): 69/69.--modules rest_api,cachingscaffold:composer test→ 13/13 PHPUnit tests, 19 assertions (was 12/12 before this regression test).composer lint→ 15/15 files, 0 errors / 0 warnings. First run flagged the new test's closure parameter named$namespaceas a reserved-keyword warning (WPCSNoReservedKeywordParameterNames) — caught by that same lint run and fixed by renaming to$route_namespacebefore landing.D (test suite depth) remains out of scope, per the report.
🤖 Generated with Claude Code