Skip to content

Commit

Permalink
Merge pull request #57 from the-thing/allowUnknownMsgFieldsFix
Browse files Browse the repository at this point in the history
QFJ-656 - fixed logic around field validation
(cherry picked from commit fbafe6b)
  • Loading branch information
chrjohn committed Jan 6, 2016
1 parent 43c3f0c commit 13b081b
Show file tree
Hide file tree
Showing 4 changed files with 331 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ <H3>QuickFIX Settings</H3>
</TR>
<TR ALIGN="left" VALIGN="middle">
<TD><I>ValidateUserDefinedFields</I></TD>
<TD>If set to N, user defined fields will not be rejected if they are not
<TD>If set to N, user defined fields (field with tag >= 5000) will not be rejected if they are not
defined in the data dictionary, or are present in messages they do not
belong to.</TD>
<TD>Y<br>
Expand Down Expand Up @@ -369,8 +369,9 @@ <H3>QuickFIX Settings</H3>
</TR>
<TR ALIGN="left" VALIGN="middle">
<TD><I>AllowUnknownMsgFields</I></TD>
<TD>Allow unknown fields in messages. This is intended for unknown fields with tags < 5000
(not user defined fields). Use ValidateUserDefinedFields for controlling validation of tags >= 5000.</TD>
<TD>If set to Y, non user defined fields (field with tag < 5000) will not be rejected if they are not
defined in the data dictionary, or are present in messages they do not
belong to.
<TD>Y<br>
N</TD>
<TD>N</TD>
Expand Down
41 changes: 23 additions & 18 deletions quickfixj-core/src/main/java/quickfix/DataDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -654,11 +654,8 @@ private void iterate(FieldMap map, String msgType, DataDictionary dd) throws Inc
checkValue(field);
}

if (beginString != null && shouldCheckTag(field)) {
dd.checkValidTagNumber(field);
if (map instanceof Message) {
checkIsInMessage(field, msgType);
}
if (beginString != null) {
dd.checkField(field, msgType, map instanceof Message);
dd.checkGroupCount(field, map, msgType);
}
}
Expand All @@ -680,17 +677,33 @@ private void checkMsgType(String msgType) {
}
}

// / If we need to check for the tag in the dictionary
private boolean shouldCheckTag(Field<?> field) {
return checkUserDefinedFields || field.getField() < USER_DEFINED_TAG_MIN;
}

// / Check if field tag number is defined in spec.
void checkValidTagNumber(Field<?> field) {
if (!fields.contains(Integer.valueOf(field.getTag()))) {
throw new FieldException(SessionRejectReason.INVALID_TAG_NUMBER, field.getField());
}
}

// / Check if field tag is defined for message or group
void checkField(Field<?> field, String msgType, boolean message) {
// use different validation for groups and messages
boolean messageField = message ? isMsgField(msgType, field.getField()) : fields.contains(field.getField());
boolean fail;

if (field.getField() < USER_DEFINED_TAG_MIN) {
fail = !messageField && !allowUnknownMessageFields;
} else {
fail = !messageField && checkUserDefinedFields;
}

if (fail) {
if (fields.contains(Integer.valueOf(field.getTag()))) {
throw new FieldException(SessionRejectReason.TAG_NOT_DEFINED_FOR_THIS_MESSAGE_TYPE, field.getField());
} else {
throw new FieldException(SessionRejectReason.INVALID_TAG_NUMBER, field.getField());
}
}
}

private void checkValidFormat(StringField field) throws IncorrectDataFormat {
try {
Expand Down Expand Up @@ -773,14 +786,6 @@ private void checkHasValue(StringField field) {
}
}

// / Check if a field is in this message type.
private void checkIsInMessage(Field<?> field, String msgType) {
if (!isMsgField(msgType, field.getField()) && !allowUnknownMessageFields) {
throw new FieldException(SessionRejectReason.TAG_NOT_DEFINED_FOR_THIS_MESSAGE_TYPE,
field.getField());
}
}

// / Check if group count matches number of groups in
private void checkGroupCount(StringField field, FieldMap fieldMap, String msgType) {
final int fieldNum = field.getField();
Expand Down

0 comments on commit 13b081b

Please sign in to comment.