[7.x] Add assert that raw and readable xcontent field names are different (#63332) (#63343)

This adds asserts that will catch the case where we accidentally provide the same raw and readable
field name in xcontent.
This commit is contained in:
Lee Hinman 2020-10-06 11:32:41 -06:00 committed by GitHub
parent 4307b1d607
commit dd99125193
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 0 deletions

View File

@ -725,6 +725,8 @@ public final class XContentBuilder implements Closeable, Flushable {
* {@link Long} class.
*/
public XContentBuilder timeField(String name, String readableName, long value) throws IOException {
assert name.equals(readableName) == false :
"expected raw and readable field names to differ, but they were both: " + name;
if (humanReadable) {
Function<Object, Object> longTransformer = DATE_TRANSFORMERS.get(Long.class);
if (longTransformer == null) {
@ -937,6 +939,8 @@ public final class XContentBuilder implements Closeable, Flushable {
//////////////////////////////////
public XContentBuilder humanReadableField(String rawFieldName, String readableFieldName, Object value) throws IOException {
assert rawFieldName.equals(readableFieldName) == false :
"expected raw and readable field names to differ, but they were both: " + rawFieldName;
if (humanReadable) {
field(readableFieldName, Objects.toString(value));
}
@ -956,6 +960,8 @@ public final class XContentBuilder implements Closeable, Flushable {
public XContentBuilder percentageField(String rawFieldName, String readableFieldName, double percentage) throws IOException {
assert rawFieldName.equals(readableFieldName) == false :
"expected raw and readable field names to differ, but they were both: " + rawFieldName;
if (humanReadable) {
field(readableFieldName, String.format(Locale.ROOT, "%1.1f%%", percentage));
}