Skip to content

Tests: Add direct tests for _wp_json_schema_matches_type(). - #14672

Open
rvpatel wants to merge 1 commit into
WordPress:trunkfrom
rvpatel:66267-followup-ba4c6de
Open

rvpatel wants to merge 1 commit into
WordPress:trunkfrom
rvpatel:66267-followup-ba4c6de

Conversation

@rvpatel

@rvpatel rvpatel commented Oct 21, 2026

Copy link
Copy Markdown

Summary

_wp_json_schema_matches_type() was added in r64592 / ba4c6de (#66267) and is currently only tested indirectly through wp_prepare_json_value_for_client(). This PR adds direct tests for it.

The new data provider covers:

  • whether the schema's type allows the requested type
  • a value with the right type that fails a constraint (e.g. minItems)
  • a schema with no type, anyOf or oneOf (returns false)
  • anyOf with a matching branch
  • anyOf where the type is in one branch and the valid value is in another (must be rejected, as the docblock says)
  • oneOf with no branch of the requested type, and with two matching branches
  • anyOf and oneOf together (both must match)
  • untyped branches taking the parent's type, when it allows the request and when it doesn't
  • a schema made only of invalid (non-array) branches
  • anyOf nested inside oneOf
  • object requested for an empty array

The patch only adds tests. The helper's code is unchanged.

Notes for reviewers

Two behaviours came up while writing these. Neither is introduced by r64592.

  1. The helper skips non-array branches only while checking them. Its final call to rest_validate_value_from_schema() still gets the full schema, so an invalid string branch next to a matching array branch causes a TypeError in rest-api.php. rest_find_any_matching_schema() fails the same way on such a schema, so I didn't add that case.
  2. rest_is_object() treats a PHP list such as array( 'a' ) as a valid object. So oneOf with an object branch next to an array branch can count as two matches.

Testing

  • phpunit --group json-schema: OK (92 tests, 149 assertions)
  • phpunit -c tests/phpunit/multisite.xml --group json-schema: OK (92 tests, 149 assertions)
  • phpcs tests/phpunit/tests/json-schema.php: no issues

Trac ticket: https://core.trac.wordpress.org/ticket/66267

Use of AI Tools

I used Claude Code (Anthropic) to help write the test cases and this description. I reviewed every case, ran the tests locally, and checked the results.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the WordPress Core Handbook for more details.

The helper added in [64592] was only exercised indirectly through
`wp_prepare_json_value_for_client()`. Add a data provider covering
explicit and inherited types, anyOf/oneOf branches, nested combining
keywords, and the case where the type and a valid value are found in
different branches.

Follow-up to [64592].

See #66267.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props ravipatel.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

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.

1 participant