Make `Fields` interpret null values as empty strings (#12024)
#12018 make Fields interpret null values as empty strings Signed-off-by: Ludovic Orban <lorban@bitronix.be>
This commit is contained in:
parent
7be30e0b15
commit
1a38b6884e
|
@ -191,7 +191,7 @@ public class Fields implements Iterable<Fields.Field>
|
|||
public void put(String name, String value)
|
||||
{
|
||||
// Preserve the case for the field name
|
||||
Field field = new Field(name, value);
|
||||
Field field = new Field(name, StringUtil.nonNull(value));
|
||||
fields.put(name, field);
|
||||
}
|
||||
|
||||
|
@ -222,9 +222,9 @@ public class Fields implements Iterable<Fields.Field>
|
|||
{
|
||||
if (f == null)
|
||||
// Preserve the case for the field name
|
||||
return new Field(name, value);
|
||||
return new Field(name, StringUtil.nonNull(value));
|
||||
else
|
||||
return new Field(f.getName(), f.getValues(), value);
|
||||
return new Field(f.getName(), f.getValues(), StringUtil.nonNull(value));
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -246,9 +246,9 @@ public class Fields implements Iterable<Fields.Field>
|
|||
fields.compute(name, (k, f) ->
|
||||
{
|
||||
if (f == null)
|
||||
return new Field(name, List.of(values));
|
||||
return new Field(name, StringUtil.toListNonNull(values));
|
||||
else
|
||||
return new Field(f.getName(), f.getValues(), List.of(values));
|
||||
return new Field(f.getName(), f.getValues(), StringUtil.toListNonNull(values));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -540,6 +540,23 @@ public class StringUtil
|
|||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an array of strings to a list of non-null strings.
|
||||
*
|
||||
* @param strings the array
|
||||
* @return The list of non-null strings.
|
||||
* @see #nonNull(String)
|
||||
*/
|
||||
public static List<String> toListNonNull(String... strings)
|
||||
{
|
||||
List<String> result = new ArrayList<>(strings.length);
|
||||
for (String s : strings)
|
||||
{
|
||||
result.add(nonNull(s));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static boolean equals(String s, char[] buf, int offset, int length)
|
||||
{
|
||||
if (s.length() != length)
|
||||
|
|
|
@ -82,4 +82,18 @@ public class FieldsTest
|
|||
|
||||
assertThat(set, containsInAnyOrder("x", "y", "z"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullValues()
|
||||
{
|
||||
Fields fields = new Fields();
|
||||
fields.add("x", (String)null);
|
||||
fields.add("y", "1", null, "2");
|
||||
fields.put("z", null);
|
||||
|
||||
assertThat(fields.getSize(), equalTo(3));
|
||||
assertThat(fields.getValues("x"), contains(""));
|
||||
assertThat(fields.getValues("y"), contains("1", "", "2"));
|
||||
assertThat(fields.getValues("z"), contains(""));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue