Skip to content

Commit 233e90d

Browse files
authored
Better support for switching parsers. Relates to ES-13516 (#138722)
1 parent a57f140 commit 233e90d

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,12 @@ <T> Map<String, T> map(Supplier<Map<String, T>> mapFactory, CheckedFunction<XCon
225225
* The callback to notify when parsing encounters a deprecated field.
226226
*/
227227
DeprecationHandler getDeprecationHandler();
228+
229+
/**
230+
* Switch to a different underlying parser.
231+
* Typically, that's a noop but some filter parsers might want to wrap the underlying parser again.
232+
*/
233+
default XContentParser switchParser(XContentParser parser) throws IOException {
234+
return parser;
235+
}
228236
}

server/src/main/java/org/elasticsearch/index/mapper/DotExpandingXContentParser.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@
3535
*/
3636
class DotExpandingXContentParser extends FilterXContentParserWrapper {
3737

38-
static boolean isInstance(XContentParser parser) {
39-
return parser instanceof WrappingParser;
40-
}
41-
4238
private static final class WrappingParser extends FilterXContentParser {
4339

4440
private final ContentPath contentPath;
@@ -52,6 +48,11 @@ private static final class WrappingParser extends FilterXContentParser {
5248
}
5349
}
5450

51+
@Override
52+
public XContentParser switchParser(XContentParser parser) throws IOException {
53+
return new WrappingParser(parser, contentPath);
54+
}
55+
5556
@Override
5657
public Token nextToken() throws IOException {
5758
Token token;

server/src/main/java/org/elasticsearch/index/mapper/XContentDataHelper.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,9 @@ private static DocumentParserContext cloneDocumentParserContext(
297297
BytesReference.bytes(builder),
298298
context.parser().contentType()
299299
);
300-
if (DotExpandingXContentParser.isInstance(context.parser())) {
301-
// If we performed dot expanding originally we need to continue to do so when we replace the parser.
302-
newParser = DotExpandingXContentParser.expandDots(newParser, context.path());
303-
}
300+
// If we performed dot expanding originally we need to continue to do so when we replace the parser.
301+
// To reliably do that, we need to delegate switching to the parser itself as it might be decorated / wrapped.
302+
newParser = context.parser().switchParser(newParser);
304303

305304
DocumentParserContext subcontext = context.switchParser(newParser);
306305
subcontext.setRecordedSource(); // Avoids double-storing parts of the source for the same parser subtree.

0 commit comments

Comments
 (0)