Add enhanced support for extra_data and JSON patching.

Review Request #12251 — Created April 22, 2022 and submitted

Information

RBTools
release-3.x

Reviewers

This brings some long-needed support for working with modern
extra_data functionality in Review Board.

Attempting to modify an extra_data dictionary now raises an exception.
This is part of the work done to make dictionary fields immutable, since
RBTools doesn't have a concept of simply saving a resource back to the
server (update() calls are required instead). The immutable error
provides handy instructions for setting new extra_data content.

The big change here, though, is the support for extra_data patching.
This was doable before through something like:

resource.update(**{'extra_data:json': json.dumps({...})})

That obviously was not ideal. Now, callers can simply set the
extra_data_json field to a native dictionary to do a JSON Merge Patch
or set extra_data_json_patch to a list of dictionaries to do a JSON
Patch. RBTools will properly serialize the results to JSON, setting the
correct key in the payload.

A .copy() method also exists to return a copy of the original field
data, without any wrapping. This works for any dictionary level of
extra_data, allowing consumers to create a copy, manipulate it, and
set it as a new full dictionary in a patch request.

Between new patching options, a more standard dictionary interface, the
ability to copy the payload, and useful immutable error handling, it
should now be a lot easier to work with extra_data fields.

Unit tests pass on all supported versions of Python.

Summary ID
Add enhanced support for extra_data and JSON patching.
This brings some long-needed support for working with modern `extra_data` functionality in Review Board. Attempting to modify an `extra_data` dictionary now raises an exception. This is part of the work done to make dictionary fields immutable, since RBTools doesn't have a concept of simply saving a resource back to the server (`update()` calls are required instead). The immutable error provides handy instructions for setting new ``extra_data`` content. The big change here, though, is the support for `extra_data` patching. This was doable before through something like: ```python resource.update(**{'extra_data:json': json.dumps({...})}) ``` That obviously was not ideal. Now, callers can simply set the `extra_data_json` field to a native dictionary to do a JSON Merge Patch or set `extra_data_json_patch` to a list of dictionaries to do a JSON Patch. RBTools will properly serialize the results to JSON, setting the correct key in the payload. A `.copy()` method also exists to return a copy of the original field data, without any wrapping. This works for any dictionary level of ``extra_data``, allowing consumers to create a copy, manipulate it, and set it as a new full dictionary in a patch request. Between new patching options, a more standard dictionary interface, the ability to copy the payload, and useful immutable error handling, it should now be a lot easier to work with ``extra_data`` fields.
d807f48779b9437105ffd50dac8c4fdffacf1b6d
Description From Last Updated

F841 local variable 'has_extra_data_json' is assigned to but never used

reviewbotreviewbot

F841 local variable 'has_extra_data_json_patch' is assigned to but never used

reviewbotreviewbot
Checks run (1 failed, 1 succeeded)
flake8 failed.
JSHint passed.

flake8

chipx86
chipx86
david
  1. Ship It!
  2. 
      
chipx86
Review request changed
Status:
Completed
Change Summary:
Pushed to release-3.x (af1a31b)