Skip to content

Commit 84d8cc2

Browse files
authored
fix: toCommaDelimitedString returns "null,null" when inputs are null (#15338)
* fix: return 'one' if others is null in toCommaDelimitedString * fix:
1 parent f9968b0 commit 84d8cc2

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,14 +1242,20 @@ public static byte decodeHexByte(CharSequence s, int pos) {
12421242
}
12431243

12441244
/**
1245-
* Create the common-delimited {@link String} by one or more {@link String} members
1245+
* Creates a comma-delimited string from one or more string values.
12461246
*
1247-
* @param one one {@link String}
1248-
* @param others others {@link String}
1249-
* @return <code>null</code> if <code>one</code> or <code>others</code> is <code>null</code>
1247+
* @param one the first string value
1248+
* @param others additional string values
1249+
* @return the combined string, or null if the first value is null
12501250
* @since 2.7.8
12511251
*/
12521252
public static String toCommaDelimitedString(String one, String... others) {
1253+
if (one == null) {
1254+
return null;
1255+
}
1256+
if (others == null) {
1257+
return one;
1258+
}
12531259
String another = arrayToDelimitedString(others, COMMA_SEPARATOR);
12541260
return isEmpty(another) ? one : one + COMMA_SEPARATOR + another;
12551261
}

dubbo-common/src/test/java/org/apache/dubbo/common/utils/StringUtilsTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ private void assertEqualsWithoutSpaces(String expect, String actual) {
474474

475475
/**
476476
* Test {@link StringUtils#toCommaDelimitedString(String, String...)}
477+
*
477478
* @since 2.7.8
478479
*/
479480
@Test
@@ -484,6 +485,9 @@ void testToCommaDelimitedString() {
484485
value = toCommaDelimitedString(null, null);
485486
assertNull(value);
486487

488+
value = toCommaDelimitedString("one", null);
489+
assertEquals("one", value);
490+
487491
value = toCommaDelimitedString("");
488492
assertEquals("", value);
489493

0 commit comments

Comments
 (0)