From b48602fdb8853dca4922ec25504cfa75579bad31 Mon Sep 17 00:00:00 2001 From: Chris Capurso Date: Wed, 12 Jan 2022 09:32:59 -0500 Subject: [PATCH] use GetOkErr in patch handler so type coercion errors result in error response (#13191) * use GetOkErr in patch handler so unknown fields result in error response * do not error on unknown fields for patch handling * godoc update for HandlePatchOperation --- sdk/framework/backend.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/sdk/framework/backend.go b/sdk/framework/backend.go index 2a71d14f45..8c69d9f88f 100644 --- a/sdk/framework/backend.go +++ b/sdk/framework/backend.go @@ -284,7 +284,9 @@ func (b *Backend) HandleRequest(ctx context.Context, req *logical.Request) (*log // the input and existing resource prior to performing the JSON merge operation // using the MergePatch function from the json-patch library. The preprocessor // is an arbitrary func that can be provided to further process the input. The -// MergePatch function accepts and returns byte arrays. +// MergePatch function accepts and returns byte arrays. Null values will unset +// fields defined within the input's FieldData (as if they were never specified) +// and remove user-specified keys that exist within a map field. func HandlePatchOperation(input *FieldData, resource map[string]interface{}, preprocessor PatchPreprocessorFunc) ([]byte, error) { var err error @@ -294,11 +296,18 @@ func HandlePatchOperation(input *FieldData, resource map[string]interface{}, pre inputMap := map[string]interface{}{} - // Parse all fields to ensure data types are handled properly according to the FieldSchema for key := range input.Raw { - val, ok := input.GetOk(key) + if _, ok := input.Schema[key]; !ok { + // Only accept fields in the schema + continue + } + + // Ensure data types are handled properly according to the FieldSchema + val, ok, err := input.GetOkErr(key) + if err != nil { + return nil, err + } - // Only accept fields in the schema if ok { inputMap[key] = val }