Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC6902 limitations when the document structure is not known in advance #170

Closed
davidhadas opened this issue Jan 22, 2023 · 2 comments
Closed

Comments

@davidhadas
Copy link

davidhadas commented Jan 22, 2023

How do I add an element to an array if I do not know if the array already exist or not?
For example,I may receive any of the following base documents:

  • doc1 := "{"foo": [ "x2" ] }"
  • doc2 := "{}"
  • doc3 := "{"foo": [ ]}"

I tried the following patch:
patch1 := "[{"op": "add", "path": "/foo/0", "value": "x1"}]"

  • apply(doc1, patch1) // result is {"foo":["x1","x2"]} - great!
  • apply(doc2, patch1) // error 'doc is missing path: "/foo/0": missing value' - oops
  • apply(doc3, patch1) // result is {"foo":["x1"]} - great!
    So in the case of doc 2, this patch will not work and will produce an error!

I tried the following patch:
patch2 := "[{"op": "add", "path": "/foo", "value": ["x1"]}]"

  • apply(doc1, patch2) // result is {"foo":["x1"]} - bad! I need to add not replace
  • apply(doc2, patch2) // result is {"foo":["x1"]} - great!
  • apply(doc3, patch2) // result is {"foo":["x1"]} - great!
    So in the case of doc 1, this patch will not work and the original document data is replaced

What can I do?
How can I create using one or more ops, an array in documents (where it does not exist), without destroying the array data in documents where it exists?

A similar issue exists with nested keys - if you do not know if a key is there in the document or not, there is no way to create it without replacing all values in it (in case it exists). These seem to be fundamental issues with the RFC.

@evan
If my reading of the RFC is correct, this is an RFC issue, not in your implementation.
Yet, it is a problem for any application that wishes to use JSON patch and be resilient to the document provided
If I am not wrong in my interpretation, the RFC needs to be extended to solve these problems.
Will you consider extending beyond the RFC?
(e.g. add {"op": "radd" ...} which behaves like "add" but offers nested addition of keys if does not exist (recursive addition).

@davidhadas
Copy link
Author

davidhadas commented Jan 23, 2023

Seems like v5 offers such extendibility by offering a way of deviating from the RFC

@davidhadas davidhadas changed the title empty array - not working as expected Limitations of the RFC6902 when approaching document with structure not known in advance Jan 23, 2023
@davidhadas davidhadas changed the title Limitations of the RFC6902 when approaching document with structure not known in advance Limitations of the RFC6902 when the document structure is not known in advance Jan 23, 2023
@davidhadas davidhadas changed the title Limitations of the RFC6902 when the document structure is not known in advance RFC6902 limitations when the document structure is not known in advance Jan 23, 2023
@evanphx
Copy link
Owner

evanphx commented Jan 12, 2024

Looks like you sorted it out!

@evanphx evanphx closed this as completed Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants