Problem/Motivation
After installing the latest version of Choices, using Composer, on a newly installed Drupal 8 project, and accessing "/admin/config/user-interface/choices" route, I received an error for a missing class.
The website encountered an unexpected error. Please try again later.
Error: Class 'JsonSchema\Validator' not found in Drupal\choices\Form\ConfigForm->__construct() (line 32 of modules/contrib/choices/src/Form/ConfigForm.php).
Drupal\choices\Form\ConfigForm->__construct(Object) (Line: 29)
Drupal\Core\Form\ConfigFormBase::create(Object) (Line: 28)
Drupal\Core\DependencyInjection\ClassResolver->getInstanceFromDefinition('\Drupal\choices\Form\ConfigForm') (Line: 48)
Drupal\Core\Controller\HtmlFormController->getFormObject(Object, '\Drupal\choices\Form\ConfigForm') (Line: 76)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 573)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 151)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
This dependency seems to be used on ConfigForm and ChoicesWidget classes.
There didn't seem to be any other external dependencies used on the project.
Steps to reproduce
- Created a Drupal 8 installation using composer:
composer create-project drupal/recommended-project=^8. - Installed the module using composer:
composer require drupal/choices. - Finished Drupal installation, and enabled the choices module.
- Accessed "/admin/config/user-interface/choices".
Proposed resolution
Add the "require" key with the "justinrainbow/json-schema" package on the project's composer.json.
Remaining tasks
Create a patch or a MR with the proposed resolution.
User interface changes
None.
API changes
None.
Data model changes
None.
Issue fork choices-3322234
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
mpauloThis seems to affect only D8 installations (otherwise CI tests would probably be failing).
Comment #4
mpauloComment #5
marcusvsouza commentedComment #6
anybodyInteresting, indeed it's missing, though Drupal Core would bring it in as dependency already, but makes sense that we should not rely on that.
https://api.drupal.org/api/drupal/vendor!justinrainbow!json-schema!src!J...
https://api.drupal.org/api/drupal/core%21modules%21jsonapi%21src%21Event...
Core JsonAPI uses it! Does anyone know how that defines its dependency on the library? Or is it "magic" by core?
Comment #7
anybodyThanks for the nice implementation, which Drupal 8 version are you using @mpaulo?
I don't think adding the dependency will do any harm, the only thing I'm unsure about is, if we should declare the library version (^5.2) or better use * here to not conflict with core in the future?
Comment #8
anybody@Grevil what do you think? I'm not sure if this really only affects Drupal 8 or perhaps even minimal Drupal 9 installations? Otherwise we could discuss to make 2.1.0 only Drupal ^9 || ^10 compatible instead and keep the 2.0.x for Drupal 8?
Comment #9
mpauloI'm using core:8.9.20.
Looking at the requirement tree for core:^9.5, created with create-project, this package is required by "drupal/core-dev":
Comment #10
anybody@mpaulo thank you very much for this important information!
Indeed we can see the same here: https://packagist.org/packages/drupal/core-dev
Which means that core-dev requires the library. Still this can't be the way how jsonapi requires it, as jsonapi has no dependency on drupal/core-dev and also works with core?
I'd still like to understand how that works...
Anyway, we won't make things worse here by adding this to the composer.json of the module. Should we ever run into a version conflict in the future with drupal-dev, we should change the version to
*in this module.Comment #12
anybodyI'll create a new 2.1.1 release.
Comment #13
grevil commented@Anybody
Yep, this is correct! Although, "JsonSchema" is ONLY used inside the jsonapi's "ResourceResponseValidator" class. And there, the validator will only get initialized if the "JsonSchema\Validator" is present otherwise, the variable is NULL and gets checked in every function if it even exists.
So, to make it simple:
No JsonSchema library required = No schema validation
I actually kinda like this approach, because the schema validation is not critically needed. If the user inputs a faulty json, I guess it's their problem then!
What I don't like about the jsonapi module implementation is, that it doesn't inform the user about the optional json schema validation, so anyone using drupal/core probably doesn't even know about the existing schema validation in "jsonapi".
@Anybody, what do you think about a similar implementation + noticing the user about the missing library on the status page? Or should we just leave it?
Comment #14
anybodyThanks for the feedback @Grevil, indeed that's a nice idea for less overhead, especially if the JSON field is not used at all! (That might be the case quite often).
So what we could do is create a new feature request issue linking this one and suggesting to:
1. Move the composer require depencendy into a composer suggestion (suggests)
2. Add information below the json input fields showing if the library is enabled for validation and otherwise informing the user about the suggestion
3. Only validate if the library is present.
I wouldn't add it to the status report as we don't want to bother users to require a library, which isn't needed (optional).
Still that's a minor feature request and I think the current status is fine for now after adding the dependency.
Please create the feature request, link it here and add a note for the dependency on the module page, then we should set this fixed again :)
Comment #15
grevil commented@Anybody, agree! Going to close this issue then :)
Comment #16
grevil commentedDone, see #3322860: Make json schema validation an optional feature