Skip to content
This repository has been archived by the owner on Aug 9, 2023. It is now read-only.

Create a patchArrayDecorator that automatically retries failed patch operations on null arrays #200

Open
julienblin opened this issue Apr 26, 2023 · 0 comments
Labels
core enhancement New feature or request

Comments

@julienblin
Copy link
Contributor

julienblin commented Apr 26, 2023

Problem

The patch operation is very useful in allowing fine-grained updates in contexts where it makes.
One annoyance though is in the context of patching array attributes.
The problem is that arrays is FHIR are not supposed to be empty. In that case, the attribute should simply not be present.
By default, JSON Patching an element of an array fails if the array is null; in this case, you have to patch the whole array.

This leads to code like this:

const hasNotes: boolean = Boolean(currentRiskAssessment.note?.length);
if (hasComments) {
  client.patch("RiskAssessment", currentRiskAssessment.id!, (patch) => patch.add("/note/-", newNote));
} else {
  client.patch("RiskAssessment", currentRiskAssessment.id!, (patch) => patch.add("/note", [newNote]));
}

It would be nice if we were able to just do this:

client.patch("RiskAssessment", currentRiskAssessment.id!, (patch) => patch.add("/note/-", newNote));

and have it work in all cases.

Suggested solution

Write a FHIR Client decorator that decorate the patch method, intercept errors on patch operations, and if there was an attempt to set a value inside an array (e.g. an add operation with the path ending in /{number or -}, retries it with setting the whole attribute with an array value.

If the retry is successful, return the retry result. If it is unsuccessful, return the original response or throw the original error.

If there are multiple patch operations pertaining to arrays, we don't retry anything as we don't know which is which.

Examples

Example interaction:

[
    { "op":"add", "path": "/note/0", "value": { "text": "this is a note" } }
]
{
    "resourceType": "OperationOutcome",
    "issue": [
        {
            "severity": "error",
            "code": "invalid",
            "details": {
                "text": "Value required at path: /note/0"
            }
        }
    ]
}

Rewrote patch

[
    { "op":"add", "path": "/note", "value": [{ "text": "this is a note" }] }
]

Error responses may vary from server to server; we may want to act on response status === 400 only, disregarding the actual OperationOutcome response.

@julienblin julienblin added enhancement New feature or request core labels Apr 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
core enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant