helidon-io / helidon

Java libraries for writing microservices
https://helidon.io
Apache License 2.0
3.5k stars 567 forks source link

Add support for `application/json-patch+json` media type #8627

Open tvallin opened 5 months ago

tvallin commented 5 months ago

Environment Details


Problem Description

Add support for application/json-patch+json media type.

Steps to reproduce

n/a

romain-grecourt commented 5 months ago

This is do-able with JSON-P.

@Path("/patch")
public class JsonPatchResource {

    private static final JsonProvider JSON_PROVIDER = JsonProvider.provider();

    private JsonObject jsonObject = JsonValue.EMPTY_JSON_OBJECT;

    @GET
    public JsonObject get() {
        return jsonObject;
    }

    @PUT
    public void put(JsonObject jsonObject) {
        this.jsonObject = jsonObject;
    }

    @Consumes("application/json-patch+json")
    @POST
    public void patch(JsonArray operations) {
        JsonPatch patch = JSON_PROVIDER.createPatch(operations);
        this.jsonObject = patch.apply(jsonObject);
    }
}
# verify the empty initial state
curl http://localhost:8080/patch
{}

# set the object
curl -X PUT -H "Content-Type: application/json" http://localhost:8080/patch -d '{"foo": "bar"}'

# verify the object
curl http://localhost:8080/patch
{"foo":"bar"}

# patch the object
curl -X POST -H "Content-Type: application/json-patch+json" http://localhost:8080/patch -d '[ {"op":"replace","path":"/foo","value": "123"} ]'

# verify the result
curl http://localhost:8080/patch
{"foo":"123"}

We should double-check if this also works with Helidon SE. If this works, we should create examples for both.