Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove lenient boolean handling #34467

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,26 +204,6 @@ enum NumberType {

boolean booleanValue() throws IOException;

// TODO #22298: Remove this method and replace all call sites with #isBooleanValue()
/**
* returns true if the current value is boolean in nature.
* values that are considered booleans:
* - boolean value (true/false)
* - numeric integers (=0 is considered as false, !=0 is true)
* - one of the following strings: "true","false","on","off","yes","no","1","0"
*
* @deprecated Just present for providing backwards compatibility. Use {@link #isBooleanValue()} instead.
*/
@Deprecated
boolean isBooleanValueLenient() throws IOException;

// TODO #22298: Remove this method and replace all call sites with #booleanValue()
/**
* @deprecated Just present for providing backwards compatibility. Use {@link #booleanValue()} instead.
*/
@Deprecated
boolean booleanValueLenient() throws IOException;

/**
* Reads a plain binary value that was written via one of the following methods:
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,34 +98,6 @@ public boolean booleanValue() throws IOException {
return doBooleanValue();
}

@Override
@Deprecated
public boolean isBooleanValueLenient() throws IOException {
switch (currentToken()) {
case VALUE_BOOLEAN:
return true;
case VALUE_NUMBER:
NumberType numberType = numberType();
return numberType == NumberType.LONG || numberType == NumberType.INT;
case VALUE_STRING:
return Booleans.isBooleanLenient(textCharacters(), textOffset(), textLength());
default:
return false;
}
}

@Override
@Deprecated
public boolean booleanValueLenient() throws IOException {
Token token = currentToken();
if (token == Token.VALUE_NUMBER) {
return intValue() != 0;
} else if (token == Token.VALUE_STRING) {
return Booleans.parseBooleanLenient(textCharacters(), textOffset(), textLength(), false /* irrelevant */);
}
return doBooleanValue();
}

protected abstract boolean doBooleanValue() throws IOException;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,35 +189,6 @@ private Map<String, String> readMapStrings(String source) throws IOException {
}
}

@SuppressWarnings("deprecation") // #isBooleanValueLenient() and #booleanValueLenient() are the test subjects
public void testReadLenientBooleans() throws IOException {
// allow String, boolean and int representations of lenient booleans
String falsy = randomFrom("\"off\"", "\"no\"", "\"0\"", "0", "\"false\"", "false");
String truthy = randomFrom("\"on\"", "\"yes\"", "\"1\"", "1", "\"true\"", "true");

try (XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"foo\": " + falsy + ", \"bar\": " + truthy + "}")) {
XContentParser.Token token = parser.nextToken();
assertThat(token, equalTo(XContentParser.Token.START_OBJECT));
token = parser.nextToken();
assertThat(token, equalTo(XContentParser.Token.FIELD_NAME));
assertThat(parser.currentName(), equalTo("foo"));
token = parser.nextToken();
assertThat(token, isIn(
Arrays.asList(XContentParser.Token.VALUE_STRING, XContentParser.Token.VALUE_NUMBER, XContentParser.Token.VALUE_BOOLEAN)));
assertTrue(parser.isBooleanValueLenient());
assertFalse(parser.booleanValueLenient());

token = parser.nextToken();
assertThat(token, equalTo(XContentParser.Token.FIELD_NAME));
assertThat(parser.currentName(), equalTo("bar"));
token = parser.nextToken();
assertThat(token, isIn(
Arrays.asList(XContentParser.Token.VALUE_STRING, XContentParser.Token.VALUE_NUMBER, XContentParser.Token.VALUE_BOOLEAN)));
assertTrue(parser.isBooleanValueLenient());
assertTrue(parser.booleanValueLenient());
}
}

public void testReadBooleansFailsForLenientBooleans() throws IOException {
String falsy = randomFrom("\"off\"", "\"no\"", "\"0\"", "0");
String truthy = randomFrom("\"on\"", "\"yes\"", "\"1\"", "1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,7 @@ private static void parseDocuments(XContentParser parser, List<Item> items, @Nul
} else if (VERSION_TYPE.match(currentFieldName, parser.getDeprecationHandler())) {
versionType = VersionType.fromString(parser.text());
} else if (SOURCE.match(currentFieldName, parser.getDeprecationHandler())) {
// check lenient to avoid interpreting the value as string but parse strict in order to provoke an error early on.
if (parser.isBooleanValueLenient()) {
if (parser.isBooleanValue()) {
fetchSourceContext = new FetchSourceContext(parser.booleanValue(), fetchSourceContext.includes(),
fetchSourceContext.excludes());
} else if (token == Token.VALUE_STRING) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,6 @@ public void testUnexpectedField() throws IOException {
"unexpected token [START_OBJECT], expected [FIELD_NAME] or [START_ARRAY]"));
}

public void testAddWithInvalidSourceValueIsRejected() throws Exception {
String sourceValue = randomFrom("on", "off", "0", "1");
XContentParser parser = createParser(XContentFactory.jsonBuilder()
.startObject()
.startArray("docs")
.startObject()
.field("_source", sourceValue)
.endObject()
.endArray()
.endObject()
);

MultiGetRequest multiGetRequest = new MultiGetRequest();
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> multiGetRequest.add
(randomAlphaOfLength(5), randomAlphaOfLength(3), null, FetchSourceContext.FETCH_SOURCE, null, parser, true));
assertEquals("Failed to parse value [" + sourceValue + "] as only [true] or [false] are allowed.", ex.getMessage());
}

public void testAddWithValidSourceValueIsAccepted() throws Exception {
XContentParser parser = createParser(XContentFactory.jsonBuilder()
.startObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,6 @@ public boolean booleanValue() throws IOException {
return parser.booleanValue();
}

@Override
@SuppressWarnings("deprecated")
public boolean isBooleanValueLenient() throws IOException {
return parser.isBooleanValueLenient();
}

@Override
@SuppressWarnings("deprecated")
public boolean booleanValueLenient() throws IOException {
return parser.booleanValueLenient();
}

@Override
public byte[] binaryValue() throws IOException {
return parser.binaryValue();
Expand Down