Skip to content

Commit fbafe6b

Browse files
committed
Merge pull request #57 from the-thing/allowUnknownMsgFieldsFix
QFJ-656 - fixed logic around field validation
2 parents bc76f6f + 3276829 commit fbafe6b

File tree

4 files changed

+331
-27
lines changed

4 files changed

+331
-27
lines changed

quickfixj-core/src/main/doc/usermanual/usage/configuration.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ <H3>QuickFIX Settings</H3>
330330
</TR>
331331
<TR ALIGN="left" VALIGN="middle">
332332
<TD><I>ValidateUserDefinedFields</I></TD>
333-
<TD>If set to N, user defined fields will not be rejected if they are not
333+
<TD>If set to N, user defined fields (field with tag >= 5000) will not be rejected if they are not
334334
defined in the data dictionary, or are present in messages they do not
335335
belong to.</TD>
336336
<TD>Y<br>
@@ -369,8 +369,9 @@ <H3>QuickFIX Settings</H3>
369369
</TR>
370370
<TR ALIGN="left" VALIGN="middle">
371371
<TD><I>AllowUnknownMsgFields</I></TD>
372-
<TD>Allow unknown fields in messages. This is intended for unknown fields with tags < 5000
373-
(not user defined fields). Use ValidateUserDefinedFields for controlling validation of tags >= 5000.</TD>
372+
<TD>If set to Y, non user defined fields (field with tag < 5000) will not be rejected if they are not
373+
defined in the data dictionary, or are present in messages they do not
374+
belong to.
374375
<TD>Y<br>
375376
N</TD>
376377
<TD>N</TD>

quickfixj-core/src/main/java/quickfix/DataDictionary.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -654,11 +654,8 @@ private void iterate(FieldMap map, String msgType, DataDictionary dd) throws Inc
654654
checkValue(field);
655655
}
656656

657-
if (beginString != null && shouldCheckTag(field)) {
658-
dd.checkValidTagNumber(field);
659-
if (map instanceof Message) {
660-
checkIsInMessage(field, msgType);
661-
}
657+
if (beginString != null) {
658+
dd.checkField(field, msgType, map instanceof Message);
662659
dd.checkGroupCount(field, map, msgType);
663660
}
664661
}
@@ -680,17 +677,33 @@ private void checkMsgType(String msgType) {
680677
}
681678
}
682679

683-
// / If we need to check for the tag in the dictionary
684-
private boolean shouldCheckTag(Field<?> field) {
685-
return checkUserDefinedFields || field.getField() < USER_DEFINED_TAG_MIN;
686-
}
687-
688680
// / Check if field tag number is defined in spec.
689681
void checkValidTagNumber(Field<?> field) {
690682
if (!fields.contains(Integer.valueOf(field.getTag()))) {
691683
throw new FieldException(SessionRejectReason.INVALID_TAG_NUMBER, field.getField());
692684
}
693685
}
686+
687+
// / Check if field tag is defined for message or group
688+
void checkField(Field<?> field, String msgType, boolean message) {
689+
// use different validation for groups and messages
690+
boolean messageField = message ? isMsgField(msgType, field.getField()) : fields.contains(field.getField());
691+
boolean fail;
692+
693+
if (field.getField() < USER_DEFINED_TAG_MIN) {
694+
fail = !messageField && !allowUnknownMessageFields;
695+
} else {
696+
fail = !messageField && checkUserDefinedFields;
697+
}
698+
699+
if (fail) {
700+
if (fields.contains(Integer.valueOf(field.getTag()))) {
701+
throw new FieldException(SessionRejectReason.TAG_NOT_DEFINED_FOR_THIS_MESSAGE_TYPE, field.getField());
702+
} else {
703+
throw new FieldException(SessionRejectReason.INVALID_TAG_NUMBER, field.getField());
704+
}
705+
}
706+
}
694707

695708
private void checkValidFormat(StringField field) throws IncorrectDataFormat {
696709
try {
@@ -773,14 +786,6 @@ private void checkHasValue(StringField field) {
773786
}
774787
}
775788

776-
// / Check if a field is in this message type.
777-
private void checkIsInMessage(Field<?> field, String msgType) {
778-
if (!isMsgField(msgType, field.getField()) && !allowUnknownMessageFields) {
779-
throw new FieldException(SessionRejectReason.TAG_NOT_DEFINED_FOR_THIS_MESSAGE_TYPE,
780-
field.getField());
781-
}
782-
}
783-
784789
// / Check if group count matches number of groups in
785790
private void checkGroupCount(StringField field, FieldMap fieldMap, String msgType) {
786791
final int fieldNum = field.getField();

0 commit comments

Comments
 (0)