This is a short write-up of a search-filter bug from work, where a value was parsed in one place and compared in its raw form in another, which meant that a bug in the parser showed up on both sides of a comparison and cancelled itself out until the parser got fixed.
The search form and its default
The page in question is a list with a search form above a grid, and an admin can configure a default value for each search field. For a multi-select field the default is stored as a plain string with the values separated by commas, so the configuration for a status field looks something like this:
const statusField = { name: 'status', multiple: true, defaultValue: 'Active, Pending' };
Before the default is used as a filter it goes through a small parser, which turns the string into an array for multi-select fields (the server filters those with an IN condition):
const parseFilterValue = (field, value) => {
if (field.multiple && typeof value === 'string') {
return value.split(',');
}
return value;
};
You can probably see it already. Whoever typed the default put a space after the comma, as most of us would, and split(',') keeps it, so the second value comes out as ' Pending'. The IN filter then looks for rows with the status ' Pending', there are none, and the grid shows only the active ones. If the user removes the "Active" value from the field, the only thing left is ' Pending' and the list goes empty.
The chip for ' Pending' looked exactly like a chip for 'Pending', because the browser drops leading whitespace when it lays out the label, so the form looked the same with or without the space and only the results were different, which made the bug confusing to report.
Trimming the values
The fix in the parser is one line:
return value.split(',').map(item => item.trim());
The regression test configures 'Active, Pending' as the default and checks that the filter sent to the server is ['Active', 'Pending']. With the trim removed again it fails (it receives ' Pending'), and with the trim in place it passes along with the rest of the suite and the type check. I took that as done, which turned out to be a bit early, and a review found the second half.
The default check
The list has a setting that turns automatic search off, so the grid stays empty until the user runs a search. To decide whether to load anything when the page opens, it asks whether any field differs from its default, and the check went roughly like this:
// '', undefined and [''] (a cleared multi-select, after toString) all mean "nothing set"
const sameOrBothEmpty = (a, b) => (a || '') === (b || '');
const isAtDefault = (field, currentValue) => {
const current = Array.isArray(currentValue) ? currentValue.toString() : currentValue;
return sameOrBothEmpty(current, field.defaultValue);
};
Here currentValue is what the field holds right now, which for an untouched form is the parsed default, and it gets joined back into a string and compared with the raw default from the configuration. This is what that comparison saw before and after the trim:
['Active', ' Pending'].toString(); // 'Active, Pending' - equal to the raw default
['Active', 'Pending'].toString(); // 'Active,Pending' - no longer equal
Before the fix, the untrimmed array joined back into exactly the string it came from, space included, so the check passed only because the same bug was in both of its inputs. Trimming the parser fixed one input and not the other, so an untouched form counted as changed, and on a list with automatic search off the grid loaded data when the page opened, before anyone had searched for anything. The regression test from before did use a spaced default, but it only checked the filter sent to the server and never asked whether the form counted as untouched, so nothing went red.
Parsing the default before comparing
The fix is to stop comparing against the raw configured string and to run the default through the same parser as the field's value:
const asComparable = value => (Array.isArray(value) ? value.toString() : value);
const isAtDefault = (field, currentValue) => {
const defaultValue = parseFilterValue(field, field.defaultValue);
return sameOrBothEmpty(asComparable(currentValue), asComparable(defaultValue));
};
Now 'Active, Pending' turns into ['Active', 'Pending'] on its way into the comparison too, and whatever the parser does with whitespace in the future, there is one place that decides what a default means.
Comparing the arrays directly, without toString(), looks tidier, but a cleared multi-select shows up as [''], which has to count as "nothing set" just like a field with no default at all. After the join it becomes '' and sameOrBothEmpty handles it, while as an array it would stop matching, and clearing a field would turn automatic loading back on, which is an older bug the check was written to prevent.
Two tests cover this now. One opens a form with a spaced default and automatic search off and checks that nothing loads, and it fails without the fix. The other removes one value from the multi-select and checks that the field no longer counts as at its default, because a check that always answered "unchanged" would pass the first test on its own (deselecting the whole field is a different case, and it still counts as unchanged).
Summary
When a value is parsed in one place and compared raw somewhere else, a bug in the parser can sit in both inputs of the comparison and cancel out, so fixing the parser breaks code that doesn't look related to it. Before calling a fix like this done, search for every place that reads the same raw value (the configured default here) and make each of them go through the same parser. The tests for those places need the messy input, a space after the comma in this case, and they need to check what that code does with it, here whether the grid loads, because a test that only checks the parsed value passes with the second half of the bug still in place.
Top comments (0)