From 264efe5478c5b47118f500bb0c28cffa5df16b38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Dev=C3=A8ze?= Date: Mon, 22 Aug 2011 18:09:27 +0200 Subject: [PATCH] Fix small bug in routing parsing --- .../cluster/metadata/MappingMetaData.java | 5 +-- .../cluster/metadata/ParseRoutingTests.java | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MappingMetaData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MappingMetaData.java index 86612ef9a18..99d1d07c325 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MappingMetaData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MappingMetaData.java @@ -150,12 +150,11 @@ public class MappingMetaData { // And then the value... t = parser.nextToken(); if (routingPart.equals(fieldName)) { - location++; - if (location == routing.pathElements().length) { + if (location + 1 == routing.pathElements().length) { return parser.textOrNull(); } if (t == XContentParser.Token.START_OBJECT) { - return parseRouting(parser, location); + return parseRouting(parser, location + 1); } } else { parser.skipChildren(); diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/cluster/metadata/ParseRoutingTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/cluster/metadata/ParseRoutingTests.java index 1a762023ac4..3a3df29ff4e 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/cluster/metadata/ParseRoutingTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/cluster/metadata/ParseRoutingTests.java @@ -59,4 +59,46 @@ public class ParseRoutingTests { .endObject().copiedBytes(); assertThat(md.parseRouting(XContentFactory.xContent(bytes).createParser(bytes)), equalTo("value2")); } + + @Test public void testParseRoutingWithRepeatedField() throws Exception { + MappingMetaData md = new MappingMetaData("type1", new CompressedString(""), new MappingMetaData.Routing(true, "field1.field1")); + + byte[] bytes = jsonBuilder().startObject() + .field("aaa", "wr") + .array("arr1", "1", "2", "3") + .field("field1", "foo") + .field("field1", "bar") + .field("test", "value") + .field("zzz", "wr") + .endObject().copiedBytes(); + assertThat(md.parseRouting(XContentFactory.xContent(bytes).createParser(bytes)), equalTo(null)); + } + + @Test public void testParseRoutingWithRepeatedFieldAndObject() throws Exception { + MappingMetaData md = new MappingMetaData("type1", new CompressedString(""), new MappingMetaData.Routing(true, "field1.field1.field2")); + + byte[] bytes = jsonBuilder().startObject() + .field("aaa", "wr") + .array("arr1", "1", "2", "3") + .field("field1", "foo") + .startObject("field1").field("field2", "bar").endObject() + .field("test", "value") + .field("zzz", "wr") + .endObject().copiedBytes(); + assertThat(md.parseRouting(XContentFactory.xContent(bytes).createParser(bytes)), equalTo(null)); + } + + @Test public void testParseRoutingWithRepeatedFieldAndValidRouting() throws Exception { + MappingMetaData md = new MappingMetaData("type1", new CompressedString(""), new MappingMetaData.Routing(true, "field1.field2")); + + byte[] bytes = jsonBuilder().startObject() + .field("aaa", "wr") + .array("arr1", "1", "2", "3") + .field("field1", "foo") + .startObject("field1").field("field2", "bar").endObject() + .field("test", "value") + .field("zzz", "wr") + .endObject().copiedBytes(); + assertThat(md.parseRouting(XContentFactory.xContent(bytes).createParser(bytes)), equalTo("bar")); + } }