Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#51911 closed enhancement (fixed)

REST API: Make JSON Schema enum more robust for non-string types

Reported by: timothyblynjacobs's profile TimothyBlynJacobs Owned by: timothyblynjacobs's profile TimothyBlynJacobs
Milestone: 5.7 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: good-first-bug json-schema has-patch has-unit-tests
Focuses: Cc:

Description

The enum JSON schema keyword allows for enforcing that the provided value is one of the listed values. This can be used with any type of value. Our implementation, however, doesn't apply the same type coercion when doing regular validation.

In other words, for an enum of [ 0, 1 ] I should be able to pass "1".

To do this, I think we might want to move the enum validation to the end of rest_validate_value_from_schema so that type validation has already been applied, and then either call rest_sanitize_value_from_schema before passing the value to the enum check, or just do the type casting.

We'll also need to pay special care to arrays/objects applying rest_stabilize_value and making sure that the type safe in_array() check has the correct JSON semantics.

Change History (4)

This ticket was mentioned in PR #912 on WordPress/wordpress-develop by yakimun.


4 years ago
#1

  • Keywords has-patch has-unit-tests added; needs-patch removed

Added support for type coercion in enum JSON Schema keyword.

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

#2 @TimothyBlynJacobs
4 years ago

  • Milestone changed from Future Release to 5.7

#3 @TimothyBlynJacobs
4 years ago

  • Owner set to TimothyBlynJacobs
  • Resolution set to fixed
  • Status changed from new to closed

In 50010:

REST API: Support type coercion when validating the enum JSON Schema keyword.

Previously, the enum keyword was validated by perform a strict equality check. For string types this is generally ok, but it prevented using alternative types like number when rich type support isn't available.

Now the same level of type coercion/sanitization is applied when validating enum as all other validation checks. This means that a value of "1" will be accepted for an enum of [ 0, 1 ]. Additionally, object types now properly ignore key order when checking for equality.

Props yakimun.
Fixes #51911.

Note: See TracTickets for help on using tickets.