-
Notifications
You must be signed in to change notification settings - Fork 20
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
feat(fieldbehavior): Support optional keyword when validating REQUIRED fields #251
Conversation
optional
keyword
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.
Thank you for your contribution.
Please make sure that your commit message follows the conventional commit standard
It also looks like you have not explicitly enabled optinal support.
You should be able to do this in the main file, something like
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
8d2e889
to
d0ce031
Compare
optional
keyword
hi @alethenorio, thanks for the quick feedback. I've made the changes requested |
t.Run("missing optional field annotated with required", func(t *testing.T) { | ||
t.Parallel() | ||
assert.Error( | ||
t, | ||
ValidateRequiredFieldsWithMask( | ||
&examplefreightv1.Site{}, | ||
&fieldmaskpb.FieldMask{Paths: []string{"personnel_count"}}, | ||
), | ||
"missing required field: personnel_count", | ||
) | ||
}) |
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 am running this test without the above changes (other than enabling optional support) and it seems to pass for me.
Can you describe what kind of problem you ran into that prompted this PR? Is there a way to reproduce the issue in a test?
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.
My bad, that test wasn't enough. I've added a happy path test that hopefully clears things up a bit.
The gist of the problem is that if a field is annotated with REQUIRED
it can't be set to its default value, 0
in the case of an int64
. The optional
keyword allows for that by differentiating unset from default values. This is reflected in the generated code by making the fields pointers:
int64 personnel_count = 7;
generates
PersonnelCount int64 `protobuf:"varint,7,opt,name=personnel_count,json=personnelCount,proto3,oneof" json:"personnel_count,omitempty"`
whereas
optional int64 personnel_count = 7;
generates
PersonnelCount *int64 `protobuf:"varint,7,opt,name=personnel_count,json=personnelCount,proto3,oneof" json:"personnel_count,omitempty"`
Therefore, if a field uses the proto3 keyword optional
but has a REQUIRED
annotation, it needs to be validated as != nil
, which allows for an int64
to be 0
in a request.
The use case I want to cover, specifically, is validating the following message:
message ExtentOrBoundingBox {
// The position of a corner
message Position {
// X axis
// We use optional on a required field to differentiate between 0 and unset.
// For more info see: https://aip.dev/149#field-behavior-and-optional
optional double x = 1 [(google.api.field_behavior) = REQUIRED];
// Y axis
// We use optional on a required field to differentiate between 0 and unset.
// For more info see: https://aip.dev/149#field-behavior-and-optional
optional double y = 2 [(google.api.field_behavior) = REQUIRED];
// Z axis. Not required because this message could be used for a 2D representation too.
optional double z = 3 [(google.api.field_behavior) = OPTIONAL];
}
// lower corner
Position min = 1 [(google.api.field_behavior) = REQUIRED];
// upper corner
Position max = 2 [(google.api.field_behavior) = REQUIRED];
}
Here, x
and y
are required values that can be 0.0
, the default value.
@alethenorio @odsod PTAL |
a709e5d
to
04cb153
Compare
This PR has been open for 180 days with no activity. Remove the stale label or add a comment or it will be closed in 14 days. |
@alethenorio @odsod PTAL |
https://google.aip.dev/149 says the following on the
optional
keyword and theREQUIRED
annotation:This is useful for distinguishing setting the field to its default value (0, false, or empty string) and not setting it at all.
This PR enables validating for this combination.