From 17b0041aaf036bde587981475823dff6976e12ba Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Tue, 15 Nov 2016 16:26:37 -0500 Subject: [PATCH] Strict level parsing for indices stats A previous commit added strict level parsing for the node stats API, but that commit missed adding the same for the indices stats API. This commit rectifies this miss. Relates #21577 --- .../indices/stats/IndicesStatsResponse.java | 8 ++-- .../stats/IndicesStatsResponseTests.java | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 core/src/test/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsResponseTests.java diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsResponse.java b/core/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsResponse.java index 3cbd7db66aa..839c27e0b8a 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsResponse.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsResponse.java @@ -152,12 +152,14 @@ public class IndicesStatsResponse extends BroadcastResponse implements ToXConten @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - String level = params.param("level", "indices"); - boolean isLevelValid = "indices".equalsIgnoreCase(level) || "shards".equalsIgnoreCase(level) || "cluster".equalsIgnoreCase(level); + final String level = params.param("level", "indices"); + final boolean isLevelValid = + "cluster".equalsIgnoreCase(level) || "indices".equalsIgnoreCase(level) || "shards".equalsIgnoreCase(level); if (!isLevelValid) { - return builder; + throw new IllegalArgumentException("level parameter must be one of [cluster] or [indices] or [shards] but was [" + level + "]"); } + builder.startObject("_all"); builder.startObject("primaries"); diff --git a/core/src/test/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsResponseTests.java b/core/src/test/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsResponseTests.java new file mode 100644 index 00000000000..b419be0465b --- /dev/null +++ b/core/src/test/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsResponseTests.java @@ -0,0 +1,43 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.action.admin.indices.stats; + +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.test.ESTestCase; + +import java.util.Collections; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.object.HasToString.hasToString; + + +public class IndicesStatsResponseTests extends ESTestCase { + + public void testInvalidLevel() { + final IndicesStatsResponse response = new IndicesStatsResponse(); + final String level = randomAsciiOfLength(16); + final ToXContent.Params params = new ToXContent.MapParams(Collections.singletonMap("level", level)); + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> response.toXContent(null, params)); + assertThat( + e, + hasToString(containsString("level parameter must be one of [cluster] or [indices] or [shards] but was [" + level + "]"))); + } + +}