Skip to content

Commit

Permalink
bug :: preserve original behavior for getStringParameter and return n…
Browse files Browse the repository at this point in the history
…ull not an empty string
  • Loading branch information
dev-mlb committed Dec 27, 2024
1 parent 0033170 commit 0a47d5a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/main/java/emissary/core/IBaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ enum MergePolicy {
*/
@Deprecated
default String getStringParameter(final String key) {
return getParameterAsConcatString(key);
return getStringParameter(key, DEFAULT_PARAM_SEPARATOR);
}

/**
Expand All @@ -371,7 +371,25 @@ default String getStringParameter(final String key) {
*/
@Deprecated
default String getStringParameter(final String key, final String sep) {
return getParameterAsConcatString(key, sep);
final List<Object> obj = getParameter(key);
if (obj == null) {
return null;
} else if (obj.isEmpty()) {
return null;
} else if ((obj.size() == 1) && (obj.get(0) instanceof String)) {
return (String) obj.get(0);
} else if ((obj.size() == 1) && (obj.get(0) == null)) {
return null;
} else {
final StringBuilder sb = new StringBuilder();
for (final Object item : obj) {
if (sb.length() > 0) {
sb.append(sep);
}
sb.append(item);
}
return sb.toString();
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/emissary/core/BaseDataObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,7 @@ void testGetParameterAsString() throws IOException {
this.b.appendParameter("A", "THREE");
assertEquals("1", this.b.getParameterAsString("A"));
assertEquals("1;TWO;THREE", this.b.getParameterAsConcatString("A"));
assertEquals("1;TWO;THREE", this.b.getStringParameter("A"));
LogbackTester.SimplifiedLogEvent logEvent = new LogbackTester.SimplifiedLogEvent(Level.WARN,
"Multiple values for parameter, returning the first - parameter:A, number of values:3", null);
logbackTester.checkLogList(Collections.singletonList(logEvent));
Expand All @@ -1360,27 +1361,33 @@ void testGetParameterAsString() throws IOException {
this.b.putParameter("A", 2L);
assertEquals("2", this.b.getParameterAsString("A"));
assertEquals("2", this.b.getParameterAsConcatString("A"));
assertEquals("2", this.b.getStringParameter("A"));

this.b.putParameter("A", "THREE");
assertEquals("THREE", this.b.getParameterAsString("A"));
assertEquals("THREE", this.b.getParameterAsConcatString("A"));
assertEquals("THREE", this.b.getStringParameter("A"));

this.b.putParameter("A", null);
assertNull(this.b.getParameterAsString("A"));
assertNull(this.b.getParameterAsConcatString("A"));
assertNull(this.b.getStringParameter("A"));

this.b.putParameter("A", "");
assertEquals("", this.b.getParameterAsString("A"));
assertEquals("", this.b.getParameterAsConcatString("A"));
assertEquals("", this.b.getStringParameter("A"));

assertNull(this.b.getParameterAsString("DNE"));
assertNull(this.b.getParameterAsConcatString("DNE"));
assertNull(this.b.getStringParameter("DNE"));

this.b.putParameter("A", null);
this.b.appendParameter("A", "FOUR");
this.b.appendParameter("A", " ");
assertEquals("null", this.b.getParameterAsString("A"));
assertEquals("null;FOUR; ", this.b.getParameterAsConcatString("A"));
assertEquals("null;FOUR; ", this.b.getStringParameter("A"));
}

}

0 comments on commit 0a47d5a

Please sign in to comment.