FHIR Patch and $diff in HAPI FHIR

FHIR Patch changes parts of a resource without resending it, and $diff shows what changed between versions. This page explains the operation types, FHIRPath targeting and how to use diffs for audit and review.

On this page

When FHIR Patch and $diff in HAPI FHIR fit your integration

FHIR Patch and $diff fit when your team must change one field on a large FHIR resource without resending the whole record, or must show exactly what changed between two versions of a resource. Both are built into the HAPI FHIR JPA server, so the decision is less about whether the server can do the work and more about whether your clients, audit process and review workflow are designed to use partial updates and diffs. If your integrations only ever send complete resources, the work here is a design change in those clients, not a server build.

The HAPI FHIR JPA server fully implements FHIR Patch, with add, insert, delete, replace and move operations, each addressed by a FHIRPath expression that identifies the target element. It also accepts JSON Patch (RFC 6902) and XML Patch, so teams already producing those formats can keep them. The $diff operation returns a FHIR Patch differential between two versions of one resource, or between two resources of the same type, and includes a previousValue part for each replaced value. That previousValue detail is what makes a diff useful as audit evidence, because it records both the old and new content of a change rather than only the new state.

Before you rely on these operations, confirm three things with your own test data: which FHIRPath expressions your clients will generate, how your access controls treat a patch compared with a full update, and whether your audit trail stores the diff or only the resulting version. The HAPI FHIR FHIR Patch documentation and the diff operation documentation describe the exact request and response shapes, and those should be checked against the HAPI FHIR version you run. Plan the verification work now, because it determines how much of the integration can safely move to partial updates.

Choosing between FHIR Patch formats and $diff

This table compares FHIR Patch, JSON Patch, XML Patch and the $diff operation by the job each one does: partial client updates, payloads from non-FHIR tooling, and change comparison for review. Read each row to see what the client builds and what the server team must confirm before go-live.

OptionBest fitWhat the client must send or buildWhat the server team must confirm
FHIR Patch (Parameters)Partial updates from FHIR-aware clients that want FHIRPath targetingA Parameters resource with add, insert, delete, replace or move parts, each addressed by a FHIRPath expressionSupported operation types are enabled for the resource types in use, and authorization rules apply to each patched element
JSON Patch (RFC 6902)Partial updates from general JSON tooling or existing web client librariesA JSON Patch document with add, remove, replace, move, copy and test operations against the resourceClients send the correct JSON Pointer paths, and validation still runs after the patch is applied
XML PatchTeams whose integration stack already produces XML and cannot easily emit JSONAn XML Patch document that targets the resource in XML formXML content type handling is enabled on the endpoint and the team tests the patch against the same resource shape in use
$diff operationReviewing what changed between two stored versions or two resources of the same typeA request naming two versions or two resources, then reading the returned FHIR Patch differentialVersion history is retained long enough for review, and reviewers can read previousValue parts for replaced content
Full PUT updateSmall resources or clients that already hold the complete current resourceThe full resource with the change applied, so the server replaces the whole recordConcurrent edits are handled, since a whole-resource write can overwrite changes made by another client
Audit trail use of $diffEvidence that a clinical or administrative change was made and what it changedStored diffs exported with the request identity, timestamp and reason for the changeThe operator's audit logging captures who sent each patch, and diff output is retained under the same controls as the data

How FHIRPath targeting selects the elements a patch changes

A FHIR Patch operation changes only what its FHIRPath expression selects, so the path is the most important part of each operation to get right. Each parameter in a FHIR Patch request names an operation type (add, insert, delete, replace or move) and a path that points at a location in the target resource, often with a name that identifies the element to act on. The HAPI FHIR JPA server evaluates that expression against the stored resource and applies the change to every match it finds. An expression that selects more nodes than intended changes more data than intended, and the server will not stop you because the request is syntactically valid. For this reason, the review step for any patch is to read the expression and predict what it selects before the request is sent.

The most common targeting risk is an expression that is correct for one resource but matches several elements on another. Many FHIR elements repeat, such as Patient.name, Patient.telecom or Observation.component, and a path written without a filter can select every instance. A replace or delete aimed at a single telecom entry may also remove or overwrite others if the filter depends on a field that is absent or duplicated in some records. Build filters on stable, unique content such as system and use for contact points, and test each expression against a representative set of sample resources that includes records with missing fields, duplicates and unusual values. Confirm the exact FHIRPath syntax your server version accepts against the HAPI FHIR FHIR Patch documentation before relying on it.

