Subject: Re: [apps-discuss] Post-WG-LC changes in draft-ietf-appsawg-json-merge-patch

From: "Manger, James" <[email-masked]>
Date: Tue, 1 Jul 2014 17:34:29 +1000

Yes, that look right, Carsten.
Even not knowing Ruby (which I guess this code is), the code is simple enough to understand and see it is doing the right thing -- which is a compliment to Ruby and merge-patch.

[assuming that in Ruby nil, "undefined", and a JSON null are sort of the same]

--
James Manger

-----Original Message-----
From: Carsten Bormann [mailto:[email-masked]] 
Sent: Tuesday, 1 July 2014 4:32 PM
To: Manger, James
Cc: Paul Hoffman; IETF Apps Discuss
Subject: Re: [apps-discuss] Post-WG-LC changes in draft-ietf-appsawg-json-merge-patch

Good points.

Here is my implementation:

def merge_patch(orig, patch)
  if Hash === patch
    orig = {} unless Hash === orig
    patch.each do |k, v|
      if v.nil?
        orig.delete(k)
      else
        orig[k] = merge_patch(orig[k], v)
      end
    end
    orig
  else
    patch
  end
end                             # yep, that's 15 lines.

This could serve to replace the Appendix B.
This implementation does not have the bug that you mention:

> 3.
> The merge behaviour in draft-03 is now deliberately undefined if the patch is not an object or array. However, it is also undefined if the target is not an object or array. This undefined behaviour can only do harm. Many implementations will involve a merge(target, patch) function that accepts any JSON for its arguments so we may as well define all the results. It is also strange to mention arrays explicitly as merge-patch treats arrays just like a primitive.

Therefore, it does not check against the examples:

Mismatch: 
orig:     {"a":"foo"}
patch:    null
draft:    "Invalid Example"
actual:   null
Mismatch: 
orig:     {"a":"foo"}
patch:    "bar"
draft:    "Invalid Example"
actual:   “bar"

It does check against your example:

           {}              {"a":{"bb":     {"a":{"bb":{}}}
                           {"ccc":null}}}

I believe the above implementation is exactly what merge-patch should do, and the text should be updated accordingly.

Grüße, Carsten