-
Notifications
You must be signed in to change notification settings - Fork 140
Allow URL metric schema to be extended #1492
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a52381d
Allow URL metric schema to be extended
westonruter e244b22
Add get() and __get() methods to OD_URL_Metric
westonruter 079e6d9
Account for type being an array
westonruter bbc97dc
Add od_url_metric_collected action
westonruter 70fd825
Add todos related to store_url_metric()
westonruter 381c829
Adopt Yoast SEO scheme for filter _doing_it_wrong
westonruter e85c683
Fix and clarify message for disallowed required property in extended …
westonruter 0cc9103
Add sanitization in addition to validation to ensure expected PHP types
westonruter 00d57b4
Persist unknown properties in stored URL Metrics but reject in new UR…
westonruter 22711e4
Add return description
westonruter 384fa53
Use properties instead of get methods
westonruter ed58151
Replace remaining use of get_timestamp
westonruter aa1e663
Disallow default value for schema
westonruter 8917ce9
Explicitly fork rest_default_additional_properties_to_false()
westonruter 907d8c3
Add return description for magic getter
westonruter 0db296f
Eliminate magic getter
westonruter ea28aa1
Merge branch 'trunk' of https://github.com/WordPress/performance into…
westonruter 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
Persist unknown properties in stored URL Metrics but reject in new UR…
…L Metric storage requests
- Loading branch information
commit 00d57b46c4a1a5105d3a0f5ca5bc30b46df15705
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
plugins/optimization-detective/class-od-strict-url-metric.php
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,63 @@ | ||
| <?php | ||
| /** | ||
| * Optimization Detective: OD_Strict_URL_Metric class | ||
| * | ||
| * @package optimization-detective | ||
| * @since n.e.x.t | ||
| */ | ||
|
|
||
| // Exit if accessed directly. | ||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| exit; | ||
| } | ||
|
|
||
| /** | ||
| * Representation of the measurements taken from a single client's visit to a specific URL without additionalProperties allowed. | ||
| * | ||
| * This is used exclusively in the REST API endpoint for capturing new URL metrics to prevent invalid additional data from being | ||
| * submitted in the request. For URL metrics which have been stored the looser OD_URL_Metric class is used instead. | ||
| * | ||
| * @since n.e.x.t | ||
| * @access private | ||
| */ | ||
| final class OD_Strict_URL_Metric extends OD_URL_Metric { | ||
|
|
||
| /** | ||
| * Gets JSON schema for URL Metric without additionalProperties. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return array<string, mixed> Schema. | ||
| */ | ||
| public static function get_json_schema(): array { | ||
| return self::falsify_additional_properties( parent::get_json_schema() ); | ||
| } | ||
|
|
||
| /** | ||
| * Recursively processes the schema to ensure that all objects have additionalProperties set to false. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param mixed $schema Schema. | ||
| * @return mixed Processed schema. | ||
| */ | ||
| private static function falsify_additional_properties( $schema ) { | ||
| if ( ! isset( $schema['type'] ) ) { | ||
| return $schema; | ||
| } | ||
| if ( 'object' === $schema['type'] ) { | ||
| $schema['additionalProperties'] = false; | ||
| if ( isset( $schema['properties'] ) && is_array( $schema['properties'] ) ) { | ||
| $schema['properties'] = array_map( | ||
| static function ( $property_schema ) { | ||
| return self::falsify_additional_properties( $property_schema ); | ||
| }, | ||
| $schema['properties'] | ||
| ); | ||
| } | ||
| } elseif ( 'array' === $schema['type'] && isset( $schema['items'] ) && is_array( $schema['items'] ) ) { | ||
| $schema['items'] = self::falsify_additional_properties( $schema['items'] ); | ||
| } | ||
| return $schema; | ||
| } | ||
| } | ||
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
62 changes: 62 additions & 0 deletions
62
plugins/optimization-detective/tests/test-class-od-strict-url-metric.php
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,62 @@ | ||
| <?php | ||
| /** | ||
| * Tests for optimization-detective class OD_Strict_URL_Metric. | ||
| * | ||
| * @package optimization-detective | ||
| * | ||
| * @coversDefaultClass OD_Strict_URL_Metric | ||
| */ | ||
| class Test_OD_Strict_URL_Metric extends WP_UnitTestCase { | ||
| use Optimization_Detective_Test_Helpers; | ||
|
|
||
| /** | ||
| * Tests get_json_schema(). | ||
| * | ||
| * @covers ::get_json_schema | ||
| */ | ||
| public function test_get_json_schema(): void { | ||
| add_filter( | ||
| 'od_url_metric_schema_root_additional_properties', | ||
| static function ( array $properties ) { | ||
| $properties['colors'] = array( | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'hex' => array( | ||
| 'type' => 'string', | ||
| ), | ||
| ), | ||
| 'additionalProperties' => true, | ||
| ); | ||
| return $properties; | ||
| } | ||
| ); | ||
| add_filter( | ||
| 'od_url_metric_schema_element_item_additional_properties', | ||
| static function ( array $properties ) { | ||
| $properties['region'] = array( | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'continent' => array( | ||
| 'type' => 'string', | ||
| ), | ||
| ), | ||
| 'additionalProperties' => true, | ||
| ); | ||
| return $properties; | ||
| } | ||
| ); | ||
| $loose_schema = OD_URL_Metric::get_json_schema(); | ||
| $this->assertTrue( $loose_schema['additionalProperties'] ); | ||
| $this->assertFalse( $loose_schema['properties']['viewport']['additionalProperties'] ); // The viewport is never extensible. Only the root and the elements are. | ||
| $this->assertTrue( $loose_schema['properties']['elements']['items']['additionalProperties'] ); | ||
| $this->assertTrue( $loose_schema['properties']['elements']['items']['properties']['region']['additionalProperties'] ); | ||
| $this->assertTrue( $loose_schema['properties']['colors']['additionalProperties'] ); | ||
|
|
||
| $strict_schema = OD_Strict_URL_Metric::get_json_schema(); | ||
| $this->assertFalse( $strict_schema['additionalProperties'] ); | ||
| $this->assertFalse( $strict_schema['properties']['viewport']['additionalProperties'] ); | ||
| $this->assertFalse( $strict_schema['properties']['elements']['items']['additionalProperties'] ); | ||
| $this->assertFalse( $strict_schema['properties']['elements']['items']['properties']['region']['additionalProperties'] ); | ||
| $this->assertFalse( $strict_schema['properties']['colors']['additionalProperties'] ); | ||
| } | ||
| } |
Oops, something went wrong.
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.
It's a bit confusing to have "strict" URL metric extend the "extendable" URL metric - would make more sense to be the other way around. This is also highlighted by how the strict class will still run the filters, but without that serving any purpose.
Taking this a bit further, strictly speaking neither of the two makes sense because a strict URL metric is not an extendable URL metric and vice-versa. So it would make more sense to use e.g. a decorator pattern, or make the two classes separate implementations of a
OD_URL_Metricinterface. Of course that would be a decent amount of refactoring to do - simple, but still some code changes so we could consider that separately.So at this point I mostly think:
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'm not sure I understand. The filters are still used. It's just that any objects have
additionalPropertiesset tofalsein the strict version.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.
In other words, in the strict version, it sets
additionalPropertiestofalsewhich refers to the properties which aren't recognized as part of the schema. The filters are still applying, however, so if a plugin adds new properties to the schema these "additional" properties will be recognized. It's just that additional unrecognized properties will be rejected in the strict verison.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.
Ah right, I had something wrong in my logic here. Some of my other feedback on the class hierarchy still applies, but that's not a big deal in regards to this PR. I do think we should separately consider using a
OD_URL_Metricinterface and use that in all (or at least most of) the type hints so that the class implementations can be independent.