Skip to content

Commit

Permalink
#12018 make Fields interpret null values as empty strings
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Orban <lorban@bitronix.be>
  • Loading branch information
lorban committed Jul 10, 2024
1 parent d8f2a81 commit 3de16be
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ public List<String> getValuesOrEmpty(String name)
*/
public void put(String name, String value)
{
String v = value == null ? "" : value;
// Preserve the case for the field name
Field field = new Field(name, value);
Field field = new Field(name, v);
fields.put(name, field);
}

Expand All @@ -218,13 +219,14 @@ public void put(Field field)
*/
public void add(String name, String value)
{
String v = value == null ? "" : value;
fields.compute(name, (k, f) ->
{
if (f == null)
// Preserve the case for the field name
return new Field(name, value);
return new Field(name, v);
else
return new Field(f.getName(), f.getValues(), value);
return new Field(f.getName(), f.getValues(), v);
});
}

Expand All @@ -246,13 +248,30 @@ public void add(String name, String... values)
fields.compute(name, (k, f) ->
{
if (f == null)
return new Field(name, List.of(values));
return new Field(name, toList(values));
else
return new Field(f.getName(), f.getValues(), List.of(values));
return new Field(f.getName(), f.getValues(), toList(values));
});
}
}

static List<String> toList(String... strings)
{
try
{
return List.of(strings);
}
catch (NullPointerException e)
{
List<String> result = new ArrayList<>(strings.length);
for (String s : strings)
{
result.add(s == null ? "" : s);
}
return Collections.unmodifiableList(result);
}
}

/**
* <p>Adds the given field, storing it if none exists for the given name,
* or adding all the values to the existing field with the given name.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,18 @@ public void testIterator()

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(""));
}
}

0 comments on commit 3de16be

Please sign in to comment.