# Add support for FHIR PATCH operations

**URL:** <https://talk.openmrs.org/t/add-support-for-fhir-patch-operations/39042>\
**Category:** FHIR\
**Tags:** fhir, gsoc2023\
**Created:** [March 16, 2023, 10:35pm UTC](https://talk.openmrs.org/t/add-support-for-fhir-patch-operations/39042 "2023-03-16T22:35:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mherman22](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mherman22/32/20233_2.png) [@mherman22](https://talk.openmrs.org/u/mherman22)\
**Post date:** [March 16, 2023, 10:35pm UTC](https://talk.openmrs.org/t/add-support-for-fhir-patch-operations/39042/1 "2023-03-16T22:35:06Z")

</div>

Hey,

I have been looking into the **Add support for FHIR PATCH operations** in the [openmrs fhir2 module](https://github.com/mherman22/openmrs-module-fhir2). Its unfortunate that this project has not been chosen for **gsoc 2023** but nevertheless i thought i should share my findings and maybe when its matured enough to be taken on next time, we have a starter pack and maybe get more input on the project from the community.

> **Patch Requests explanation**
>
> - [PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH) requests essentially comprise of instructions on how to modify a resource or resources. Patch requests can be used to avoid sending a whole document when only a part has changed and this is where it basically differs with the [PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT) request.
> 
> - Patch Request formats include the following [JSON PATCH](https://www.rfc-editor.org/rfc/rfc6902), [XML PATCH](https://www.rfc-editor.org/rfc/rfc5261) which have to be implemented within the openmrs fhir2 module and in addition to those, the [FHIR PATCH](http://hl7.org/implement/standards/fhir/fhirpatch.html) has to be included aswell as a way of ensuring that when slight changes are made to the Json document, the entire document does not have to be sent.
> 
> - The Patch operations used within the above mentioned patch formats are `add, replace, move, copy, test, remove`.

We can get the necessary parameters for the [patch operation](https://hapifhir.io/hapi-fhir/docs/server_plain/rest_operations.html#instance-level-patch) from the [HAPI framework](https://hapifhir.io/hapi-fhir/) and then pass those parameters along to the [FhirService](https://github.com/openmrs/openmrs-module-fhir2/blob/master/api/src/main/java/org/openmrs/module/fhir2/api/FhirService.java) interface which is depended on by the resource providers. The interface can then be implemented within [BaseFhirService](https://github.com/openmrs/openmrs-module-fhir2/blob/master/api/src/main/java/org/openmrs/module/fhir2/api/impl/BaseFhirService.java) class.

@ibacher advised that Supporting **FHIRPath** Patch is the most complicated part of the project because there’s no stand-alone library for it and the incoming format is a little **FHIR-specific**. Though he thinks there’s some stuff in HAPI [we can use for inspiration](https://github.com/hapifhir/hapi-fhir/blob/bfa8ca957136a486840e2848485aedd69153e3e0/hapi-fhir-storage/src/main/java/ca/uhn/fhir/jpa/patch/FhirPatch.java).

For both **JSON Patch** and **XML Patch** , we can probably rely on existing libraries, e.g., like [zjsonpatch](https://github.com/flipkart-incubator/zjsonpatch), in which case the code we need needs to:

1. Detect the incoming format
2. Load the existing resource in the appropriate format
3. Apply the patch

**More Resources**

- [FHIR CRUD Operations - Smile CDR Documentation](https://smilecdr.com/docs/fhir_standard/fhir_crud_operations.html)
- [Updating a resource using patch | Hay on FHIR](https://fhirblog.com/2019/08/13/updating-a-resource-using-patch/)
- [https://jsonpatch.com/](https://jsonpatch.com/)
- [https://jsonpatch.me/](https://jsonpatch.me/)
- [GitHub - hapifhir/fhir-tutorial](https://github.com/hapifhir/fhir-tutorial)

/cc: @ibacher @dkayiwa @mozzy @kdaud
