How to use the jsonpatch.make_patch function in jsonpatch

To help you get started, we’ve selected a few jsonpatch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stefankoegl / python-json-patch / tests.py View on Github external
def test_arrays(self):
        src = {'numbers': [1, 2, 3], 'other': [1, 3, 4, 5]}
        dst = {'numbers': [1, 3, 4, 5], 'other': [1, 3, 4]}
        patch = jsonpatch.make_patch(src, dst)
        res = patch.apply(src)
        self.assertEqual(res, dst)
github stefankoegl / python-json-patch / tests.py View on Github external
def fn(_src, _dst):
            patch = list(jsonpatch.make_patch(_src, _dst))
            # Check if there are only 'move' operations
            for p in patch:
                self.assertEqual(p['op'], 'move')
            res = jsonpatch.apply_patch(_src, patch)
            self.assertEqual(res, _dst)
github stefankoegl / python-json-patch / tests.py View on Github external
def test_success_if_replace_inside_dict(self):
        src = [{'a': 1, 'foo': {'b': 2, 'd': 5}}]
        dst = [{'a': 1, 'foo': {'b': 3, 'd': 6}}]
        patch = jsonpatch.make_patch(src, dst)
        self.assertEqual(patch.apply(src), dst)
github openstack / openstacksdk / openstack / resource.py View on Github external
def _prepare_request_body(self, patch, prepend_key):
        if patch:
            if not self._store_unknown_attrs_as_properties:
                # Default case
                new = self._body.attributes
                original_body = self._original_body
            else:
                new = self._unpack_properties_to_resource_root(
                    self._body.attributes)
                original_body = self._unpack_properties_to_resource_root(
                    self._original_body)

            # NOTE(gtema) sort result, since we might need validate it in tests
            body = sorted(
                list(jsonpatch.make_patch(
                    original_body,
                    new).patch),
                key=operator.itemgetter('path')
            )
        else:
            if not self._store_unknown_attrs_as_properties:
                # Default case
                body = self._body.dirty
            else:
                body = self._unpack_properties_to_resource_root(
                    self._body.dirty)

            if prepend_key and self.resource_key is not None:
                body = {self.resource_key: body}
        return body
github f5devcentral / f5-cccl / f5_cccl / resource / resource.py View on Github external
try:
            # This actually backs out the previous updates
            # to get back to the original F5 resource state.
            if prev_updates:
                self._data = prev_updates.apply(self._data)
        except Exception as e:  # pylint: disable=broad-except
            LOGGER.warning("Failed removing updates to resource %s: %s",
                           self.name, e)

        # 3. perform new merge with latest CCCL specific config
        original_resource = copy.deepcopy(self)
        self._data = merge(self._data, desired_data)
        self.post_merge_adjustments()

        # 4. compute the new updates so we can back out next go-around
        cur_updates = jsonpatch.make_patch(self._data, original_resource.data)

        # 5. remove move / adjust indexes per resource specific
        pospatch.convert_from_positional_patch(self._data, cur_updates)

        changed = self._data != prev_data

        # 6. update metadata with new CCCL updates
        self._save_whitelist_updates(cur_updates)

        # 7. determine if there was a needed change
        return changed
github EUDAT-B2SAFE / B2SAFE-core / cmd / b2shareclientworker.py View on Github external
# replace: patch_source[option_name] = draft_metadata[option_name]
                    # add: 
                    patch_source[option_name] = ''
            option_value = option_value_line[1]
            if self.isArray(option_value): 
                option_value = self.patchArrayFrom(option_value)
            elif self.isObject(option_value): 
                option_value = self.toJsonObject(option_value)
            elif self.isBoolean(option_value):
                option_value = self.valueToBoolean(option_value)   
            else:
                # value is a string
                option_value = option_value
            patch_destination[option_name] = option_value
        
        patch = jsonpatch.make_patch(patch_source, patch_destination)
        headers = {"Content-Type": "application/json-patch+json"}
        
        if self.configuration.dryrun:
            self.configuration.logger.info("DRYRUN: the method would send: PATCH Request with URL: " + patch_url \
                                                            + "\n headers: "+ str(headers) \
                                                            + "\n data: " + str(patch))
        else:
            try:
                #TODO: delete 'verify=False', if B2SHARE server has proper certificate
                response = requests.patch(url=patch_url, headers=headers, data=str(patch), verify=False)
                if str(response.status_code) == "200":
                    self.configuration.logger.info("Adding metadata to the draft " + record_id + " SUCCESSFUL.")
                else:
                    self.configuration.logger.error("Adding metadata to the draft " + record_id + " FAILED. " + \
                                                "\n response: " + response.text)
            except requests.exceptions.RequestException as e:
github opencord / voltha / voltha / core / config / config_node.py View on Github external
def diff(self, hash1, hash2=None, txid=None):
        branch = self._branches[txid]
        rev1 = branch[hash1]
        rev2 = branch[hash2] if hash2 else branch._latest
        if rev1.hash == rev2.hash:
            return JsonPatch([])
        else:
            dict1 = message_to_dict(rev1.data)
            dict2 = message_to_dict(rev2.data)
            return make_patch(dict1, dict2)
github mitodl / micromasters / search / api.py View on Github external
index = get_default_alias(PRIVATE_ENROLLMENT_INDEX_TYPE)

    conn = get_conn()
    try:
        document = conn.get(index=index, id=enrollment.id)
    except NotFoundError:
        return True
    serialized_enrollment = serialize_program_enrolled_user(enrollment)
    del serialized_enrollment['_id']
    source = document['_source']

    if serialized_enrollment != source:
        # Convert OrderedDict to dict
        reserialized_enrollment = json.loads(json.dumps(serialized_enrollment))

        diff = make_patch(source, reserialized_enrollment).patch
        serialized_diff = json.dumps(diff, indent="    ")
        log.info("Difference found for enrollment %s: %s", enrollment, serialized_diff)
        return True
    return False
github openprocurement / openprocurement.api / src / openprocurement / api / utils / common.py View on Github external
def get_revision_changes(dst, src):
    return make_patch(dst, src).patch