Open APIs

 View Only
  • 1.  once again - arrays vs maps

    Posted Jan 09, 2020 08:41
    Hello. This problem was posted here several times but I still didn't find any solution for it - how to patch an array element without ID?
    Subitems in resources are arrays, and array elements are very hard to address in patch operations. F.e., I can use element index when removing or adding an element, but if someone change array contents in background, this index will become invalid. Moreover, even if I add a unique key to every element in the array, to access it, I have to iterate over all elements, comparing keys. Is there any solution for modifying array elements or it is still in discussion?

    ------------------------------
    Sergey Gusev
    Sber
    ------------------------------


  • 2.  RE: once again - arrays vs maps

    TM Forum Member
    Posted Jan 10, 2020 06:27
    Sergey ,

    You may use the PATCH method to modify a resource. For your use case use JSON Patch (RFC6902) where you provide an array of objects, each representing one operation (add, remove, replace,...) to perform on the target resource. Each object includes a "path" member, which references part of the target resource to operate on, using JSON-Pointer (RFC6901). Array members may be referenced by an index value (e.g. /foo/0 is the first member of array foo).

    Finally to address the problem of concurrent access the server MUST support conditional requests (RFC7232) and the client MUST include an If-Match header with the value of the ETag header received in a previous GET request.

    PATCH /path/to/resource HTTP/1.1
    Content-Type: application/json-patch+json
    If-Match: "c753e249"

    [
         { "op": "replace", "path": "/foo/5", "value": 42 \}
    ]


    ------------------------------
    Vance Shipley
    SigScale
    ------------------------------