Merge pull request #1448 from Alfresco/fix/MNT-23094_prevent-error-when-sorting-by-empty-field

[MNT-23094] Prevent ArrayIndexOutOfBoundsException when sorting by empty field
This commit is contained in:
tiagosalvado10
2022-07-07 13:22:45 +01:00
committed by GitHub
2 changed files with 36 additions and 13 deletions
@@ -162,29 +162,39 @@ public class AlfrescoCollatableTextFieldType extends StrField
return values[slot];
}
/**
* Finds the term string value for supplied doc
*
* @param doc
* the document id that was hit
* @param term
* a {@link BytesRef} object representing an UTF8 encoded term in the index
*
* @return the term value in string format
*/
private String findBestValue(int doc, BytesRef term)
{
if (term.length == 0 && docsWithField != null && docsWithField.get(doc) == false)
{
return null;
}
// Converts the stored bytes (as UTF8) to string
String withLocale = term.utf8ToString();
// split strin into MLText object
if (withLocale == null)
{
return withLocale;
}
else if (withLocale.startsWith("\u0000"))
if (withLocale != null && withLocale.startsWith("\u0000"))
{
// the array can either be [, locale, term value] or just [, locale] depending whether the term value used
// to perform the sort is empty or not
String[] parts = withLocale.split("\u0000");
return parts[2];
}
else
{
return withLocale;
if (parts != null && parts.length == 3)
{
return parts[2];
}
}
return withLocale;
}
/* (non-Javadoc)
@@ -173,4 +173,17 @@ public class AlfrescoCollatableTextFieldTypeTest
verify(mockCollator).compare("NotNull1", "NotNull2");
assertEquals("Expected result to be obtained from collator.", comparisonResult, result);
}
@Test
public void testMNT23094()
{
// Set up the document to have an encoded value for the field.
when(mockDocTerms.get(DOC)).thenReturn(new BytesRef("\u0000"));
when(mockDocsWithField.get(DOC)).thenReturn(false);
// Call the method under test.
textSortFieldComparator.compareBottom(DOC);
verify(mockCollator).compare(BOTTOM_STRING, "\u0000");
}
}