Search code examples
javajax-rswildflyjson-patch

Is there a way to disable automatic JSON PATCH application in Wildfly


when using HTTP-PATCH Resources in JAX-RS on Wildfly (tested with 14 and 18), some automagic happens that calls the same resource path using GET, applies the changes from the JSON-PATCH and calls the actual method with the result.

$ curl localhost:8080/so/auto/Bob -H 'Content-Type: application/json-patch+json' -X PATCH -d '[{"op":"replace","path":"/age","value":24}]' -v
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> PATCH /so/auto/Bob HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Type: application/json-patch+json
> Content-Length: 43
>
* upload completely sent off: 43 out of 43 bytes
< HTTP/1.1 200 OK
< Connection: keep-alive
< Transfer-Encoding: chunked
< Content-Type: application/json
< Date: Tue, 26 Nov 2019 12:45:51 GMT
<
* Connection #0 to host localhost left intact
{"age":24,"name":"Bob"}

Is there a way to disable this behaviour (to make #patchManual work)?

$ curl localhost:8080/so/manual/Bob -H 'Content-Type: application/json-patch+json' -X PATCH -d '[{"op":"replace","path":"/age","value":24}]' -v
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> PATCH /so/manual/Bob HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Type: application/json-patch+json
> Content-Length: 43
>
* upload completely sent off: 43 out of 43 bytes
< HTTP/1.1 400 Bad Request
< Connection: keep-alive
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 41
< Date: Tue, 26 Nov 2019 12:42:58 GMT
<
* Connection #0 to host localhost left intact
not a json patch: {"name":"Bob","age":24}

Code to reproduce the issue:

package so;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonPatch;
import javax.json.JsonStructure;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PATCH;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

@Path( "/" )
public class MyPatchResource
{
    public static class Data
    {
        public String name;
        public int    age;
    }

    @GET
    @Produces( MediaType.APPLICATION_JSON )
    @Path( "auto/{name}" )
    public Data read( @PathParam( "name" ) String name )
    {
        Data result = new Data();
        result.name = name;
        result.age = 42;
        return result;
    }

    @PATCH
    @Path( "auto/{name}" )
    @Consumes( MediaType.APPLICATION_JSON_PATCH_JSON )
    @Produces( MediaType.APPLICATION_JSON )
    public Data patchAuto( Data patched )
    {
        return patched;
    }

    @PATCH
    @Path( "manual/{name}" )
    @Consumes( MediaType.APPLICATION_JSON_PATCH_JSON )
    @Produces( MediaType.APPLICATION_JSON )
    public JsonObject patchManual( @PathParam( "name" ) String name, JsonStructure patch )
    {
        if ( !(patch instanceof JsonArray) )
        {
            throw new WebApplicationException( Response.status( Status.BAD_REQUEST )
                    .type( MediaType.TEXT_PLAIN )
                    .entity( "not a json patch: " + patch )
                    .build() );
        }

        JsonPatch jsonPatch = Json.createPatch( patch.asJsonArray() );
        JsonObject original = readManual( name );

        return jsonPatch.apply( original );
    }

    @GET
    @Path( "manual/{name}" )
    @Produces( MediaType.APPLICATION_JSON )
    public JsonObject readManual( @PathParam( "name" ) String name )
    {
        return Json.createObjectBuilder()
                .add( "name", name )
                .add( "age", 42 )
                .build();
    }
}


Solution

  • So I kind of found a solution...

    1) Define a custom http method

    @Target( { ElementType.METHOD } )
    @Retention( RetentionPolicy.RUNTIME )
    @HttpMethod( "JSONPATCH" )
    @Documented
    public @interface JSONPATCH
    {}
    

    2) Use the method in the business resource

    -    @PATCH
    +    @JSONPATCH
        @Path( "manual/{name}" )
        @Consumes( MediaType.APPLICATION_JSON_PATCH_JSON )
        @Produces( MediaType.APPLICATION_JSON )
        public JsonObject patchManual( @PathParam( "name" ) String name, JsonStructure patch )
    

    3) create a prematching ContainerRequestFilter that moves PATCH requests to JSONPATCH

    @Provider
    @PreMatching
    public class JsonPatchEnableFilter implements ContainerRequestFilter
    {
        @Override
        public void filter( ContainerRequestContext ctx ) throws IOException
        {
            if ( ctx.getMethod().equals( HttpMethod.PATCH )
                    && MediaType.APPLICATION_JSON_PATCH_JSON_TYPE.isCompatible( ctx.getMediaType() ) )
            {
                ctx.setMethod( "JSONPATCH" );
            }
        }
    }
    

    this disables the original behaviour completely by bypassing the filter, which could probably be tuned by adding additional logic to the filter.