Problem/Motivation
While working on #2976147: Create a sales report supporting daily, weekly, monthly, and yearly breakdowns., my tests failed due to configuration schema validation.
Schema errors for views.view.sales_report with the following errors: views.view.sales_report:display.default.display_options.fields.report_id.set_precision missing schema
The report_id
is the entity identifier base field. The View has aggregation enabled, and the report_id
is set to COUNT.
In Views, the handler is swapped to numeric as an override when the display is built. This is causing a SchemaIncompleteException exception to be thrown.
This following code is what changes the handler and causes the schema error
\Drupal\views\Plugin\views\display\DisplayPluginBase::getHandlers
// If aggregation is on, the group type might override the actual
// handler that is in use. This piece of code checks that and,
// if necessary, sets the override handler.
$override = NULL;
if ($this->useGroupBy() && !empty($info['group_type'])) {
if (empty($this->view->query)) {
$this->view->initQuery();
}
$aggregate = $this->view->query->getAggregationInfo();
if (!empty($aggregate[$info['group_type']]['handler'][$type])) {
$override = $aggregate[$info['group_type']]['handler'][$type];
}
}
Steps to reproduce
Create test view, showing content of any type. Enable aggregation. Add field 'Content: ID' and set COUNT type of aggregation on added field.
Proposed resolution
Update logic of manipulating on 'plugin_id' option and change corresponding code, so plugin_id reflects actual state of the field.
Remaining tasks
Review patch/MR, give feedback, rework, accept.
Comment | File | Size | Author |
---|---|---|---|
#58 | 2976616-58.patch | 178.6 KB | ilya.no |
#48 | reroll_diff_42-48.txt | 17.03 KB | Akram Khan |
#48 | 2976616-48.patch | 184.29 KB | Akram Khan |
Issue fork drupal-2976616
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 #2
mglamanComment #3
LendudeDoes this only happen in a test or does it also happen when you try to save the View (which is when the config gets validated in a non-test scenario)?
Comment #4
mglamanIt only happens when running a test which installs the configuration. There is no UI issues, but I think the bigger issue is that this may highlight a gap in test coverage. That's the only time the schema checker runs. The config is valid technically (
0
could pass asfalse
) but not against the defined schema.Comment #5
tim.plunkettUnless \Drupal\Core\Config\Development\ConfigSchemaChecker is added as an event subscriber, config schema exceptions will not be raised at runtime.
This is is done for Kernel and Functional tests, which is why schema errors are commonly not found until such a test is written.
Comment #8
pmate CreditAttribution: pmate commentedI can confirm this bug on Drupal 8.6.10 when running functional tests.
I have a view with aggregation enabled and use MIN and MAX on some fields.
--
Edit: It looks like when using aggregation, the views are being saved with data of multiple schema at the same time when (I assume) they only support a single one.
In my case, fields had a `date` plugin_id and when I enabled MIN or MAX aggregation on them they were "converted" to `numeric`. This caused the options to be saved for both plugins under the same "plugin_id" which is breaking the schema.
I have no knowledge here, but shouldn't these type options be saved separately in an array? Both types are valid and all the options saved seems also required but the way it saves is causing the bug.
Comment #11
ilya.no CreditAttribution: ilya.no as a volunteer and at Skilld for Skilld commentedAttaching patch with simple solution of adding missing fields into
views_field
type, which is common for other views fields. It's not clean, but at least has some logic. Another solution is to change whole override functionality, we even have corresponding comment in \Drupal\views\Plugin\ViewsHandlerManager::getHandler(), saying// @todo This is crazy. Find a way to remove the override functionality.
But I'm not sure, whether this type of change is in scope of this issue.
Comment #12
ilya.no CreditAttribution: ilya.no as a volunteer and at Skilld for Skilld commentedComment #13
vacho CreditAttribution: vacho at Skilld commentedHi here!
I reviewed the patch and this works well!
However, I have an observation.
The module commerce_reports (https://www.drupal.org/project/commerce_reports) has the view views.view.sales_report.yml that has the fields set_precision, precision, etc.
So I think that the scheme configuration solution that has patch #11 is needed to implement into the commerce_reports module more than views core module.
Comment #14
ilya.no CreditAttribution: ilya.no as a volunteer and at Skilld for Skilld commentedThanks for the review. commerce_reports module is only example. Actually you can reproduce this issue by creating test view, showing content of any type, adding field Content: ID, enabling aggregation and setting COUNT type on added field. Then you will see error in configuration, saying missing schema.
Comment #15
sorlov CreditAttribution: sorlov at Skilld commentedI think, adding aggregation fields to schema of
views_field
type from patch #11 makes sense. This way, schema fix will be applied to any field with aggregation.Comment #16
mparker17I can confirm the patch in #11 works for me, and passed my code review!
Boldly marking as RTBC.
Comment #17
quietone CreditAttribution: quietone as a volunteer commentedLets put the solution into the IS and skimming the issue there are steps to reproduce in #14 which should be in the IS as well.
The remaining tasks indicates that a test is to be written yet there are no tests here, adding tag.
Comment #20
alexpottHere's a test. Thanks for the patch @ilya.no!
Comment #24
solotandem CreditAttribution: solotandem as a volunteer commentedThe comments above note the saved config is not in sync with the schema definitions. The straightforward approach seems to be to keep them in sync. This is easily done by changing the plugin_id when the config is saved.
The $all parameter to unpackOptions should not be TRUE by default. This enables the contamination of config with items from two schema definitions. Setting it to FALSE allows views to prune items no longer applicable to the handler.
Comment #26
solotandem CreditAttribution: solotandem as a volunteer commentedThe test errors were nearly all due to data that is out of sync with the code change. In other words, default module views and test module views need to be updated to reflect the config changes. This includes views in a tar.gz file for testing.
It might make sense to split the patch over new issues:
1) add plugin_id to defineOptions() and schema.yml file
- this requires moving plugin_id in existing default views (regular and test modules)
- this is not directly the topic of this issue but collateral damage of fixing it
2) code change to update plugin_id when grouping applies
Comment #27
solotandem CreditAttribution: solotandem as a volunteer commentedTry again.
Comment #29
solotandem CreditAttribution: solotandem as a volunteer commentedAdd more undefined keys used in code and in tests; not in schema.yml files.
Comment #31
solotandem CreditAttribution: solotandem as a volunteer commentedTo reiterate, since patch in #24, the test errors are all due to 1) missing schema, 2) code and schema not being in sync, and 3) errors in default views.
Latest patch:
- adds more undefined keys used in code and in tests; not in schema.yml files
- syncs code with schema
- fixes default views
Comment #33
solotandem CreditAttribution: solotandem as a volunteer commentedMore of the same.
Comment #34
solotandem CreditAttribution: solotandem as a volunteer commentedComment #35
andypostNW for issue summary update
Comment #36
solotandem CreditAttribution: solotandem as a volunteer commented@andypost Have you read the related issue 3258664? It has the issue summary update. This issue is RTBC in reality.
Comment #37
andypost@solotandem yes, but "no idea" as proposed will not work for commiters
Comment #40
vacho CreditAttribution: vacho at Skilld commentedThis is only a rebase for now.
Comment #41
andypost@vacho your patch contains lots of unrelated changes, please clean-up it and reupload
Comment #42
vacho CreditAttribution: vacho at Skilld commentedYea @andypost, thanks to this catch.
Now yes, a good rebase.
Comment #43
vacho CreditAttribution: vacho at Skilld commentedAlso looks right
Comment #44
andypostissue summary still needs update
Comment #45
Anchal_gupta CreditAttribution: Anchal_gupta at Material for Drupal India Association commentedi have fixed cs error.Please review it
Comment #47
andypost2 tests still broken and not clear how it will work for existing sites/views
Comment #48
Akram KhanUpdating patch for 10.1.x and Fixing Custom comand failed #47
Comment #49
vacho CreditAttribution: vacho at Skilld commentedAfter review, the last patch needs these works:
- Removes all code comments. Also, explain what the task remains to fix on these.
- Is not clear to me: why changing the order of the plugin_id: field to the first position fixes something.
As I see this looks like the path to fix this issue:
- After saving the view (with aggregation and...) at a time to export the views configuration, this should reflect the plugin_id that views use internally, also the schema should cover this scenario when some item turns as aggregation.
Also one note: Currently, with the last code of Drupal10 I don't get the issue creating a view as #14 explains, how to reproduce. Can some check if still happens and comment on the exact steps to reproduce it?
Comment #50
vacho CreditAttribution: vacho at Skilld commentedRelated to that I can't reproduce the issue anymore.
I follow these steps:
- Create a view for filter fields for Basic page CT
- Let only a field for Content ID
- Add Aggregation - Count for this field.
This is the git diff for view configuration and these don't throw a config inspection issue.
Not that any field changes of the type.
this is the view configuration by GUI:
maybe the issue doesn't happen anymore?
Comment #51
vacho CreditAttribution: vacho at Skilld commentedThis is only a palliative solution. I found this issue in a doesn't upgraded to D10 project.
So this hack propose: If the view field change to numeric, and the schema doesn't chain, the view field can contain these keys(for numeric) also and this can avoid the issue.
These case for schemes with fields that not always be used, was used before on Drupal. So it is no a crazy solution.
Comment #52
andypostGood point with schema but it means we have no test coverage for this handler, IS also needs update
Comment #53
ilya.no CreditAttribution: ilya.no as a volunteer and at Skilld for Skilld commentedI've tested on D10 site and haven't reproduced issue neither. I think, that patch from comment #51 is enough for sites, using D9, while issue itself may be closed.
Comment #54
smustgrave CreditAttribution: smustgrave at Mobomo commentedStill needs tests and an issue summary.
If not an issue for 10 then that should be documented in the code and version of the ticket changed.
Comment #56
ilya.no CreditAttribution: ilya.no as a volunteer and at Skilld for Skilld commentedAttaching patch with tests, which are taken from #20 comment. I've also updated issue summary with 2 possible solutions.
Comment #57
needs-review-queue-bot CreditAttribution: needs-review-queue-bot as a volunteer commentedThe Needs Review Queue Bot tested this issue.
While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)
Comment #58
ilya.no CreditAttribution: ilya.no as a volunteer and at Skilld for Skilld commentedWhile checking failed tests for my patch, I've discovered following points:
1) My solution doesn't work for latest versions of Drupal, because it adds more mess into config schema definition and it affects views more heavy, than I've expected, so I've focused on another solution of that issue.
2) I've put a bit wrong description on `Steps to reproduce` section. If we create field and then enable aggregation, than issue is not reproduced. But if we create view, enable aggregation, add field and set its aggregation to COUNT, than issue can be reproduced with latest version of Drupal. So we first need to enable aggregation and only then add field and set aggregation for it.
I've fixed `Steps to reproduce` section and also removed my solution from `Proposed resolution` section. I'm adding new patch, which is rebase of the patch from 33 comment. I couldn't generate interdiff, but basically it's rebase for D11 with couple of fixes. I've also added hook_post_update. I haven't run all tests, but the ones, which were failing are now good, at least locally.
Comment #60
ilya.no CreditAttribution: ilya.no as a volunteer and at Skilld for Skilld commentedI've created MR with changes from latest patch for further developing.
Comment #61
andypostThank you 🙏 Is there a way to prevent reordering in yml files?
Comment #62
ilya.no CreditAttribution: ilya.no as a volunteer and at Skilld for Skilld commentedAs I understand no, because currently different mappings in schema contain plugin_id in their own place. In this issue we remove plugin_id from all these mappings and add it to the `views_handler` type at the first place.
Basically we don't need to reorder it for existing views, but we need to update default views from core modules, because otherwise tests fail. It happens, because test compares default config with actual and fails, as they are not the same.
I've pushed phpcs fix into MR, not sure, that I need to update patch for it.
Comment #63
smustgrave CreditAttribution: smustgrave at Mobomo commentedAppears to have test failures. Also believe we will need a simple test for the post_update hook.