Index-based operations need separate care. Insert and move operations address positions in a list, so the result depends on the current order of the repeating element. If a resource is updated by another process between the time a patch is prepared and the time it is applied, an index that pointed at the correct entry can point at a different one. Where possible, express the target by content rather than by position, and treat any index as a value to verify immediately before the patch is sent. The same concern applies to delete and replace when they select by index, and a sequence of several operations in one patch can change the positions that later operations rely on, so order and test the operations as a single unit.

(Illustrative example) A patch that inserts a new Patient.name entry at index 0 places it first in the list, which can change which name a downstream system treats as the official one. Before production, run each patch against a copy of the data in a non-production HAPI FHIR server, read the resulting resource, and compare it to the expected output. Keep the test resources, expected results and the patch documents under version control so that a change to an expression is reviewed the same way as any other configuration change.

How to set up and test partial updates on a HAPI FHIR server

This sequence takes a team from confirming the server version to proving that FHIR Patch and $diff behave correctly on non-production data, with a clear audit trail for each change.

  1. Confirm server version and Java runtime

    The platform team checks which HAPI FHIR version the JPA server runs and confirms the JDK it uses. HAPI FHIR 8.2.0 requires JDK 17, so a server on an older runtime needs its Java version or its HAPI FHIR version changed before patch testing starts. Confirm the version against the HAPI FHIR versions documentation and your own build file, then record both in the test plan. The step is done when the running server reports the expected version and the runtime matches the requirement for that release.

  2. Identify resources that need partial updates

    The clinical informatics lead and a developer list the resource types and fields that change often without needing a full replacement, such as Patient contact details, Task status or Observation notes. For each one, note who sends the change, whether the sender holds a complete copy of the resource, and which fields must never change through a patch. Write these down as an inventory. The step is done when every resource type in scope has a named owner, a list of allowed paths and a decision on whether the sender uses FHIR Patch, JSON Patch or XML Patch.

  3. Write and test FHIRPath expressions on sample data

    A developer loads synthetic or de-identified resources into a non-production database and writes the FHIRPath expressions that target each field in the inventory. Each expression is tested for add, insert, delete, replace and move operations, including cases where the path matches zero elements or several. Confirm that the expression selects only what was intended. The step is done when every expression returns the expected element on test resources and the team has recorded the results, including the edge cases that failed and how they were fixed.

  4. Run patches against a staging server

    The developer sends FHIR Patch requests to the staging server, first with a single operation and then with a multi-operation Parameters body, and checks the returned resource. The tester also sends the same change as JSON Patch or XML Patch where the calling system uses those formats, to confirm the server applies them as expected. Failed patches are checked for clear error responses and confirm that no partial change was saved. The step is done when each test case has a recorded request, response and resulting resource.

  5. Compare versions with $diff in staging

    The tester runs the $diff operation between two versions of a resource and between two resources of the same type, then reads the returned FHIR Patch differential. Check that added, removed and replaced values appear correctly and that replaced values include the previousValue part. Compare the diff against the change you intended to make. The step is done when the diff output matches the expected change for every test case, and the team has agreed how diffs will be stored for review.

  6. Confirm version history and audit records

    The tester reads the resource history after each patch to confirm that a new version was created with the expected content and metadata. The security or compliance lead then checks the audit logs configured for the server, such as BALP audit logging if it is enabled, to confirm that each patch request is recorded with the actor and resource. Confirm that the audit records and the version history can be linked. The step is done when every test patch has a matching version entry and an audit record, and any gaps are logged as open items before production work begins.

  7. Document the approved patch pattern

    The team writes a short runbook covering the approved FHIRPath expressions, the request format for each calling system, how $diff is used in review, and the checks that must pass before a patch goes to production. The runbook names the owner for each resource type and states which version of HAPI FHIR and JDK it was tested on. The step is done when the runbook is reviewed by the platform lead and the security lead, and the test cases are saved so they can be rerun after any server upgrade.

Security and audit controls around patches and diffs

HAPI FHIR does not ship a single security layer for partial updates. Authorization, consent, audit and response handling are separate building blocks that you configure, and each one has to cover patch requests and diff output as well as ordinary reads and writes.

