Skip to content

Commit c39b6d3

Browse files
authored
Remove Booleans use from XContent and ToXContent (elastic#28768)
* Remove Booleans use from XContent and ToXContent This removes the use of the `common.Boolean` class from two of the XContent classes, so they can be decoupled from the ES code as much as possible. Related to elastic#28754, elastic#28504
1 parent 02493ef commit c39b6d3

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.common.xcontent;
21+
22+
/**
23+
* Helpers for dealing with boolean values. Package-visible only so that only XContent classes use them.
24+
*/
25+
final class Booleans {
26+
/**
27+
* Parse {@code value} with values "true", "false", or null, returning the
28+
* default value if null or the empty string is used. Any other input
29+
* results in an {@link IllegalArgumentException} being thrown.
30+
*/
31+
static boolean parseBoolean(String value, Boolean defaultValue) {
32+
if (value != null && value.length() > 0) {
33+
switch (value) {
34+
case "true":
35+
return true;
36+
case "false":
37+
return false;
38+
default:
39+
throw new IllegalArgumentException("Failed to parse param [" + value + "] as only [true] or [false] are allowed.");
40+
}
41+
} else {
42+
return defaultValue;
43+
}
44+
}
45+
46+
}

server/src/main/java/org/elasticsearch/common/xcontent/ToXContent.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package org.elasticsearch.common.xcontent;
2121

22-
import org.elasticsearch.common.Booleans;
23-
2422
import java.io.IOException;
2523
import java.util.Map;
2624

@@ -132,4 +130,5 @@ public Boolean paramAsBoolean(String key, Boolean defaultValue) {
132130
default boolean isFragment() {
133131
return true;
134132
}
133+
135134
}

server/src/main/java/org/elasticsearch/common/xcontent/XContent.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package org.elasticsearch.common.xcontent;
2121

22-
import org.elasticsearch.common.Booleans;
23-
2422
import java.io.IOException;
2523
import java.io.InputStream;
2624
import java.io.OutputStream;
@@ -51,7 +49,7 @@ public interface XContent {
5149
*/
5250
static boolean isStrictDuplicateDetectionEnabled() {
5351
// Don't allow duplicate keys in JSON content by default but let the user opt out
54-
return Booleans.parseBoolean(System.getProperty("es.xcontent.strict_duplicate_detection", "true"));
52+
return Booleans.parseBoolean(System.getProperty("es.xcontent.strict_duplicate_detection", "true"), true);
5553
}
5654

5755
/**

0 commit comments

Comments
 (0)