-
Notifications
You must be signed in to change notification settings - Fork 3.5k
REST API: Add a shared helper for JSON Schema allowed keywords #12234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gziolo
wants to merge
4
commits into
WordPress:trunk
Choose a base branch
from
gziolo:add/64955-json-schema-allowed-keywords
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9749b5f
REST API: Add a shared helper for JSON Schema allowed keywords.
gziolo 69d8205
REST API: Clarify JSON Schema keyword helper after review.
gziolo d99f15d
REST API: Type the abilities schema keyword lookup property.
gziolo 435808d
REST API: Clarify the draft-04 schema keyword profile.
gziolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?php | ||
| /** | ||
| * JSON Schema API: shared functions for working with JSON Schema. | ||
| * | ||
| * @package WordPress | ||
| * @subpackage JSON_Schema | ||
| * @since 7.1.0 | ||
| */ | ||
|
|
||
| /** | ||
| * Gets the JSON Schema keywords allowed for a given schema profile. | ||
| * | ||
| * Use the returned list to decide which keywords to keep when a schema is | ||
| * output as JSON. Both profiles describe JSON Schema draft-04 output, also | ||
| * called JSON Schema Version 4. They differ only in how much of the keyword | ||
| * vocabulary stays in the result. | ||
| * | ||
| * - 'rest-api' returns the subset of draft-04 that the WordPress REST API | ||
| * uses for route output. This is the default. | ||
| * - 'draft-04' returns the larger draft-04 set used when publishing a schema | ||
| * as a standalone document to clients, such as the Abilities API. It keeps | ||
| * documentation and passthrough keywords like '$ref', 'definitions', | ||
| * 'allOf', 'not', 'dependencies', and 'additionalItems'. | ||
| * | ||
| * The keywords are allowed to stay in the schema output. This does not mean | ||
| * WordPress validates or sanitizes values against them. | ||
| * | ||
| * @since 7.1.0 | ||
| * | ||
| * @param string $schema_profile Optional. Name of the schema profile to get keywords for. | ||
| * Accepts 'rest-api' or 'draft-04'. Any other value falls | ||
| * back to the 'rest-api' profile. Default 'rest-api'. | ||
| * @return string[] Allowed JSON Schema keywords. | ||
| */ | ||
| function wp_get_json_schema_allowed_keywords( string $schema_profile = 'rest-api' ): array { | ||
| $rest_keywords = rest_get_allowed_schema_keywords(); | ||
|
|
||
| $keywords_by_profile = array( | ||
| 'rest-api' => $rest_keywords, | ||
| 'draft-04' => array_merge( | ||
| array( | ||
| '$schema', | ||
| 'id', | ||
| '$ref', | ||
| ), | ||
| $rest_keywords, | ||
| array( | ||
| 'required', | ||
| 'allOf', | ||
| 'not', | ||
| 'definitions', | ||
| 'dependencies', | ||
| 'additionalItems', | ||
| ) | ||
| ), | ||
| ); | ||
|
|
||
| $allowed_keywords = $keywords_by_profile[ $schema_profile ] ?? $rest_keywords; | ||
|
|
||
| /** | ||
| * Filters the JSON Schema keywords allowed for a given schema profile. | ||
| * | ||
| * Adding a keyword lets it stay in the schema output for that profile. | ||
| * It does not make WordPress validate or sanitize values against the keyword. | ||
| * | ||
| * @since 7.1.0 | ||
| * | ||
| * @param string[] $allowed_keywords Allowed JSON Schema keywords. | ||
| * @param string $schema_profile The schema profile the keywords are for. | ||
| */ | ||
| return apply_filters( 'wp_json_schema_allowed_keywords', $allowed_keywords, $schema_profile ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
| /** | ||
| * JSON Schema functions. | ||
| * | ||
| * @package WordPress | ||
| * @subpackage JSON_Schema | ||
| */ | ||
|
|
||
| /** | ||
| * @group json-schema | ||
| */ | ||
| class Tests_JSON_Schema extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * @ticket 64955 | ||
| */ | ||
| public function test_wp_get_json_schema_allowed_keywords_uses_rest_keywords_by_default() { | ||
| $this->assertSame( rest_get_allowed_schema_keywords(), wp_get_json_schema_allowed_keywords() ); | ||
| $this->assertSame( rest_get_allowed_schema_keywords(), wp_get_json_schema_allowed_keywords( 'rest-api' ) ); | ||
| $this->assertSame( rest_get_allowed_schema_keywords(), wp_get_json_schema_allowed_keywords( 'unknown-context' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64955 | ||
| */ | ||
| public function test_wp_get_json_schema_allowed_keywords_includes_draft_04_keywords() { | ||
| $keywords = wp_get_json_schema_allowed_keywords( 'draft-04' ); | ||
|
|
||
| // Keywords the draft-04 profile adds on top of the REST keyword set. | ||
| foreach ( array( '$schema', 'id', '$ref', 'required', 'allOf', 'not', 'definitions', 'dependencies', 'additionalItems' ) as $keyword ) { | ||
| $this->assertContains( $keyword, $keywords ); | ||
| } | ||
|
|
||
| // 'type' is a base REST keyword, not a draft-04 addition. Checking it | ||
| // confirms the draft-04 profile is a superset that keeps the REST keywords. | ||
| $this->assertContains( 'type', $keywords ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64955 | ||
| */ | ||
| public function test_wp_get_json_schema_allowed_keywords_filter_receives_schema_profile() { | ||
| $schema_profiles = array(); | ||
| $filter = static function ( $keywords, $schema_profile ) use ( &$schema_profiles ) { | ||
| $schema_profiles[] = $schema_profile; | ||
| $keywords[] = 'xCustomKeyword'; | ||
|
|
||
| return $keywords; | ||
| }; | ||
|
|
||
| add_filter( 'wp_json_schema_allowed_keywords', $filter, 10, 2 ); | ||
| $keywords = wp_get_json_schema_allowed_keywords( 'draft-04' ); | ||
|
|
||
| $this->assertContains( 'xCustomKeyword', $keywords ); | ||
| $this->assertSame( array( 'draft-04' ), $schema_profiles ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not fully familiar with this area of WP, but
draft-04looks strange, are you sure this is correct?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
REST API uses a subset of draft-04 with elements from draft-03 JSON Schema specification. Abilities API allows using the semantic format for input and output schema as a REST API, but it transforms it to draft-04 when sending through the REST API to ensure that on the client, it gets the same validation against stricter draft-04.
The intent is correct, and the naming can have adjustments based on the feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a pass on the code documentation to explain it better in 435808d.