Who may patch which resources

A PATCH is a write, but it can change one field on a resource a user could otherwise only read. Decide which roles may send each patch type (FHIR Patch, JSON Patch, XML Patch) and against which resource types and compartments. Check that the authorization rules treat a PATCH as an update, and test a user who can read a Patient but not update it, confirming the request is refused.

HAPI FHIR security docs

Authorization interceptor rules for patch

The AuthorizationInterceptor evaluates each request against rules you define, and patch requests need their own rule set. Write rules that name the operation and resource type, then check the outcome with a test account for each role. Confirm that a rule permitting a create or read does not implicitly permit a patch on the same resource.

Consent and search narrowing on patches

Consent interceptors and search narrowing limit what a user can see, but they do not automatically limit what a user can change. Confirm how your consent logic applies to a patch that targets a resource excluded from the user's consent scope. Test that a patch to a record the user cannot read is refused, and that the server does not reveal the record's existence through a different status or message.

BALP audit records for changes

BALP audit logging can record who accessed or changed FHIR data, which makes it the main evidence source for patch activity. Confirm that patch and update operations generate audit events, that the events identify the user and the resource, and that they reach a store your audit team can query. Ask your auditors which fields they need before you rely on the default output.

Diff output carries clinical values

A $diff response includes previousValue parts and the new values for changed elements, so it can expose clinical content such as diagnoses, medications or observation results. Apply the same access rules to $diff as to the resource itself, and do not let a user with read access to a diff see fields they could not read directly. Review diff logs and exports as protected health information.

Ownership of the control design

Because HAPI FHIR provides building blocks, your team owns the combined design: role mapping, interceptor order, audit destinations, log retention and access to stored diffs. Write this design down, assign an owner for each control, and test it after every upgrade, since interceptor behavior can change between releases. Ask DeployOpen or your own security team to review the design before go-live.

How a patch request and a diff comparison move through the server

The flow starts with a client sending a FHIR Patch, JSON Patch or XML Patch request through a reverse proxy, where it meets the authorization and audit interceptors inside the HAPI FHIR JPA server. The server applies the operations and writes a new version of the resource to the database. A later $diff call reads two stored versions and returns a Parameters resource holding the differential. Check that every request is authorized before it reaches the server logic, and that the audit log records both the patch and the diff calls.

Patch writes a new version, and $diff reads stored versions and returns the differential.

Limits to confirm in your environment before relying on patches

FHIR Patch and $diff look simple in examples, but their behavior depends on the HAPI FHIR version, the profiles on your resources, data volume and the other modules running on the server. The cards below cover the checks to run against your own version and test data before a patch workflow reaches production.

Operation support by version

Confirm which FHIR Patch operations (add, insert, delete, replace and move) and which patch formats (FHIR Patch, JSON Patch and XML Patch) your exact HAPI FHIR version supports, and how each handles edge cases such as repeating elements and extensions. Support described in current documentation may not match an older release you run, so test every operation type on your own version and record the results. Ask the vendor documentation owner or your upgrade plan to confirm any gap.

FHIR Patch docs

Patches against validated profiles

A patch is applied to the stored resource and the result must still conform to the profiles the server enforces, such as US Core or a local profile. Test whether a patch that produces an invalid resource is rejected, whether the server reports the failure clearly, and whether required elements removed by a delete operation are caught. Include both valid and deliberately invalid patches in your test data set so the behavior is documented rather than assumed.

Diff cost on large resources

The $diff operation compares two versions of a resource and returns a patch that includes previousValue parts for replaced values. Large resources, such as bundles, resources with long narrative text or resources with many repeating elements, can produce large outputs and slower responses. Measure response time and payload size with resources at the sizes your organization actually stores, and set limits on who can call the operation and how often.

Diff operation docs

Interaction with subscriptions

Each accepted patch creates a new resource version, which can trigger subscriptions that notify other systems. Confirm that the subscription channels you enable fire once per patch, how they behave when several patches arrive in quick succession, and whether downstream consumers can handle partial changes. Test this in a non-production environment with the same subscription configuration you plan to run.

Patches and MDM linked records

