Overview
Drupal\experience_builder\Plugin\Adapter\AdapterBase relies on justinrainbow/json-schema:
use JsonSchema\Constraints\Constraint;
use JsonSchema\Validator;
This ships with drupal/core-dev but is not declared as a dependency of XB. If you try to run XB without this package you get an error:
Error: Class "JsonSchema\Validator" not found in Drupal\experience_builder\Plugin\Adapter\AdapterBase->validateConformanceToJsonSchemaType() (line 53 of /var/www/html/web/modules/contrib/experience_builder/src/Plugin/Adapter/AdapterBase.php).
Proposed resolution
Either add justinrainbow/json-schema as a runtime dependency, or make it optional (see JSON:API in core which also uses this as an optional dependency).
User interface changes
Comments
Comment #2
kristen polYeah, just ran into that and @longwave told me to composer require
drupal/core-devwhich fixed that error.Comment #3
wim leersHm.
I was trying to stay closer to how JSON:API does it: better DX if you install this dev-only dependency.
But here, making it optional would make it impossible to validate that the data stored by XB:
\Drupal\experience_builder\Plugin\Validation\Constraint\ValidComponentTreeConstraintValidator::validate()is XB's high-level validation logic, and it calls:\Drupal\Core\Theme\Component\ComponentValidator::validateProps(), which validates the given props values fit into a component's props.IOW: it returns early if
justinrainbow/json-schemais absent.So AFAICT this indeed requires the XB module to explicitly depend on that library.
Thanks for surfacing this! 🙏
Comment #4
larowlanFYI if we make this a production requirement, we should add a hook_requirements warning people with jsonapi module and assertions enabled that their performance will go through the floor because of that module's response validator.
I've been asked to audit sites for performance and found that combo in production 😱
Comment #5
danielvezaBased on #4, if this does get in we should open a follow up to explore other options that aren't going to have performance regressions for unrelated parts of the site.
Comment #6
longwaveInstead of the JSON:API validation magically happening when the package is installed maybe we should move it to a feature flag module (that explicitly depends on the package as well).
Comment #7
wim leersWow, VERY good call, @larowlan! 👏
@danielveza I don't see an alternative solution for this, because it's
\Drupal\Core\Theme\Component\ComponentValidatorthat uses\JsonSchema\Validator… 🤔I think #6 is the right call. Or better yet: have the JSON:API module add a , similar to core's recently added ? 😊
Comment #10
deepakkm commentedComment #11
wim leersYou added it as development dependency, not a runtime dependency. 😅
P.S.: can you mark this issue next time when something is ready? 🙏
Comment #13
danielvezaPushed up an alternate approach based on the feature flag idea to 3469516-feature-flag.
It should hopefully prevent us from needing to add this as a runtime dep and stop the JSON:API preformance regressions outlined in #4.
That being said, I also agree with this in #7
We should open a core issue for this, it feels too easy to shoot yourself in the foot without realizing it and degrade your JSON:API performance. (As this issue has proved). If this approach gets approved and JSON:API gets a "dev mode", we could consider deprecating this feature flag.
Comment #14
deepakkm commentedComment #15
deepakkm commentedComment #16
wim leers@danielveza I'm very sorry 😭… but the feature flag doesn't make sense here, only for JSON:API. For JSON:API, it's a nice-to-have, just to know about compliance of JSON:API responses with the JSON:API spec.
For XB, the JSON schema validation is critical: without it,
\Drupal\experience_builder\Plugin\Validation\Constraint\ValidComponentTreeConstraintValidator::validate()becomes pretty much pointless.That's AFAICT also what @longwave proposed in #6: that it's Drupal core's JSON:API functionality that should be doing that validation behind a feature flag.
Am I missing something here? Can you confirm, @longwave? 🙏
Comment #17
longwaveYeah I meant that we should change core so JSON:API validation is explicitly enabled with a feature flag. I wonder even if it should just be a contrib module and the feature removed from core directly, but that is a product manager decision. Will open a core issue to discuss.
Comment #18
longwaveOpened #3472008: Avoid JSON:API schema validation by default when assert is enabled to discuss the core change.
For the time being, I think we should land MR!247 in XB, marking RTBC for that.
Comment #20
wim leersNW for https://git.drupalcode.org/project/experience_builder/-/merge_requests/2...
After that's fixed, will happily merge :)
Comment #21
wim leersComment #23
omkar-pd commentedMade changes.
Comment #24
wim leersComment #25
wim leersComment #27
wim leers2 out of 3 needed MR approvals, but @longwave approved it here at #18, so bypassing the GitLab
CODEOWNERScheck to be pragmatic… 👍Comment #29
wim leersComment #30
danielvezaMy POC didn't take long so it's ok that it didn't make it in.
The reason I thought a feature flag here could be valuable is that we have no guarantee that the core issue is going to get in before XB is considered production ready or early adopters start using it. I'd like to avoid future devs having pain when trying to track down what is most likely a quite annoying performance regression to figure out.
Probably the softer approach for XB rather than the feature flag that removes it is a hook_requirements implementation that runs if JOSN:API is also enabled. I'll open a ticket.
Comment #31
wim leers@danielveza That's a relief! 😮💨
That's totally fair.
I was going to suggest docs but … this is far better! 👏🤩 Thank you! 🙏