Warnings indicating ignored and replaced parameters (#14962)

* Warnings indicating ignored and replaced parameters

* Avoid additional var creation

* Add warnings only if the response is non-nil

* Return the response even when error is non-nil

* Fix tests

* Rearrange comments

* Print warning in the log

* Fix another test

* Add CL
This commit is contained in:
Vishal Nayak
2022-04-11 09:57:12 -04:00
committed by GitHub
parent f258885f7a
commit fd73653b9d
5 changed files with 88 additions and 4 deletions

View File

@@ -218,10 +218,19 @@ func (b *Backend) HandleRequest(ctx context.Context, req *logical.Request) (*log
// Build up the data for the route, with the URL taking priority
// for the fields over the PUT data.
raw := make(map[string]interface{}, len(path.Fields))
var ignored []string
for k, v := range req.Data {
raw[k] = v
if path.Fields[k] == nil {
ignored = append(ignored, k)
}
}
var replaced []string
for k, v := range captures {
if raw[k] != nil {
replaced = append(replaced, k)
}
raw[k] = v
}
@@ -275,7 +284,29 @@ func (b *Backend) HandleRequest(ctx context.Context, req *logical.Request) (*log
}
}
return callback(ctx, req, &fd)
resp, err := callback(ctx, req, &fd)
if err != nil {
return resp, err
}
switch resp {
case nil:
default:
// If fields supplied in the request are not present in the field schema
// of the path, add a warning to the response indicating that those
// parameters will be ignored.
if len(ignored) != 0 {
resp.AddWarning(fmt.Sprintf("Endpoint ignored these unrecognized parameters: %v", ignored))
}
// If fields supplied in the request is being overwritten by the values
// supplied in the API request path, add a warning to the response
// indicating that those parameters will be replaced.
if len(replaced) != 0 {
resp.AddWarning(fmt.Sprintf("Endpoint replaced the value of these parameters with the values captured from the endpoint's path: %v", replaced))
}
}
return resp, nil
}
// HandlePatchOperation acts as an abstraction for performing JSON merge patch