When a Patient or Practitioner is linked to a golden resource through MDM, a patch to one record may change identifiers or names that affect matching decisions. Confirm whether patches trigger MDM re-evaluation, how possible matches are queued, and whether the golden resource is updated consistently. Use duplicate test records that cover your matching rules and verify the outcome after each patch.

Audit trail of diffs

A $diff output can serve as review evidence only if your audit logging captures who made each change and which version it applied to. Confirm that your logging setup records the patch request, the resulting versions and the caller identity, and that retention meets your policy. Keep an independent record of the patch body, since the stored diff depends on the version history that your retention and purge settings keep.

Questions teams ask before using FHIR Patch or $diff

Can FHIR Patch replace a full resource update?

Often for specific fields, but not always for every change. FHIR Patch sends only the operations you need, such as adding, replacing, deleting or moving an element addressed by a FHIRPath expression, so a client does not have to fetch and resend the whole resource. That reduces the risk of overwriting fields another system changed in the meantime. A full update is still the simpler choice when a client already holds the complete resource and most fields change. Confirm in your own tests that each patch targets the elements you expect, because a FHIRPath expression that matches more nodes than intended will change more data than you planned.

What does a team need to build and test patches?

Your team needs working knowledge of FHIR resource structure, FHIRPath expressions and the JPA server's REST API, plus a test environment with realistic resource shapes. Build tests that cover each operation type you plan to use, each target path in your resource profiles, and the cases where a path matches zero or several elements. Include negative tests for invalid patches and for concurrent updates. Patch logic usually lives in client code or integration middleware, so it also needs version control, code review and a process for updating expressions when profiles change.

Can an internal team implement this without outside help?

Yes, if it already runs HAPI FHIR and has engineers comfortable with Java, FHIR and test automation. The HAPI FHIR documentation covers FHIR Patch, JSON Patch, XML Patch and $diff, so the main gaps are usually in your own resource profiles, edge cases and test coverage rather than the API itself. An outside partner is most useful when the team lacks FHIR depth, when several systems need consistent patch behavior, or when the work must be documented for a review. Decide early who owns patch rules after go-live.

What drives the duration of this work?

The main drivers are the number of resource types, how deeply nested and profile-constrained those resources are, and how much test coverage the team requires. A simple set of Observation or Patient updates is far less work than patch logic across many profiles with conflict handling and audit output. Other factors include whether patches come from several client applications, whether existing update flows must be replaced, and how many review cycles clinical and compliance teams need. Ask for a written scope that lists resource types, operations and test cases before agreeing on a timeline.

What do clinical and compliance reviewers need to provide?

Clinical reviewers need to confirm which fields may change through partial updates, which changes need a clinician to sign off, and which values must never be altered after entry. Compliance reviewers need to confirm how patch and diff records are retained, who can read them, and how they fit into the audit and access controls around the server. Both groups should review sample patches and diffs from a test environment before go-live. Plan for their review time in the schedule, because it often sets the pace more than engineering does.

Can $diff serve as an audit trail on its own?

No. The $diff operation shows what changed between two versions of a resource, including previous values for replaced elements, which makes it useful for review. It does not by itself record who made the change, from which system, for what reason, or whether the change was authorized. Pair diffs with the server's version history, authentication and authorization controls, and audit logging such as BALP. Confirm with your compliance team which records they must keep and for how long, and test that diff output is stored and retrievable in your environment.

What recurring work comes after go-live?

Recurring work includes updating patch expressions when resource profiles change, retesting after HAPI FHIR upgrades, and reviewing diff output as clinical content and workflows evolve. Teams also need to monitor patch errors and conflicts, and to keep test fixtures aligned with production resource shapes. Ask any partner whether ongoing support, upgrades and continuous testing are covered by a retainer or by a separate fixed-scope engagement, and confirm how changes to the written scope are handled.

Using $diff for audit and clinical review

The $diff output is a record of what changed between two versions, not a record of who changed it or why, so teams should treat it as one piece of evidence in change review, incident investigation and data correction approvals. In practice, a reviewer compares two versions of a resource (or two resources of the same type) and reads the FHIR Patch differential the HAPI FHIR $diff operation returns, including the previousValue parts for replaced values. That is useful for confirming that a correction touched only the intended elements, for scoping what an incident affected, and for giving an approver a short, reviewable summary before a fix is applied through FHIR Patch.

