question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ObjectReader support for RFC 7386 Patch Merge

See original GitHub issue

Problem

I want to support JSON Patch Merge on a JSON REST API when using the PUT HTTP Method with a Content-Type of application/merge-patch+json.

The current usage of ObjectMapper.readerForUpdating does perform a merge, but it is not compliant with RFC 7386.

Some control can be had by using the @JsonMerge field annotation, but I believe this simply allows you to control if an array is appended or overwritten with the incoming value used with the ObjectMapper.readerForUpdating reader.

I would be happy to submit a PR (or try) -or assist in any way that I can if this is something you are willing to consider in the library.

Ideal Solution

In my usage, I will know the HTTP Method and the Content Type of the request. Using this information I would like to create a Reader that will use the merge patch rules as defined by RFC 7368.

Example usage to build a reader that is configured for Patch Merge.

// Existing object
User user = userService.retrieveUserById(42);

// New Object Mapper and reader configuration for Merge Patch
ObjectMapper objectMapper = new ObjectMapper();
ObjectReader reader = objectMapper.readerForUpdating(user)
                                  // New method or configuration to turn on merge strategy
                                  .withMergeStrategy(MergeStrategy.RFC_7368);

// Merge in JSON patch 
User merged = reader.readValue(request.getInputStream());

Alternatives

  • Third party libraries (listed below)
  • Perhaps there is a way to accomplish some basic merge / patch with the right combination of Deserialization and Serialization features on the Object Mapper.

Example Code

Using the pseudo code provided in the RFC and tests, I have stubbed out a test that passes. This is not optimal code, but maps the provided pseudo code into some (poor) semblance of Jackson code.

Test: https://github.com/prime-framework/prime-mvc/blob/f4b583fd169608d6bf8f7b0912eae60b0e71acfd/src/test/java/org/primeframework/mvc/PatchMergeTest.java#L109

Business logic for recursive patch merge: https://github.com/prime-framework/prime-mvc/blob/f4b583fd169608d6bf8f7b0912eae60b0e71acfd/src/test/java/org/primeframework/mvc/PatchMergeTest.java#L136

Additional Context

RFC 7396

Other Java libraries that have implemented this specification in part or in full.

Related Issues

https://github.com/FusionAuth/fusionauth-issues/issues/441

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Oct 3, 2022

Thank you for update @robotdan!

1reaction
robotdancommented, Oct 3, 2022

I ended up using https://github.com/java-json-tools/json-patch which has worked pretty well. This library supports both RFC 6902 and 7368.

Our usage is pretty straight forward, we just bind a handler based upon the Content-Type request header, and then handle patch or merge patch. https://github.com/prime-framework/prime-mvc/blob/efb88fd50807c86c0d9a2da33bbc476a26ad304a/src/main/java/org/primeframework/mvc/content/json/JacksonPatchContentHandler.java#L72

@fge or @Capstan any interest in porting your library into a patch here?

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON Merge Patch RFC 7386 - IETF Datatracker
JSON Merge Patch (RFC 7386, October 2014; obsoleted by rfc7396 obsoletes rfc7386)
Read more >
Patching JAX-RS - Craftsmen
For example support for JSON merge patch, RFC 7386, or the XML equivalent of JSON patch, RFC 5261, could easily be supported as...
Read more >
TMF 632 Patch with JSON Merge Patch | Open APIs
According to RFC 7386 - JSON Merge Patch, JSON merge patch cannot be used to make changes to an element within array, rather...
Read more >
How to handle partial updates to an Entity in Spring Boot
This is an implementation of RFC 6902 (JSON Patch) and RFC 7386 (JSON Merge Patch) written in Java, which uses Jackson (2.2.x) at...
Read more >
Patching JAX-RS - Model Zoo
This is the Transparent PATCH Support in JAX-RS 2.0 article written by ... For example support for JSON merge patch, RFC 7386, or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found