toedter / spring-hateoas-jsonapi

A JSON:API media type implementation for Spring HATEOAS
Apache License 2.0
106 stars 15 forks source link

Read JSON:API content type in ResponseEntity #80

Closed Rezar00 closed 11 months ago

Rezar00 commented 11 months ago

Hi, I was faced with an issue. Below is my endpoint:

    @GetMapping(value = "/{id}", produces = JSON_API_VALUE)
    public ResponseEntity<RepresentationModel<?>> getOrder(@RequestHeader(value = "X-CUSTOMER-ID") Actor actor,
                                                           @PathVariable String id) {

        OrderResponse order = orderService.getOrder(id, actor);
        JsonApiModelBuilder orderJsonApiBuilder = jsonApiModel().model(order);
        if (order.getStatus().equals(OrderStatus.COMPLETED)) {
            List<DocumentServiceResponse> orderedDocuments = this.orderService.getDocumentByOrderId(id);
            orderedDocuments.forEach(document -> document.setLocation(documentServiceEndpoint + "/" + document.getId() + "/body"));
            orderJsonApiBuilder.included(orderedDocuments);
        }

        return ResponseEntity.ok(orderJsonApiBuilder.build());
    }

And Below is my test case:

var orderStatusResponse = dummyOrderResponseWithStatus(OrderStatus.COMPLETED); when(orderService.getOrder(ORDER_ID, ACTOR)).thenReturn(orderStatusResponse); when(orderService.getDocumentByOrderId(ORDER_ID)).thenReturn(List.of(dummyDocumentServiceResponse())); ResultActions accept = mockMvc.perform(MockMvcRequestBuilders.get("/orders/{id}", ORDER_ID) .accept(JSON_API) .header(CUSTOMER_ID_HEADER, ACTOR)); accept .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.id").value(orderStatusResponse.getId())) .andExpect(MockMvcResultMatchers.jsonPath("$.included").value(OrderStatus.COMPLETED.toString()));

I cannot read $.included. It is not present in json path

Rezar00 commented 11 months ago

@toedter

toedter commented 11 months ago

thx for reporting this, I will take a look at it soon.

toedter commented 11 months ago

I thing your test case is incorrect, 'included' is never at the same json path level as 'id', it is always on the level of 'data'. Did you manually check the actual response?

Rezar00 commented 11 months ago

I tried "$.data" but there is no JSON PATH.