|
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; |
@@ -1502,16 +1503,12 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti |
1502 | 1503 | if (parser.currentToken() == XContentParser.Token.START_OBJECT) { // on a start object move to next token |
1503 | 1504 | parser.nextToken(); |
1504 | 1505 | } |
1505 | | - if (parser.currentToken() != XContentParser.Token.FIELD_NAME) { |
1506 | | - throw new IllegalArgumentException("expected field name but got a " + parser.currentToken()); |
1507 | | - } |
| 1506 | + XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser); |
1508 | 1507 | Builder builder = new Builder(parser.currentName()); |
1509 | 1508 |
|
1510 | 1509 | String currentFieldName = null; |
1511 | 1510 | XContentParser.Token token = parser.nextToken(); |
1512 | | - if (token != XContentParser.Token.START_OBJECT) { |
1513 | | - throw new IllegalArgumentException("expected object but got a " + token); |
1514 | | - } |
| 1511 | + XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser); |
1515 | 1512 | boolean mappingVersion = false; |
1516 | 1513 | boolean settingsVersion = false; |
1517 | 1514 | boolean aliasesVersion = false; |
@@ -1594,11 +1591,8 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti |
1594 | 1591 | } else if (KEY_PRIMARY_TERMS.equals(currentFieldName)) { |
1595 | 1592 | LongArrayList list = new LongArrayList(); |
1596 | 1593 | while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { |
1597 | | - if (token == XContentParser.Token.VALUE_NUMBER) { |
1598 | | - list.add(parser.longValue()); |
1599 | | - } else { |
1600 | | - throw new IllegalStateException("found a non-numeric value under [" + KEY_PRIMARY_TERMS + "]"); |
1601 | | - } |
| 1594 | + XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser); |
| 1595 | + list.add(parser.longValue()); |
1602 | 1596 | } |
1603 | 1597 | builder.primaryTerms(list.toArray()); |
1604 | 1598 | } else { |
@@ -1629,7 +1623,7 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti |
1629 | 1623 | throw new IllegalArgumentException("Unexpected token " + token); |
1630 | 1624 | } |
1631 | 1625 | } |
1632 | | - |
| 1626 | + XContentParserUtils.ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser); |
1633 | 1627 | Version indexVersion = indexCreatedVersion(builder.settings); |
1634 | 1628 | if (Assertions.ENABLED && indexVersion.onOrAfter(Version.V_6_5_0)) { |
1635 | 1629 | assert mappingVersion : "mapping version should be present for indices created on or after 6.5.0"; |
|
0 commit comments