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

  1. Add a datetime field to an indexed entity type, leave it optional, and index it as type date on an index using the Azure AI Search backend (the field becomes Edm.Int64 in the Azure index).
  2. Save an entity with the field empty.
  3. 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.

Command icon 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

jonathanshaw created an issue. See original summary.

jonathanshaw’s picture

Issue summary: View changes

jonathanshaw’s picture

Issue summary: View changes
jonathanshaw’s picture

Status: Active » Needs review
rinus van linden’s picture

Status: Needs review » Reviewed & tested by the community

Thanks Jonathan for noticing and providing a fix.

I've tested and reviewed this and it works!

jonathanshaw’s picture

Status: Reviewed & tested by the community » Fixed

:)

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.