What $diff does not prove on its own matters as much as what it shows. The patch says nothing about the authenticated user, the client application, the reason for the change or whether the stored version is complete. A clean diff can hide a missing element that was never captured, and a diff between two versions only exists if both versions were retained. Confirm with your own environment which versions your server keeps and for how long, and whether any cleanup or expunge process could remove the history your reviewers depend on.

Pair each diff with the access trail and version history. BALP audit logging, which the HAPI FHIR security documentation lists among its building blocks, records who accessed which data and when, and the resource history supplies the full versions behind each diff. Your team must design the review workflow itself: who requests a diff, who approves a correction, how evidence is attached to a ticket and how reviewers are trained to read previousValue parts. HAPI FHIR provides the operations and logs, but the approval process, retention rules and sign-off roles are decisions the operator owns.

Phases for a patch and diff review workflow

The workflow starts when a change request arrives for a stored resource. The team applies the change as a FHIR Patch on the HAPI FHIR server, which keeps the update targeted to the elements named in the request. Next, a $diff is generated between the prior version and the new version, and that diff is routed to a reviewer. Only the approved diff is archived with the audit records, so reviewers can check what changed, who requested it and who approved it. Check that the diff names the changed elements and that the reviewer can trace each change back to the request.

Decision path from change request to archived, reviewed diff.

What DeployOpen scopes, delivers and supports for patch and diff work

DeployOpen scopes patch and diff work as a fixed-scope engagement, and the written scope is where most of the value is set. Before work starts, both parties agree which resource types are in scope (for example, Patient, Observation or Encounter, and whether any are excluded because they carry sensitive data), which patch formats the workflows must accept (FHIR Patch, JSON Patch or XML Patch), and what audit evidence the client expects from a diff. The scope also names the HAPI FHIR version and the database the work targets, since those determine which operations and settings are available to test. Deliverables, acceptance criteria and timelines are written down so that the client's security, clinical informatics and compliance teams can review the same document before any change reaches a server.

DeployOpen builds and tests the workflows inside the client's own HAPI FHIR environment, not in a separate hosted copy. The work covers the patch requests that clients and integrations send, the server-side handling of FHIRPath targets, and the use of $diff to produce reviewable change records for the audit trail. Testing uses representative data that the client approves, and the test cases include rejected patches, unauthorized callers and patches that touch fields the authorization model should protect. Each deliverable is documented so the client's own engineers can operate it. Confirm with DeployOpen whether the environment for testing is separated from production and who holds the credentials used during build.

Ongoing support is available as a retainer for teams that need changes after go-live. A retainer typically covers upgrades to newer HAPI FHIR releases, review of new resource types or patch formats added to the scope, and re-testing after changes to interceptors or authorization rules. Ask any provider to state in the quote which of these activities recur, which are one-time, and how changes to the written scope are priced and approved. DeployOpen does not issue audit reports or certifications for this work. The diff records and test evidence it produces are inputs for the client's internal review and for independent auditors, who decide what they accept.

How DeployOpen scopes and supports HAPI FHIR patch and diff work

DeployOpen approaches FHIR Patch and $diff in HAPI FHIR as part of a self-hosted HAPI FHIR JPA server running in infrastructure you control. Work starts with a written scope that names the resource types, patch formats and audit uses involved, then moves through build, testing, documentation and handover, with ongoing support available as a retainer.

How we scope

  • Inventory which resource types and update flows need partial updates, and which clients send them.
  • Agree in writing which patch formats are in scope: FHIR Patch, JSON Patch or XML Patch.
  • Define which $diff comparisons feed audit or review, and who reads the output.

How we build

  • Write and test FHIRPath targets for add, insert, delete, replace and move operations against your profiles.
  • Build client-side patch generation and $diff calls that match your server version and JDK requirements.
  • Test rejected patches, concurrent updates and version conflicts before any production traffic reaches the server.

How we hand over

  • Deliver documented patch request examples, targeting rules and known limits for your resource types.
  • Provide runbooks for reviewing $diff output, including how previousValue parts are read and retained.
  • Walk your engineers and reviewers through the build and record decisions in the written handover.

How we support

  • Offer ongoing support, version upgrades and continuous testing for patch and diff behavior as a retainer.
  • Retest patch flows after HAPI FHIR upgrades, since the server version sets which operations you can rely on.
  • Confirm with your team which audit retention and access controls apply to stored diff output.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.