|
23 | 23 | import org.elasticsearch.cluster.block.ClusterBlockLevel; |
24 | 24 | import org.elasticsearch.cluster.node.DiscoveryNodeFilters; |
25 | 25 | import org.elasticsearch.cluster.routing.allocation.IndexMetadataUpdater; |
| 26 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 27 | +import org.elasticsearch.common.xcontent.ToXContentFragment; |
| 28 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 29 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 30 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 31 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 32 | +import org.elasticsearch.common.xcontent.XContentParserUtils; |
26 | 33 | import org.elasticsearch.core.Nullable; |
27 | 34 | import org.elasticsearch.common.collect.ImmutableOpenIntMap; |
28 | 35 | import org.elasticsearch.common.collect.ImmutableOpenMap; |
|
34 | 41 | import org.elasticsearch.common.settings.Setting; |
35 | 42 | import org.elasticsearch.common.settings.Setting.Property; |
36 | 43 | import org.elasticsearch.common.settings.Settings; |
37 | | -import org.elasticsearch.common.xcontent.ToXContent; |
38 | | -import org.elasticsearch.common.xcontent.ToXContentFragment; |
39 | | -import org.elasticsearch.common.xcontent.XContentBuilder; |
40 | | -import org.elasticsearch.common.xcontent.XContentFactory; |
41 | | -import org.elasticsearch.common.xcontent.XContentHelper; |
42 | | -import org.elasticsearch.common.xcontent.XContentParser; |
43 | 44 | import org.elasticsearch.gateway.MetadataStateFormat; |
44 | 45 | import org.elasticsearch.index.Index; |
45 | 46 | import org.elasticsearch.index.mapper.MapperService; |
@@ -1422,16 +1423,12 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti |
1422 | 1423 | if (parser.currentToken() == XContentParser.Token.START_OBJECT) { // on a start object move to next token |
1423 | 1424 | parser.nextToken(); |
1424 | 1425 | } |
1425 | | - if (parser.currentToken() != XContentParser.Token.FIELD_NAME) { |
1426 | | - throw new IllegalArgumentException("expected field name but got a " + parser.currentToken()); |
1427 | | - } |
| 1426 | + XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser); |
1428 | 1427 | Builder builder = new Builder(parser.currentName()); |
1429 | 1428 |
|
1430 | 1429 | String currentFieldName = null; |
1431 | 1430 | XContentParser.Token token = parser.nextToken(); |
1432 | | - if (token != XContentParser.Token.START_OBJECT) { |
1433 | | - throw new IllegalArgumentException("expected object but got a " + token); |
1434 | | - } |
| 1431 | + XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser); |
1435 | 1432 | boolean mappingVersion = false; |
1436 | 1433 | boolean settingsVersion = false; |
1437 | 1434 | boolean aliasesVersion = false; |
@@ -1514,11 +1511,8 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti |
1514 | 1511 | } else if (KEY_PRIMARY_TERMS.equals(currentFieldName)) { |
1515 | 1512 | LongArrayList list = new LongArrayList(); |
1516 | 1513 | while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { |
1517 | | - if (token == XContentParser.Token.VALUE_NUMBER) { |
1518 | | - list.add(parser.longValue()); |
1519 | | - } else { |
1520 | | - throw new IllegalStateException("found a non-numeric value under [" + KEY_PRIMARY_TERMS + "]"); |
1521 | | - } |
| 1514 | + XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser); |
| 1515 | + list.add(parser.longValue()); |
1522 | 1516 | } |
1523 | 1517 | builder.primaryTerms(list.toArray()); |
1524 | 1518 | } else { |
@@ -1549,6 +1543,7 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti |
1549 | 1543 | throw new IllegalArgumentException("Unexpected token " + token); |
1550 | 1544 | } |
1551 | 1545 | } |
| 1546 | + XContentParserUtils.ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser); |
1552 | 1547 | if (Assertions.ENABLED) { |
1553 | 1548 | assert mappingVersion : "mapping version should be present for indices created on or after 6.5.0"; |
1554 | 1549 | } |
|
0 commit comments