Skip to content

Conversation

dmytro-grankin
Copy link
Contributor

@dmytro-grankin dmytro-grankin commented Nov 2, 2018

This PR fixes #154.

Now Oneof fields are considered in a special way and only the actually set field is validated. For example, if a Oneof contains 3 fields, the only one is validated. The work is done by a newly introduced OneofValidator.

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 is required (see the EveryRequired below). And the message is considered valid if the first or second field is set.

message EveryRequired {
    oneof fields {
        string first = 1 [(required) = true];
        string second = 2 [(required) = true];
    }
}

Also, the config was updated.

@dmytro-grankin dmytro-grankin self-assigned this Nov 2, 2018
@codecov
Copy link

codecov bot commented Nov 2, 2018

Codecov Report

Merging #240 into master will increase coverage by 0.16%.
The diff coverage is 100%.

@@             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

@dmytro-grankin dmytro-grankin requested a review from armiol November 2, 2018 18:12
@dmytro-grankin
Copy link
Contributor Author

@armiol PTAL.

.toBuilder()
.addFieldName(oneOf.getName())
.build();
ConstraintViolation requiredFieldNotFound =
Copy link
Contributor

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)?

Suggested change
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()
Copy link
Contributor

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.

Copy link
Collaborator

@armiol armiol left a 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>
Copy link
Collaborator

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];
Copy link
Collaborator

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 {
Copy link
Collaborator

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.
Copy link
Collaborator

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
Copy link
Collaborator

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.

@dmytro-grankin dmytro-grankin changed the title Properly validate OneOf fields Properly validate Oneof fields Nov 5, 2018
@dmytro-grankin
Copy link
Contributor Author

@alexander-yevsyukov PTAL.

@dmytro-grankin
Copy link
Contributor Author

@armiol PTAL.

@dmytro-grankin dmytro-grankin merged commit 844985e into master Dec 12, 2018
@dmytro-grankin dmytro-grankin deleted the required-oneof-fields branch December 12, 2018 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to make oneof options required
3 participants