Skip to content

✨ Compile WP args schema from #[Param] attribute on handler parameters#40

Merged
dimitriBouteille merged 2 commits into
developfrom
feat/schema-support
May 14, 2026
Merged

✨ Compile WP args schema from #[Param] attribute on handler parameters#40
dimitriBouteille merged 2 commits into
developfrom
feat/schema-support

Conversation

@dimitriBouteille

Copy link
Copy Markdown
Owner

Summary

Add #[Param] attribute on handler parameters so the args schema is compiled into WordPress's register_rest_route instead of being re-implemented inside every handler. WordPress now enforces required / type / enum / sanitize_callback before the handler runs, and the auto-discovery endpoint (/wp-json/<ns>) describes the route arguments.

Why

RouteLoader::buildRouteArgs() was always emitting 'args' => [], so:

  • Validation had to be duplicated in every handler.
  • Misuse (missing required field) hit the handler before being rejected.
  • The discovery endpoint exposed nothing useful to API consumers.

Usage

#[Route('blog/v1', '/articles/(?P<id>\d+)')]
class ArticleRoute
{
    #[Action(Method::GET)]
    public function show(
        #[Param(required: true, description: 'Article identifier')]
        int $id,
        #[Param(default: 10, enum: [10, 25, 50])]
        int $perPage,
    ): \WP_REST_Response { ... }
}

Compiled into:

  'args' => [
      'id' => [
          'type' => 'integer',
          'required' => true,
          'description' => 'Article identifier',
          'sanitize_callback' => 'absint',
      ],
      'perPage' => [
          'type' => 'integer',
          'default' => 10,
          'enum' => [10, 25, 50],
          'sanitize_callback' => 'absint',
      ],
  ]

Type inference (when Param::$type is null)

Backward compatibility

Handlers without #[Param] keep registering with empty args — nothing else changes.

@dimitriBouteille dimitriBouteille added the enhancement New feature or request label May 13, 2026
@dimitriBouteille dimitriBouteille merged commit 0ac69f9 into develop May 14, 2026
5 checks passed
@dimitriBouteille dimitriBouteille deleted the feat/schema-support branch May 14, 2026 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant