Browse Source

ObjectUtils uses HexFormat to format byte[]

Also remove equivalent, applied temporarily in FieldError in 6.2.x.

Closes gh-35675
pull/35696/head
rstoyanchev 2 months ago
parent
commit
a982c9b481
  1. 14
      spring-context/src/main/java/org/springframework/validation/FieldError.java
  2. 13
      spring-core/src/main/java/org/springframework/util/ObjectUtils.java
  3. 4
      spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java

14
spring-context/src/main/java/org/springframework/validation/FieldError.java

@ -16,8 +16,6 @@ @@ -16,8 +16,6 @@
package org.springframework.validation;
import java.util.HexFormat;
import org.jspecify.annotations.Nullable;
import org.springframework.util.Assert;
@ -127,18 +125,8 @@ public class FieldError extends ObjectError { @@ -127,18 +125,8 @@ public class FieldError extends ObjectError {
// We would preferably use ObjectUtils.nullSafeConciseToString(rejectedValue) here but
// keep including the full nullSafeToString representation for backwards compatibility.
return "Field error in object '" + getObjectName() + "' on field '" + this.field +
"': rejected value [" + formatRejectedValue() + "]; " +
"': rejected value [" + ObjectUtils.nullSafeToString(this.rejectedValue) + "]; " +
resolvableToString();
}
private String formatRejectedValue() {
// Special handling of byte[], to be moved into ObjectUtils in 7.0
if (this.rejectedValue instanceof byte[] bytes && bytes.length != 0) {
return "{" + HexFormat.of().formatHex(bytes) + "}";
}
return ObjectUtils.nullSafeToString(this.rejectedValue);
}
}

13
spring-core/src/main/java/org/springframework/util/ObjectUtils.java

@ -21,6 +21,7 @@ import java.nio.charset.Charset; @@ -21,6 +21,7 @@ import java.nio.charset.Charset;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Collection;
import java.util.HexFormat;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@ -695,9 +696,9 @@ public abstract class ObjectUtils { @@ -695,9 +696,9 @@ public abstract class ObjectUtils {
/**
* Return a String representation of the contents of the specified array.
* <p>The String representation consists of a list of the array's elements,
* enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
* by the characters {@code ", "} (a comma followed by a space).
* <p>As of 7.0, the String representation is a hex-encoded string enclosed
* in curly braces ({@code "{}"}). The String consists of 2 hexadecimal
* chars per element, and without a delimiter between adjacent elements.
* Returns a {@code "null"} String if {@code array} is {@code null}.
* @param array the array to build a String representation for
* @return a String representation of {@code array}
@ -709,11 +710,7 @@ public abstract class ObjectUtils { @@ -709,11 +710,7 @@ public abstract class ObjectUtils {
if (array.length == 0) {
return EMPTY_ARRAY;
}
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
for (byte b : array) {
stringJoiner.add(String.valueOf(b));
}
return stringJoiner.toString();
return ARRAY_START + HexFormat.of().formatHex(array) + ARRAY_END;
}
/**

4
spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java

@ -630,8 +630,8 @@ class ObjectUtilsTests { @@ -630,8 +630,8 @@ class ObjectUtilsTests {
@Test
void nullSafeToStringWithByteArray() {
byte[] array = {5, 8};
assertThat(ObjectUtils.nullSafeToString(array)).isEqualTo("{5, 8}");
byte[] array = {5, 38};
assertThat(ObjectUtils.nullSafeToString(array)).isEqualTo("{0526}");
}
@Test

Loading…
Cancel
Save