-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Allow specifying an exclusive set of fields on ObjectParser #52893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -274,6 +274,10 @@ public Value parse(XContentParser parser, Value value, Context context) throws I | |
String currentFieldName = null; | ||
XContentLocation currentPosition = null; | ||
List<String[]> requiredFields = new ArrayList<>(this.requiredFieldSets); | ||
List<List<String>> exclusiveFields = new ArrayList<>(); | ||
for (int i = 0; i < this.exclusiveFieldSets.size(); i++) { | ||
exclusiveFields.add(new ArrayList<>()); | ||
} | ||
|
||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { | ||
if (token == XContentParser.Token.FIELD_NAME) { | ||
|
@@ -302,18 +306,40 @@ public Value parse(XContentParser parser, Value value, Context context) throws I | |
} | ||
} | ||
|
||
// Check if this field is in an exclusive set, if it is then mark | ||
// it as seen. If the set is already marked, then we have a duplicate | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't throw an exception though! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see. It does, but later. Got it. Maybe update the comment. Not sure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I rewrote the comments a bit to make things clearer |
||
// so throw an exception | ||
for (int i = 0; i < this.exclusiveFieldSets.size(); i++) { | ||
for (String field : this.exclusiveFieldSets.get(i)) { | ||
if (field.equals(currentFieldName)) { | ||
exclusiveFields.get(i).add(currentFieldName); | ||
} | ||
} | ||
} | ||
|
||
parseSub(parser, fieldParser, currentFieldName, value, context); | ||
} | ||
fieldParser = null; | ||
} | ||
} | ||
|
||
StringBuilder message = new StringBuilder(); | ||
for (List<String> fieldset : exclusiveFields) { | ||
if (fieldset.size() > 1) { | ||
message.append("The following fields are not allowed together: ").append(fieldset.toString()).append(" "); | ||
} | ||
} | ||
if (message.length() > 0) { | ||
throw new IllegalArgumentException(message.toString()); | ||
} | ||
|
||
if (requiredFields.isEmpty() == false) { | ||
StringBuilder message = new StringBuilder(); | ||
for (String[] fields : requiredFields) { | ||
message.append("Required one of fields ").append(Arrays.toString(fields)).append(", but none were specified. "); | ||
} | ||
throw new IllegalArgumentException(message.toString()); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be worth mentioning that it doesn't make
foo
orbar
required.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++