Skip to content

Common interface for Patches #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
xelamitchell opened this issue Feb 4, 2015 · 5 comments
Open

Common interface for Patches #26

xelamitchell opened this issue Feb 4, 2015 · 5 comments

Comments

@xelamitchell
Copy link

In my API I wish to support both "application/json-patch" and "application/merge-patch" for deeply related and large entities. An example in a Spring Controller:

 @RequestMapping(method = RequestMethod.PATCH, value = "/configurations/{id}", consumes = "application/merge-patch+json")
 public Configuration merge(@PathVariable Long id, @RequestBody ObjectNode request) {
   // Creates a JsonMergePatch from the ObjectNode and calls:
    return apply(id, mergePatch);
}

@RequestMapping(method = RequestMethod.PATCH, value = "/configurations/{id}", consumes = "application/json-patch+json")
public Configuration patch(@PathVariable Long id, @RequestBody ArrayNode request) {
    // Creates a JsonPatch from the ArrayNode and calls:
    return apply(id, jsonPatch);
}

private Configuration apply(Long id, Patch patch) {
    // Before Apply: Retrieves configuration from database using id, transforms it into a JsonNode
    JsonNode patchedNode = patch.apply(configurationNode);
    // After Apply: Transforms the patched node into a Configuration and saves to database.
    return patchedConfiguration;
}

In this case, the actions before and after patch application are the same independent of which patch I am applying and the apply method shouldn't really care which patch it is applying as long as it is a valid patch for JSON.

@daveclayton
Copy link
Contributor

Hi @xelamitchell can you please clarify what you require in terms of changes to the json-patch libraries?

Thanks

@xelamitchell
Copy link
Author

Hey there @daveclayton. I was just looking for JsonPatch and JsonMergePatch classes to implement an interface of the type:

public interface Patch {

    JsonNode apply(JsonNode node) throws JsonPatchException;

}

This way, independent of the operation type, a Provider can give me the appropriate Patch object (JsonPatch or JsonMergePatch) and the patch operation itself can be used transparently when validation of message happens in one level (Controller/Provider) and actual update (call to method apply(JsonNode)) happens in another (Service).

Many thanks

@daveclayton
Copy link
Contributor

Hi

If I understand correctly, this is about pulling the apply method into an interface so that we can code to the behaviour instead of to the implementation (e.g. for IoC or Strategy, etc). Is that right?

There has been another question posted about splitting out interfaces and common objects into an API, so perhaps this should be tagged to that discussion so that we get the best solution overall.

What do you think?

@xelamitchell
Copy link
Author

Yes, isolating the apply method in an interface for dependency injection is the intent.

It certainly looks to me as though such an interface would be an important aspect for an API.

@zozoit
Copy link

zozoit commented Jun 4, 2019

Hello,

Stumbled on the same issue than xelamitchell. A simple interface would be really useful to avoid duplication of code when one needs to provide both patch services.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants