-
Notifications
You must be signed in to change notification settings - Fork 4
Properly validate Oneof
fields
#240
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
Conversation
# Conflicts: # base/src/main/java/io/spine/validate/MessageValidator.java # base/src/test/java/io/spine/validate/MessageValidatorTest.java
Codecov Report
@@ Coverage Diff @@
## master #240 +/- ##
============================================
+ Coverage 66.22% 66.38% +0.16%
Complexity 583 583
============================================
Files 309 310 +1
Lines 8118 8158 +40
Branches 560 561 +1
============================================
+ Hits 5376 5416 +40
Misses 2625 2625
Partials 117 117 |
@armiol PTAL. |
.toBuilder() | ||
.addFieldName(oneOf.getName()) | ||
.build(); | ||
ConstraintViolation requiredFieldNotFound = |
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.
Can we have the type on the previous line (preparing for Java 11 var
syntax)?
ConstraintViolation requiredFieldNotFound = | |
ConstraintViolation requiredFieldNotFound = ConstraintViolation | |
.newBuilder() |
@Test | ||
@DisplayName("valid if a required field is set to a non-default value") | ||
void validIfRequireFieldIsNotDefault() { | ||
EveryRequired requiredIsNotDefault = EveryRequired.newBuilder() |
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.
Please have newBuilder()
here and below in new lines.
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.
Please see my comments.
* <p>Only a single field from a {@code OneOf} is validated — the field that is actually set. | ||
* If none of fields is set, a constraint violation is created. | ||
* | ||
* @see <a href="https://developers.google.com/protocol-buffers/docs/proto3#oneof">One Of documentation</a> |
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.
As long as Protobuf documentation references the language element as Oneof
, let's do the same in our code, docs and PR description. I.e. not OneOf
or One Of
.
message OneRequired { | ||
oneof fields { | ||
string required_val = 1 [(required) = true]; | ||
string non_required = 2 [(required) = false]; |
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.
It should probably be optional
.
} | ||
} | ||
|
||
message EveryNotRequired { |
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.
And this one EveryOptional
.
/** | ||
* Obtains field values of the message. | ||
* | ||
* <p>Values of {@code OneOf} fields are filtered and not returned. |
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.
...are filtered out...
* | ||
* @param oneOf | ||
* the {@code OneOf} descriptor | ||
* @return a value of the populated field or {@code Optional.empty()} if the message |
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.
It seems to me that if the message does not contain the field, which description was passed to the method, we should throw an invalid argument exception.
OneOf
fieldsOneof
fields
@alexander-yevsyukov PTAL. |
@armiol PTAL. |
This PR fixes #154.
Now
Oneof
fields are considered in a special way and only the actually set field is validated. For example, if aOneof
contains 3 fields, the only one is validated. The work is done by a newly introducedOneofValidator
.The important note is that if none of a
Oneof
fields are set, a declaration is considered invalid.So, after the change, it is possible to create a message where every
Oneof
field isrequired
(see theEveryRequired
below). And the message is considered valid if thefirst
orsecond
field is set.Also, the
config
was updated.