Skip to content
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): ignore immutable in validation if not in field mask #266

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions fieldbehavior/fieldbehavior_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ func TestValidateImmutableFieldsWithMask(t *testing.T) {
err := ValidateImmutableFieldsWithMask(req, req.GetUpdateMask())
assert.NilError(t, err)
})
t.Run("no error when immutable field not part of fieldmask", func(t *testing.T) {
t.Parallel()
req := &examplefreightv1.UpdateShipmentRequest{
Shipment: &examplefreightv1.Shipment{
ExternalReferenceId: "external-reference-id",
OriginSite: "shippers/shipper1/sites/site1",
},
UpdateMask: &fieldmaskpb.FieldMask{
Paths: []string{"shipment.origin_site"},
},
}
err := ValidateImmutableFieldsWithMask(req, req.GetUpdateMask())
assert.NilError(t, err)
})
t.Run("errors when wildcard fieldmask used", func(t *testing.T) {
t.Parallel()
req := &examplefreightv1.UpdateShipmentRequest{
Expand Down Expand Up @@ -170,7 +184,7 @@ func TestValidateImmutableFieldsWithMask(t *testing.T) {
ExternalReferenceId: "I am immutable!",
},
UpdateMask: &fieldmaskpb.FieldMask{
Paths: []string{""},
Paths: []string{},
Copy link
Contributor Author

@oscarmuhr oscarmuhr Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fieldmask in this test case should be completely empty. "" is also considered a value in most other cases in the aip-go library. Most importantly it aligns with this case in fieldmask.Update:

// Special-case: No update mask.
// Update all fields of src that are set on the wire.
case len(mask.GetPaths()) == 0:
	updateWireSetFields(dstReflect, srcReflect)
...

},
}
err := ValidateImmutableFieldsWithMask(req, req.GetUpdateMask())
Expand All @@ -187,7 +201,7 @@ func TestValidateImmutableFieldsWithMask(t *testing.T) {
},
},
UpdateMask: &fieldmaskpb.FieldMask{
Paths: []string{""},
Paths: []string{},
},
}
err := ValidateImmutableFieldsWithMask(req, req.GetUpdateMask())
Expand Down
4 changes: 2 additions & 2 deletions fieldbehavior/immutable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// ValidateImmutableFieldsWithMask returns a validation error if the message
// or field mask contains a field that is immutable.
// or field mask contains a field that is immutable and a change to an immutable field is requested.
// This can be used when validating update requests and want to return
// INVALID_ARGUMENT to the user.
// If you want to ignore immutable fields rather than error then use ClearFields().
Expand All @@ -29,7 +29,7 @@ func validateImmutableFields(m protoreflect.Message, mask *fieldmaskpb.FieldMask
}

currPath += string(field.Name())
if isImmutable(field) && (hasPath(mask, currPath) || m.Has(field)) {
if isImmutable(field) && hasPath(mask, currPath) {
return fmt.Errorf("field is immutable: %s", currPath)
}

Expand Down
7 changes: 7 additions & 0 deletions fieldbehavior/required.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ func validateRequiredFields(reflectMessage protoreflect.Message, mask *fieldmask
return nil
}

func isEmpty(mask *fieldmaskpb.FieldMask) bool {
return mask == nil || len(mask.GetPaths()) == 0
}

func hasPath(mask *fieldmaskpb.FieldMask, needle string) bool {
if isEmpty(mask) {
return true
}
for _, straw := range mask.GetPaths() {
if straw == "*" || straw == needle {
return true
Expand Down