lux icon indicating copy to clipboard operation
lux copied to clipboard

Creating entity with incorrect related entity ID, still gets created.

Open Jinjeleta opened this issue 8 years ago • 5 comments

Creating new entity with related entity's ID, which does not exist, still gets created, however response comes with an error message. An example:

POST /answers 

{
  "data":{
    "type": "answers",
    "attributes": {
    	"body": "My new answer to your question!"
    },
    "relationships": {
    	"question": { "data": { "type": "questions", "id": 999}}
    }
  }
}

Response:
{
  "errors": [{
    "status": "404",
    "title": "Not Found",
    "detail": "Could not find Question with id 999."
  }],
    "jsonapi": {
      "version": "1.0"
    }
}

However doing another request to get all answers, still returns freshly created entity, however I think it should have rolled back.

GET /answers

{
    "data": [
        {
            "id": "1",
            "type": "answers",
            "attributes": {
                "body": "My new answer to your question!",
                "created-at": "2016-12-01T19:38:28.008Z",
                "updated-at": "2016-12-01T19:38:28.008Z"
            },
            "relationships": {
                "question": {
                    "data": null
                }
            },
            "links": {
                "self": "http://localhost:4000/answers/1"
            }
        }
    ],
    "links": {
        "self": "http://localhost:4000/answers",
        "first": "http://localhost:4000/answers",
        "last": "http://localhost:4000/answers",
        "prev": null,
        "next": null
    },
    "jsonapi": {
        "version": "1.0"
    }
}

We can see that from log as well:

------------------------------------------------------------------------------------------------------

[2016-12-01T19:38:28.094Z] INSERT INTO "answers" ("body", "created_at", "question_id", "updated_at") VALUES ('MY NEW ANSWER TO YOUR QUESTION!', '2016-12-01 20:38:28.008', NULL, '2016-12-01 20:38:28.008')

------------------------------------------------------------------------------------------------------

[2016-12-01T19:38:28.137Z] SELECT "questions"."id" AS "id", "questions"."title" AS "title", "questions"."body" AS "body", "questions"."created_at" AS "createdAt", "questions"."updated_at" AS "updatedAt" FROM "questions" WHERE "questions"."id" = 999 LIMIT 1

------------------------------------------------------------------------------------------------------

[2016-12-01T19:38:28.142Z] Error: Could not find Question with id 999.
    at /home/student/Projects/lux/examples/forum/node_modules/lux-framework/src/packages/database/query/runner/index.js:85:25
    at Generator.next (<anonymous>)
    at step (/home/student/Projects/lux/examples/forum/dist/bundle.js:228:30)
    at /home/student/Projects/lux/examples/forum/dist/bundle.js:239:13

------------------------------------------------------------------------------------------------------

[2016-12-01T19:38:28.162Z] Processed POST "/answers" from ::ffff:127.0.0.1 with 404 Not Found by AnswersController#create

Jinjeleta avatar Dec 01 '16 19:12 Jinjeleta

Probably this will get fixed with https://github.com/postlight/lux/pull/527 as well.

Jinjeleta avatar Dec 01 '16 19:12 Jinjeleta

Is the intent here to create the answer and the question in the same request or to create an answer for a question that already exists?

I can confirm that we have a bug with updating the belongs-to side of relationships in the current stable version 1.0.X. That will be fixed in #527.

zacharygolba avatar Dec 01 '16 19:12 zacharygolba

Actually, intention was to test whether this will be handled properly, when creating an answer to non-existing question. This scenario could happen in real cases as well, when client is slightly out of sync and tries to create answer to recently deleted question.

Jinjeleta avatar Dec 01 '16 20:12 Jinjeleta

Hmm that's a good point.

What would you expect to happen here? A 404 due to the related record not existing? Is that better than just ignoring the invalid resource?

I can't seem to find anything about this in the JSON API spec.

zacharygolba avatar Dec 01 '16 20:12 zacharygolba

In my opinion response is correct and fine.

We are just missing to ensure that behind the scenes entity will not get created with null or incorrect ID of related entity, when it gives a such response back.

Jinjeleta avatar Dec 01 '16 20:12 Jinjeleta