Make WordPress Core

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

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's hex-color example.

This change recognizes a supported metadata type combined with null while retaining the complete schema for validation and sanitization. It also preserves the existing conversion of empty database strings to false, 0, and 0.0 for nullable boolean, integer, and number fields. Other unsupported type arrays remain excluded.

Requests containing null continue through the existing deletion/default path, including its checks for invalid stored values; this does not add storage of SQL NULL values. 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.

  • Clean production baseline: 34 cases, 28 expected failures and 6 passing compatibility controls; no setup errors.
  • Final focused regression tests: 34 tests, 163 assertions passed.
  • Complete WP_Test_REST_Post_Meta_Fields class: 185 tests, 744 assertions passed.
  • npm run build:dev: passed.
  • PHPCS and PHP compatibility checks for both changed files: passed.
  • Independent automated implementation review: no remaining blocking findings.
  • Full REST selection through the serialized native test helper: 59 classes, 3,179 tests and 15,259 assertions; six pre-existing Windows attachment file-size failures, four pre-existing PHPUnit warnings, and six skipped cases. No nullable-metadata or other new failure was observed. The six attachment failures and four warning cases were reproduced with production source restored to the exact base commit. The local configuration's unrelated test-title mismatch was corrected to the upstream sample value before this final run.

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's G:/... 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

  1. Register the backgroundColor post meta from the Trac ticket, with storage type string, single => true, and REST schema type => array( 'string', 'null' ), format => 'hex-color'.
  2. Set a valid value such as #123456 on a post and request /wp/v2/posts/<id> and the endpoint's OPTIONS schema. Confirm the metadata property and its nullable schema are present.
  3. As a user allowed to edit the post, update the color through REST. A valid color must persist; an invalid color must be rejected without replacing the stored value.
  4. With a valid color stored, send null for 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.

​@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.

Note: See TracTickets for help on using tickets.