IllegalArgumentException via REST API with Composition with back references and m-to-n relationship

Hi,

i’ve encountered a “bug” in the REST API with the following scenario:

Entity Model:

  • Customer
  • Item
  • Order

Customer – composition → Order

Customer – Many to Many association → Item

If i create a customer with the following JSON via the REST API v2:


{
  "id": "82233ab0-371b-ff76-1c7f-0f6cd19e0788",
  "name": "Customer that doesnt work",
  "orderedItems": [
  {
    "id": "82233ab0-371b-ff76-1c7f-0f6cd19e073d"
    }
  ],
  "orders": [
    {
      "id": "82233ab0-371b-ff76-1c7f-0f6cd19e0734",
      "orderDate": "2017-01-20 13:49:48.701",
      "customer": {
      	"id": "82233ab0-371b-ff76-1c7f-0f6cd19e0788"
      }
    }
  ]
}

the response is:


{
  "error": "Server error",
  "details": ""
}

In the server there is an exception java.lang.IllegalArgumentException: argument type mismatch. You can find the stacktrace in the attachement.

The problem occurs due to the situation, that within the orders JSON structure there is a back reference to the customer (which is not created yet). But instead of not using this reference, it wants to bind this data.
In this line of the EntityImportExport, where the orderedItems is stored as a loadedEntity: cuba/EntityImportExport.java at master · cuba-platform/cuba · GitHub
it put’s the Item into the map of loadedEntities under the key: “82233ab0-371b-ff76-1c7f-0f6cd19e0788” which is the ID of the customer, and not of the Item (which is “82233ab0-371b-ff76-1c7f-0f6cd19e073d”). Therefore in later stages it tries to set the item via setValue. Unfortunately it get’s back an instance of Item and not of Customer due to the fact that in the loadedEntities map the customer is no longer under the key “82233ab0-371b-ff76-1c7f-0f6cd19e0788” but the Item is.

It’s definitvly just a minor problem, because if the JSON doesn’t creates the back reference from the order to the customer, everything works fine:


{
  "id": "82233ab0-371b-ff76-1c7f-0f6cd19e0788",
  "name": "Customer that works correct",
  "orderedItems": [
  {
    "id": "82233ab0-371b-ff76-1c7f-0f6cd19e073d"
    }
  ],
  "orders": [
    {
      "id": "82233ab0-371b-ff76-1c7f-0f6cd19e0734",
      "orderDate": "2017-01-20 13:49:48.701"
    }
  ]
}

Unfortunately the Error Message is somewhat misleading. One solution would be to create a hint in the docs that back references for compositions are not allowed (which is only partly true, because if we remove the orderedItems element from the JSON, it works as well).

Attached you’ll find the example project.

Perhaps someone wants to look at it.

Bye
Mario

stacktrace.txt (26.8K)

cuba-problem-m-n-rest-binding.zip (44.7K)

Hi Mario,

Thank you for reporting a problem!

:ticket: See the following issue in our bug tracker:

https://youtrack.cuba-platform.com/issue/PL-8538