Skip to content

Commit 7620e94

Browse files
authored
Add assert that raw and readable xcontent field names are different (#63332)
This adds asserts that will catch the case where we accidentally provide the same raw and readable field name in xcontent.
1 parent aa6c88f commit 7620e94

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

libs/x-content/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,8 @@ public XContentBuilder timeField(String name, Object timeValue) throws IOExcepti
725725
* {@link Long} class.
726726
*/
727727
public XContentBuilder timeField(String name, String readableName, long value) throws IOException {
728+
assert name.equals(readableName) == false :
729+
"expected raw and readable field names to differ, but they were both: " + name;
728730
if (humanReadable) {
729731
Function<Object, Object> longTransformer = DATE_TRANSFORMERS.get(Long.class);
730732
if (longTransformer == null) {
@@ -937,6 +939,8 @@ private XContentBuilder value(Iterable<?> values, boolean ensureNoSelfReferences
937939
//////////////////////////////////
938940

939941
public XContentBuilder humanReadableField(String rawFieldName, String readableFieldName, Object value) throws IOException {
942+
assert rawFieldName.equals(readableFieldName) == false :
943+
"expected raw and readable field names to differ, but they were both: " + rawFieldName;
940944
if (humanReadable) {
941945
field(readableFieldName, Objects.toString(value));
942946
}
@@ -956,6 +960,8 @@ public XContentBuilder humanReadableField(String rawFieldName, String readableFi
956960

957961

958962
public XContentBuilder percentageField(String rawFieldName, String readableFieldName, double percentage) throws IOException {
963+
assert rawFieldName.equals(readableFieldName) == false :
964+
"expected raw and readable field names to differ, but they were both: " + rawFieldName;
959965
if (humanReadable) {
960966
field(readableFieldName, String.format(Locale.ROOT, "%1.1f%%", percentage));
961967
}

0 commit comments

Comments
 (0)