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
This commit is contained in:
Chris Capurso 2022-01-12 09:32:59 -05:00 committed by GitHub
parent 3553e223ca
commit fc75aabd03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 4 deletions

View File

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