Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Fields interpret null values as empty strings #12024

Merged
merged 4 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
gregw marked this conversation as resolved.
Show resolved Hide resolved
// 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)
lorban marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
return List.of(strings);
}
catch (NullPointerException e)
lorban marked this conversation as resolved.
Show resolved Hide resolved
{
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(""));
}
}
Loading