Skip to content

Commit 5feb40b

Browse files
committed
[Java] Fix validation of valueRef in fields which use a primitive encoded type. Issue #529.
1 parent 74ea339 commit 5feb40b

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed

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

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.w3c.dom.Node;
1919

2020
import java.util.List;
21+
import java.util.Map;
2122

2223
import static uk.co.real_logic.sbe.xml.XmlSchemaParser.*;
2324

@@ -84,7 +85,7 @@ public Field(
8485
this.timeUnit = timeUnit;
8586
}
8687

87-
public void validate(final Node node)
88+
public void validate(final Node node, final Map<String, Type> typeByNameMap)
8889
{
8990
if (type != null &&
9091
semanticType != null &&
@@ -96,34 +97,43 @@ public void validate(final Node node)
9697

9798
checkForValidName(node, name);
9899

100+
if (null != valueRef)
101+
{
102+
validateValueRef(node, typeByNameMap);
103+
}
104+
99105
if (type instanceof EnumType && presence == Presence.CONSTANT)
100106
{
101107
if (null == valueRef)
102108
{
103109
handleError(node, "valueRef not set for constant enum");
104110
}
105-
else
111+
}
112+
113+
if (null != valueRef && presence == Presence.CONSTANT)
114+
{
115+
final String valueRefType = valueRef.substring(0, valueRef.indexOf('.'));
116+
117+
if (!(type instanceof EnumType))
106118
{
107-
final int periodIndex = valueRef.indexOf('.');
108-
if (periodIndex < 1 || periodIndex == (valueRef.length() - 1))
119+
if (type instanceof EncodedDataType)
109120
{
110-
handleError(
111-
node, "valueRef format not valid for constant (enum-name.valid-value-name): " + valueRef);
112-
}
121+
final EnumType enumType = (EnumType)typeByNameMap.get(valueRefType);
113122

114-
final String valueRefType = valueRef.substring(0, periodIndex);
115-
if (!valueRefType.equals(type.name()))
116-
{
117-
handleError(node, "valueRef for enum name not found: " + valueRefType);
123+
if (((EncodedDataType)type).primitiveType() != enumType.encodingType())
124+
{
125+
handleError(node, "valueRef does not match field type: " + valueRef);
126+
}
118127
}
119-
120-
final String validValueName = valueRef.substring(periodIndex + 1);
121-
final EnumType enumType = (EnumType)type;
122-
if (null == enumType.getValidValue(validValueName))
128+
else
123129
{
124-
handleError(node, "valueRef for validValue name not found: " + validValueName);
130+
handleError(node, "valueRef does not match field type: " + valueRef);
125131
}
126132
}
133+
else if (!valueRefType.equals(type.name()))
134+
{
135+
handleError(node, "valueRef for enum name not found: " + valueRefType);
136+
}
127137
}
128138
}
129139

@@ -255,6 +265,38 @@ public String toString()
255265
'}';
256266
}
257267

268+
private void validateValueRef(final Node node, final Map<String, Type> typeByNameMap)
269+
{
270+
final int periodIndex = valueRef.indexOf('.');
271+
if (periodIndex < 1 || periodIndex == (valueRef.length() - 1))
272+
{
273+
handleError(
274+
node, "valueRef format not valid for constant (enum-name.valid-value-name): " + valueRef);
275+
}
276+
277+
final String valueRefType = valueRef.substring(0, periodIndex);
278+
final Type valueType = typeByNameMap.get(valueRefType);
279+
if (null == valueType)
280+
{
281+
handleError(node, "valueRef for enum name not found: " + valueRefType);
282+
}
283+
284+
if (valueType instanceof EnumType)
285+
{
286+
final EnumType enumType = (EnumType)valueType;
287+
final String validValueName = valueRef.substring(periodIndex + 1);
288+
289+
if (null == enumType.getValidValue(validValueName))
290+
{
291+
handleError(node, "valueRef for validValue name not found: " + validValueName);
292+
}
293+
}
294+
else
295+
{
296+
handleError(node, "valueRef for is not of type enum: " + valueRefType);
297+
}
298+
}
299+
258300
public static class Builder
259301
{
260302
private String name;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ private Field parseField(final NodeList nodeList, final int nodeIndex)
278278
.type(fieldType)
279279
.build();
280280

281-
field.validate(node);
281+
field.validate(node, typeByNameMap);
282282

283283
return field;
284284
}
@@ -338,7 +338,7 @@ else if (!(fieldType instanceof CompositeType))
338338
.variableLength(true)
339339
.build();
340340

341-
field.validate(node);
341+
field.validate(node, typeByNameMap);
342342

343343
return field;
344344
}

0 commit comments

Comments
 (0)