Skip to content

Commit

Permalink
Accept PATCH to nonexistent configs (#16)
Browse files Browse the repository at this point in the history
This allows a client to always PATCH, and create the config if
necessary.
  • Loading branch information
amrc-benmorrow authored Oct 19, 2023
2 parents ccb22e5 + a54a460 commit 6473f83
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/api-v1/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export default class API {

const st = await this.model.config_merge_patch(req.params, req.body,
etags.checker(req));
res.status(st).send();
res.status(st == 201 ? 204 : st).send();
}

config_search_parse(query) {
Expand Down
10 changes: 6 additions & 4 deletions lib/api-v1/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,17 +435,19 @@ export default class Model extends EventEmitter {
const etst = check_etag(conf?.etag);
if (etst) return etst;
}
if (conf == null) return 404;

/* This needs to be null, not undefined, for a nonexistent
* object, or the 409 test below will fail. */
const json = conf?.json ?? null;
console.log("PATCH: source: %o", json);
console.log("PATCH: patch: %o", patch);

/* Safety check: applying a merge-patch to a non-object destroys
* the whole thing. */
const json = conf.json;
if (typeof(json) != "object" || Array.isArray(json))
return 409;

const nconf = merge_patch.apply(json, patch);
console.log("PATCH: source: %o", json);
console.log("PATCH: patch: %o", patch);
console.log("PATCH: result: %o", nconf);
return await this.config_put(q, nconf);
}
Expand Down

0 comments on commit 6473f83

Please sign in to comment.