Skip to content

Commit

Permalink
[7.x] Fix copyCurentStructure(MapXContentParser) (#76357) (#76374)
Browse files Browse the repository at this point in the history
* Fix copyCurentStructure(MapXContentParser) (#76357)

This stops `MapXContentParser` from throwing an
`UnsupportedOperationException` when passed as an argument to
`XContentBuilder#copyCurrentStructure`. This is mostly useful in tests
where `Map` is a convenient way to talk about structured configuration
but the production APIs need the map to be embedded into a larger blob
of `XContent`.

Co-authored-by: Elastic Machine <[email protected]>

* Fixup

Co-authored-by: Nik Everett <[email protected]>
Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
3 people authored Aug 11, 2021
1 parent 6a6212f commit 4941d0e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public Object objectBytes() throws IOException {

@Override
public boolean hasTextCharacters() {
throw new UnsupportedOperationException("use text() instead");
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.elasticsearch.core.CheckedConsumer;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.common.xcontent.support.MapXContentParser;
import org.elasticsearch.test.ESTestCase;

Expand Down Expand Up @@ -57,19 +58,51 @@ public void testSimpleMap() throws IOException {
});
}


public void testRandomObject() throws IOException {
compareTokens(builder -> generateRandomObject(builder, randomIntBetween(0, 10)));
}

public void compareTokens(CheckedConsumer<XContentBuilder, IOException> consumer) throws IOException {
/**
* Assert that {@link XContentParser#hasTextCharacters()} returns false because
* we don't support {@link XContentParser#textCharacters()}.
*/
public void testHasTextCharacters() throws IOException {
assertFalse(
new MapXContentParser(
xContentRegistry(),
LoggingDeprecationHandler.INSTANCE,
org.elasticsearch.core.Map.of("a", "b"),
randomFrom(XContentType.values())
).hasTextCharacters()
);
}

public void testCopyCurrentStructure() throws IOException {
try (
XContentParser parser = new MapXContentParser(
xContentRegistry(),
LoggingDeprecationHandler.INSTANCE,
org.elasticsearch.core.Map.of("a", "b"),
randomFrom(XContentType.values())
)
) {
try (
XContentBuilder builder = JsonXContent.contentBuilder().copyCurrentStructure(parser);
XContentParser copied = createParser(builder)
) {
assertEquals(copied.map(), org.elasticsearch.core.Map.of("a", "b"));
}
}
}

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

public void compareTokens(CheckedConsumer<XContentBuilder, IOException> consumer, XContentType xContentType) throws IOException {
private void compareTokens(CheckedConsumer<XContentBuilder, IOException> consumer, XContentType xContentType) throws IOException {
try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
consumer.accept(builder);
final Map<String, Object> map;
Expand Down

0 comments on commit 4941d0e

Please sign in to comment.