toedter / spring-hateoas-jsonapi

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

Attribute "relationships" inside "included" section #73

Closed G4br131 closed 1 year ago

G4br131 commented 1 year ago

Hi, we have a json file (see the code below) where the entity "orders" has a relationship (to one) with an entity "customers" and this one has a relationship (to many) with the entity "addresses". We want to parse the json file into the java bean entities whose code is below. The "relationships" attribute of "customers" is inside the "customers" entity which is placed in the "included" section. We can't achieve the "addresses" collection to auto-populate. Looking at the "JsonApiEntityModelDeserializer" class in the "createRelationship" method, this one does not iterate over the JsonApiDocument to find the "relationships" attribute that belongs to the var "typeArgument" passed and so don't included in the var "entry" to create an EntityModel. Also, when the "createRelationship" method call to "convertoToResource" always passes the var "wrapInEntityModel" to false.

Are we trying to do something that is not provided for in the original functionality of the library, or do we need to do something else to map the relationship "addresses" automatically?

I hope I have explained.

Thanks for your time.

json file

{
  "jsonapi": {
    "version": "1.1"
  },
  "data": [
    {
      "id": "1",
      "type": "orders",
      "attributes": {
        "name": "Order1"
      },
      "relationships": {
     "customer": {
            "data": {
            "id": "1",
            "type": "customers"
     }
       }
  ],
   "included": [
    {
      "id": "1",
      "type": "customers",
      "attributes": {
        "name": "Customer1"
      },
      "relationships": {
        "addresses": {
          "data": [
            {
              "id": "1",
              "type": "addresses"
            }
          ]
        }
      }
    },
    {
      "id": "1",
      "type": "addresses",
      "attributes": {
        "name": "Address1"
      }
    }
],
  "links": {
    ...
  },
  "meta": {
    "page": {
      "size": 1,
      "totalElements": 1,
      "totalPages": 1,
      "number": 0
    }
  }
}

java beans entities


public class Orders extends EntityModel<Orders>{
    private Long id;
    private String name;

    @JsonIgnore
    @JsonApiRelationships("customer")
    private Customers customer;
    ...Getters and setters...
}

public class Customers extends EntityModel<Customers>{
    private Long id;
    private String name;

    @JsonIgnore
    @JsonApiRelationships("addresses")
    private List<Addresses> addresses;
    ...Getters and setters...
}

public class Addresses extends EntityModel<Addresses>{
    private Long id;
    private String name;
    ...Getters and setters...
}

rest call ResponseEntity<PagedModel<EntityModel<Orders>>> response = restTemplate.exchange(...)

toedter commented 1 year ago

sorry, but this goes beyond the planned capabilities. Currently, only one level of indirection (when a data resource has a relationship) will be resolved. I am not sure if I will implement this. Give me a bit of time to evaluate...

toedter commented 1 year ago

I think I could fix this, please check release 2.0.3.