Opened 5 years ago
Last modified 3 weeks ago
#53828 new defect (bug)
Nullable registered meta with format does not display in JSON schema
| Reported by: | chrisvanpatten | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | REST API | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: | rest-api |
Description
When registering a piece of meta as a nullable type in the REST schema, it does not display in the JSON schema for the particular object.
For example, the following will not correctly register a meta key in REST:
<?php register_meta( 'post', 'backgroundColor', [ 'single' => true, 'type' => 'string', 'show_in_rest' => [ 'schema' => [ 'title' => __( 'Background Color' ), 'type' => [ 'string', 'null' ], 'format' => 'hex-color', ], ], ], );
The key will not be visible in the JSON schema, and the key/value pair will also not be available in the meta object in the post response from the API.
Change History (2)
This ticket was mentioned in PR #14298 on WordPress/wordpress-develop by @salvatorecorsi.
3 weeks ago
#1
- Keywords has-patch has-unit-tests added
@salvatorecorsi commented on PR #14298:
3 weeks ago
#2
The remaining failed check is Upgrade from 7.1 / PHP 8.4 with MySQL 8.4. It fails while provisioning WP-CLI (Could not setup wp-cli), followed by wp: command not found and exit 127, before the upgrade tests execute. The other 120 checks passed; 20 were skipped.
I attempted a targeted rerun of that job, but GitHub returned HTTP 403: repository administration rights are required. Could a maintainer rerun this failed job when convenient? No code change is needed to address this provisioning failure.
This CI investigation and comment were prepared with Codex. No human review is claimed.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
REST metadata registered with a nullable schema such as
array( 'string', 'null' )is currently omitted from both the endpoint schema and the response. This prevents clients from discovering or editing a registered optional field, including the ticket'shex-colorexample.This change recognizes a supported metadata type combined with
nullwhile retaining the complete schema for validation and sanitization. It also preserves the existing conversion of empty database strings tofalse,0, and0.0for nullable boolean, integer, and number fields. Other unsupported type arrays remain excluded.Requests containing
nullcontinue through the existing deletion/default path, including its checks for invalid stored values; this does not add storage of SQLNULLvalues. Existing format constraints, explicit defaults, object/array schema constraints, and multi-value rules continue to apply.Testing
The tests exercise actual REST requests and disposable database state rather than mocking the metadata handler. They cover both nullable type orders, all six supported base types, schema discovery, saved values, format rejection without mutation, reset/default behavior, multi-value rules, and empty-string scalar casting.
WP_Test_REST_Post_Meta_Fieldsclass: 185 tests, 744 assertions passed.npm run build:dev: passed.Native validation uses PHP 8.3.28 on Windows. Full PHPStan reports three unchanged legacy-theme hook-documentation path errors, reproduced with production source restored to the base commit. The local resolver compares mixed Windows path separators; the referenced files exist. No PHPStan rule or baseline was changed to hide these findings. The six attachment failures arise because
get_attached_file()treats the native fixture'sG:/...temporary path as relative, so the fallback cannot read the existing file; this is outside the metadata-schema change. No full clean REST suite, full clean PHPStan, or Docker E2E pass is claimed.Manual reproduction
backgroundColorpost meta from the Trac ticket, with storage typestring,single => true, and REST schematype => array( 'string', 'null' ), format => 'hex-color'.#123456on a post and request/wp/v2/posts/<id>and the endpoint's OPTIONS schema. Confirm the metadata property and its nullable schema are present.nullfor the field and verify its stored metadata is removed and its default is returned. Repeat with the schema's type order reversed.Use of AI Tools
AI assistance: Yes. OpenAI Codex investigated the ticket, authored the implementation and regression tests, executed the recorded checks, and performed automated review with additional Codex agents. The account owner has not performed a human review of this patch; maintainer review is requested. No unavailable test is reported as passing.
---
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 for more details.