Skip to content

Commit 88a7b45

Browse files
committed
[Java] Detect duplicate ids in message members. Fix for issue #597.
1 parent fa5b60b commit 88a7b45

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/xml/Message.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package uk.co.real_logic.sbe.xml;
1717

18+
import org.agrona.collections.IntHashSet;
1819
import org.agrona.collections.ObjectHashSet;
1920
import uk.co.real_logic.sbe.ir.Token;
2021

@@ -75,7 +76,7 @@ public Message(final Node messageNode, final Map<String, Type> typeByNameMap) th
7576
semanticType = getAttributeValueOrNull(messageNode, "semanticType"); // optional
7677
this.typeByNameMap = typeByNameMap;
7778

78-
fieldList = parseFieldsAndGroups(messageNode);
79+
fieldList = parseMembers(messageNode);
7980
computeAndValidateOffsets(messageNode, fieldList, blockLength);
8081

8182
computedBlockLength = computeMessageRootBlockLength(fieldList);
@@ -162,14 +163,15 @@ public int blockLength()
162163
return blockLength > computedBlockLength ? blockLength : computedBlockLength;
163164
}
164165

165-
private List<Field> parseFieldsAndGroups(final Node node) throws XPathExpressionException
166+
private List<Field> parseMembers(final Node node) throws XPathExpressionException
166167
{
167168
final XPath xPath = XPathFactory.newInstance().newXPath();
168169
final NodeList list = (NodeList)xPath.compile(FIELD_OR_GROUP_OR_DATA_EXPR).evaluate(node, NODESET);
169170
boolean groupEncountered = false, dataEncountered = false;
170171

171172
final ObjectHashSet<String> distinctNames = new ObjectHashSet<>();
172-
final List<Field> fieldList = new ArrayList<>();
173+
final IntHashSet distinctIds = new IntHashSet();
174+
final ArrayList<Field> fieldList = new ArrayList<>();
173175

174176
for (int i = 0, size = list.getLength(); i < size; i++)
175177
{
@@ -206,6 +208,11 @@ private List<Field> parseFieldsAndGroups(final Node node) throws XPathExpression
206208
throw new IllegalStateException("Unknown node name: " + nodeName);
207209
}
208210

211+
if (!distinctIds.add(field.id()))
212+
{
213+
handleError(node, "duplicate id found: " + field.id());
214+
}
215+
209216
if (!distinctNames.add(field.name()))
210217
{
211218
handleError(node, "duplicate name found: " + field.name());
@@ -248,7 +255,7 @@ else if (!(dimensionType instanceof CompositeType))
248255

249256
XmlSchemaParser.checkForValidName(node, field.name());
250257

251-
field.groupFields(parseFieldsAndGroups(node)); // recursive call
258+
field.groupFields(parseMembers(node)); // recursive call
252259

253260
return field;
254261
}

sbe-tool/src/test/java/uk/co/real_logic/sbe/xml/ErrorHandlerTest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void shouldExitAfterMessage()
138138
throws Exception
139139
{
140140
exceptionRule.expect(IllegalStateException.class);
141-
exceptionRule.expectMessage("had 11 errors");
141+
exceptionRule.expectMessage("had 13 errors");
142142

143143
final ParserOptions options = ParserOptions.builder().suppressOutput(true).warningsFatal(true).build();
144144

@@ -181,12 +181,6 @@ public void shouldExitInvalidFieldNames()
181181
parse(TestUtil.getLocalResource("error-handler-invalid-name.xml"), options);
182182
}
183183

184-
/*
185-
* TODO:
186-
* left over entry count and length field (warning)
187-
* dup field id? (not currently tracked)
188-
*/
189-
190184
private static void parseTestXmlAddToMap(
191185
final Map<String, Type> map, final String xPathExpr, final String xml, final ErrorHandler handler)
192186
throws ParserConfigurationException, XPathExpressionException, IOException, SAXException

0 commit comments

Comments
 (0)