/
Analysis of Replace a Node API

Analysis of Replace a Node API

Use Discussion notes to capture meeting items, assign owners, and track actions and decisions.

Date

Item

Owner

Notes

Relevant Links

Date

Item

Owner

Notes

Relevant Links

Aug 9, 2024

 

@someone

Action

 

Problem Statement

The Replace Node Leaves API can only perform a replace operation on one data node at a time and in order to perform this request the user needs to provide the following parameters as part of the request:

  • the parent node xpath of the node being replaced

  • the JSON/XML payload containing the new data which will replace the existing data.

Scenario 1: API accepts new list items

If a replace operation is performed on a list node, where an already existing list item is being replaced, then in this scenario a user induced error can take place where the user can send a new list item as part of the payload.

This results in a 200-response code being returned to the user but with a partial replace operation.

Because when the data is verified after the replace operation is executed, it is seen that the data of existing list item is replaced, which is the expected behavior. But the data of the new list item is not added under the list node.

But since the API returns a 200-response code, it could create a false sense of security for the user that the request executed successfully, where in fact only a partial operation took place in the database.

A more detailed explanation with examples is as follows:

1. In the following data, the node titled “Matilda",under the list named “books",is to be replaced.
{ "book-store:categories": { "code": "1", "name": "Kids", "books": [ { "title": "Matilda", "lang": "English", "authors": ["Roald Dahl"], "editions": [2024] } ] } }
2. In order to do so the correct parent node xpath and JSON payload would be as follows:

Correct parent node xpath: /bookstore/categories[@code=1]

Correct JSON payload:

{ "books": [ { "title": "Matilda", "lang": "French", "editions": [2025] } ] }
3. API accepts extra/new list elements as part of request

If the user modifies an existing list item and along with that adds a new list item in the payload then once the request is executed the user receives a 200-response code.

Correct parent node xpath: /bookstore/categories[@code=1]

Incorrect JSON payload with newly added list item with title: Book 2

{ "books": [ { "title": "Matilda", "lang": "French", "editions": [2025] }, { "title": "Book 2", "lang": "English", "authors": ["Test"], "editions": [2000] } ] }
4. The data queried after the above replace operation is executed is as follows:

It can be seen that only the existing list item, i.e. title: Matilda is replaced. Whereas the newly added list item with title: Book 2 is not added but user received a 200-response code.

Expected Behavior

  • Based on the RESTful principles a PUT operation should completely rewrite the data in the database with the new data provided in the payload.

  • So, the expected behavior is to replace all the data of the existing data node and if any new data node is provided as part of the request, then it should be added as a new entity. And the response code should be changed to 204-Created.

Based on the above observations the correct response after data is Replaced should be as follows

Proposed/Accepted Solutions

WIP

Related pages