1

I'm trying out JsonPatch but ran into an issue when I try to test it. My REST controller looks like this:

@RestController
@RequestMapping("/v1/arbetsorder")
class ArbetsorderController {

  @PatchMapping(path ="/tider/{tid-id}", consumes = "application/json-patch+json")
  public ResponseEntity<Persontid> patchTid(@PathVariable("tid-id") Long id, @RequestBody JsonPatch patch) {
    Persontid modifieradPersontid = personTidService.patchPersontid(id, patch);
    return new ResponseEntity<>(modifieradPersontid, HttpStatus.OK);
  }

}

I'm testing this endpoint using this method:

@Test
void patchPersontid() {
  String body = "{\"op\":\"replace\",\"path\":\"/resursID\",\"value\":\"Jonas\"}";

  given()
    .auth().basic(user, pwd)
    .port(port)
    .header(CONTENT_TYPE, "application/json-patch+json")
    .body(body)
  .when()
    .patch("/v1/arbetsorder/tider/3")
  .then()
    .assertThat()
    .statusCode(HttpStatus.OK.value())
    .body("resursID", equalTo("Jonas"));
}

But this test fails because the status code is 500 instead of 200. I'm not reaching my endpoint.

I have tried using regular JSON and then everything works fine.

Can anyone help me understand why I'm not reaching my endpoint when using JsonPatch?

1 Answers1

2

Apparently the body I send to the endpoint has to be an array in order for it to be converted to a JsonPatch object. Changing my body to:

String body = "[{\"op\":\"replace\",\"path\":\"/resursID\",\"value\":\"Jonas\"}]";

solved my problem!