Skip to content

Commit 959933f

Browse files
authored
Update AbstractXContentParser to support parsers that don't provide text characters (#129005) (#129060)
(cherry picked from commit 5ee6dfa)
1 parent 73e4c52 commit 959933f

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
@@ -85,7 +85,13 @@ void ensureNumberConversion(boolean coerce, long result, Class<? extends Number>
8585
public boolean isBooleanValue() throws IOException {
8686
return switch (currentToken()) {
8787
case VALUE_BOOLEAN -> true;
88-
case VALUE_STRING -> Booleans.isBoolean(textCharacters(), textOffset(), textLength());
88+
case VALUE_STRING -> {
89+
if (hasTextCharacters()) {
90+
yield Booleans.isBoolean(textCharacters(), textOffset(), textLength());
91+
} else {
92+
yield Booleans.isBoolean(text());
93+
}
94+
}
8995
default -> false;
9096
};
9197
}
@@ -94,7 +100,11 @@ public boolean isBooleanValue() throws IOException {
94100
public boolean booleanValue() throws IOException {
95101
Token token = currentToken();
96102
if (token == Token.VALUE_STRING) {
97-
return Booleans.parseBoolean(textCharacters(), textOffset(), textLength(), false /* irrelevant */);
103+
if (hasTextCharacters()) {
104+
return Booleans.parseBoolean(textCharacters(), textOffset(), textLength(), false /* irrelevant */);
105+
} else {
106+
return Booleans.parseBoolean(text(), false /* irrelevant */);
107+
}
98108
}
99109
return doBooleanValue();
100110
}

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)