Skip to content

Commit

Permalink
feat(fieldbehavior): ignore immutable in validation if not in field mask
Browse files Browse the repository at this point in the history
AIP-203
"When a service receives an immutable field in an update request (or similar),
even if included in the update mask, the service should ignore the field if the value matches,
but should error with INVALID_ARGUMENT if a change is requested"
  • Loading branch information
oscarmuhr committed Nov 3, 2023
1 parent 0a0c056 commit 47c6047
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
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{},
},
}
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

0 comments on commit 47c6047

Please sign in to comment.