Skip to content

Commit b66fb26

Browse files
authored
Update AbstractXContentParser to support parsers that don't provide text characters (elastic#129005) (elastic#129058)
(cherry picked from commit 5ee6dfa)
1 parent 5184731 commit b66fb26

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

docs/changelog/129005.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 129005
2+
summary: Update AbstractXContentParser to support parsers that don't provide text
3+
characters
4+
area: Infra/Core
5+
type: bug
6+
issues: []

libs/x-content/src/main/java/org/elasticsearch/xcontent/support/AbstractXContentParser.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ void ensureNumberConversion(boolean coerce, long result, Class<? extends Number>
8787
public boolean isBooleanValue() throws IOException {
8888
return switch (currentToken()) {
8989
case VALUE_BOOLEAN -> true;
90-
case VALUE_STRING -> Booleans.isBoolean(textCharacters(), textOffset(), textLength());
90+
case VALUE_STRING -> {
91+
if (hasTextCharacters()) {
92+
yield Booleans.isBoolean(textCharacters(), textOffset(), textLength());
93+
} else {
94+
yield Booleans.isBoolean(text());
95+
}
96+
}
9197
default -> false;
9298
};
9399
}
@@ -96,7 +102,11 @@ public boolean isBooleanValue() throws IOException {
96102
public boolean booleanValue() throws IOException {
97103
Token token = currentToken();
98104
if (token == Token.VALUE_STRING) {
99-
return Booleans.parseBoolean(textCharacters(), textOffset(), textLength(), false /* irrelevant */);
105+
if (hasTextCharacters()) {
106+
return Booleans.parseBoolean(textCharacters(), textOffset(), textLength(), false /* irrelevant */);
107+
} else {
108+
return Booleans.parseBoolean(text(), false /* irrelevant */);
109+
}
100110
}
101111
return doBooleanValue();
102112
}

libs/x-content/src/test/java/org/elasticsearch/xcontent/MapXContentParserTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,40 @@ public void testCopyCurrentStructure() throws IOException {
9797
}
9898
}
9999

100+
public void testParseBooleanStringValue() throws IOException {
101+
try (
102+
XContentParser parser = new MapXContentParser(
103+
xContentRegistry(),
104+
LoggingDeprecationHandler.INSTANCE,
105+
Map.of("bool_key", "true"),
106+
randomFrom(XContentType.values())
107+
)
108+
) {
109+
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
110+
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
111+
assertEquals(XContentParser.Token.VALUE_STRING, parser.nextToken());
112+
assertTrue(parser.isBooleanValue());
113+
assertTrue(parser.booleanValue());
114+
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
115+
}
116+
117+
try (
118+
XContentParser parser = new MapXContentParser(
119+
xContentRegistry(),
120+
LoggingDeprecationHandler.INSTANCE,
121+
Map.of("bool_key", "false"),
122+
randomFrom(XContentType.values())
123+
)
124+
) {
125+
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
126+
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
127+
assertEquals(XContentParser.Token.VALUE_STRING, parser.nextToken());
128+
assertTrue(parser.isBooleanValue());
129+
assertFalse(parser.booleanValue());
130+
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
131+
}
132+
}
133+
100134
private void compareTokens(CheckedConsumer<XContentBuilder, IOException> consumer) throws IOException {
101135
for (XContentType xContentType : EnumSet.allOf(XContentType.class)) {
102136
logger.info("--> testing with xcontent type: {}", xContentType);

0 commit comments

Comments
 (0)