Skip to content
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
6 changes: 6 additions & 0 deletions docs/changelog/129005.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 129005
summary: Update AbstractXContentParser to support parsers that don't provide text
characters
area: Infra/Core
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ void ensureNumberConversion(boolean coerce, long result, Class<? extends Number>
public boolean isBooleanValue() throws IOException {
return switch (currentToken()) {
case VALUE_BOOLEAN -> true;
case VALUE_STRING -> Booleans.isBoolean(textCharacters(), textOffset(), textLength());
case VALUE_STRING -> {
if (hasTextCharacters()) {
yield Booleans.isBoolean(textCharacters(), textOffset(), textLength());
} else {
yield Booleans.isBoolean(text());
}
}
default -> false;
};
}
Expand All @@ -94,7 +100,11 @@ public boolean isBooleanValue() throws IOException {
public boolean booleanValue() throws IOException {
Token token = currentToken();
if (token == Token.VALUE_STRING) {
return Booleans.parseBoolean(textCharacters(), textOffset(), textLength(), false /* irrelevant */);
if (hasTextCharacters()) {
return Booleans.parseBoolean(textCharacters(), textOffset(), textLength(), false /* irrelevant */);
} else {
return Booleans.parseBoolean(text(), false /* irrelevant */);
}
}
return doBooleanValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@ public void testCopyCurrentStructure() throws IOException {
}
}

public void testParseBooleanStringValue() throws IOException {
try (
XContentParser parser = new MapXContentParser(
xContentRegistry(),
LoggingDeprecationHandler.INSTANCE,
Map.of("bool_key", "true"),
randomFrom(XContentType.values())
)
) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals(XContentParser.Token.VALUE_STRING, parser.nextToken());
assertTrue(parser.isBooleanValue());
assertTrue(parser.booleanValue());
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
}

try (
XContentParser parser = new MapXContentParser(
xContentRegistry(),
LoggingDeprecationHandler.INSTANCE,
Map.of("bool_key", "false"),
randomFrom(XContentType.values())
)
) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals(XContentParser.Token.VALUE_STRING, parser.nextToken());
assertTrue(parser.isBooleanValue());
assertFalse(parser.booleanValue());
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
}
}

private void compareTokens(CheckedConsumer<XContentBuilder, IOException> consumer) throws IOException {
for (XContentType xContentType : EnumSet.allOf(XContentType.class)) {
logger.info("--> testing with xcontent type: {}", xContentType);
Expand Down