Problem/Motivation
When a field mapped to a non-string Azure type has no values, the document sent to Azure contains boolean false instead of null, and Azure rejects the whole batch. For a Search API date field (mapped to Edm.Int64) the error is:
The request is invalid. Details: Cannot convert the literal 'False' to the expected type 'Edm.Int64'.
The cause is in DocumentsParamBuilder::createFieldValues(): for types that are not Azure-specific data types, the values array is unwrapped with end($field_values), and PHP's end() returns FALSE for an empty array.
This is a regression of #3467433: Empty fields should be nulls. That issue's fix (commit 25c6c68) added an unconditional early return NULL; for empty values. Commit 917f17b from #3546412: Collection fields with empty field values prevent indexing later moved that guard inside the Azure-specific-types branch so empty collections could return [] instead of NULL — but as a side effect every plain type (date, boolean, decimal, duration, string, text) lost the guard. #3548312: Handle empty string field values then restored empty-value handling for string/text only (and integer now has a guard too), so the remaining scalar types still leak FALSE.
Steps to reproduce
- Add a datetime field to an indexed entity type, leave it optional, and index it as type
dateon an index using the Azure AI Search backend (the field becomesEdm.Int64in the Azure index). - Save an entity with the field empty.
- Indexing the item fails; the log shows the error above and the document is not indexed.
Proposed resolution
In the non-Azure-type branch, treat an empty values array as NULL instead of letting end() turn it into FALSE:
else {
if (is_array($field_values)) {
$field_values = empty($field_values) ? NULL : end($field_values) ;
}
}
This is deliberately narrower than restoring the original unconditional guard from #3467433: Empty fields should be nulls, which would re-break the two later fixes: #3546412: Collection fields with empty field values prevent indexing needs empty collections to become [], and #3548312: Handle empty string field values needs empty string/text to become ''. With this change those behaviours are preserved (the string/text cases still convert NULL to ''), while empty date, boolean and decimal values are sent as null, which Azure accepts for any non-key field.
Remaining tasks
Review and commit.
User interface changes
None.
API changes
None.
Data model changes
None. Sites affected by the bug may want to re-index items that previously failed.
Issue fork search_api_aais-3614095
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
jonathanshawComment #4
jonathanshawComment #5
jonathanshawComment #6
rinus van linden commentedThanks Jonathan for noticing and providing a fix.
I've tested and reviewed this and it works!
Comment #8
jonathanshaw:)