From c491df83200fab2a118e7b0c1cb76cc534e1aa1e Mon Sep 17 00:00:00 2001 From: Yang Wang Date: Thu, 30 Jun 2022 14:42:00 +1000 Subject: [PATCH 1/2] [Test] Fix parsing of exception objects The exception parsing logic expects the starting curly bracket already be consumed. This PR ensures the expectation is met. Resolves: #88166 --- .../org/elasticsearch/test/TestSecurityClient.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/test/TestSecurityClient.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/test/TestSecurityClient.java index abca7d63c9a98..970021fcbebe6 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/test/TestSecurityClient.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/test/TestSecurityClient.java @@ -48,6 +48,7 @@ import java.util.Optional; import java.util.stream.Collectors; +import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; import static org.elasticsearch.test.rest.ESRestTestCase.entityAsMap; public class TestSecurityClient { @@ -389,7 +390,7 @@ public TokenInvalidation invalidateTokens(String requestBody) throws IOException return new TokenInvalidation( ((Number) responseBody.get("invalidated_tokens")).intValue(), ((Number) responseBody.get("previously_invalidated_tokens")).intValue(), - errors == null ? List.of() : errors.stream().map(this::toException).toList() + errors == null ? List.of() : errors.stream().map(TestSecurityClient::toException).toList() ); } @@ -463,11 +464,10 @@ private XContentParser getParser(Response response) throws IOException { return XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, responseBody); } - private ElasticsearchException toException(Map map) { - try { - return ElasticsearchException.fromXContent( - XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, toJson(map)) - ); + private static ElasticsearchException toException(Map map) { + try (final XContentParser parser = XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, toJson(map))) { + ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); + return ElasticsearchException.fromXContent(parser); } catch (IOException e) { throw new RuntimeIoException(e); } From 3d9f7340371ff8093e100f8f85b1d3c0873c5f12 Mon Sep 17 00:00:00 2001 From: Yang Wang Date: Thu, 30 Jun 2022 14:52:00 +1000 Subject: [PATCH 2/2] tweak --- .../test/java/org/elasticsearch/test/TestSecurityClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/test/TestSecurityClient.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/test/TestSecurityClient.java index 970021fcbebe6..6da61510ed8a1 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/test/TestSecurityClient.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/test/TestSecurityClient.java @@ -465,7 +465,7 @@ private XContentParser getParser(Response response) throws IOException { } private static ElasticsearchException toException(Map map) { - try (final XContentParser parser = XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, toJson(map))) { + try (var parser = XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, toJson(map))) { ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); return ElasticsearchException.fromXContent(parser); } catch (IOException e) {