iiif-prezi/iiif-prezi3

`.json` and `.jsonld` functions can pass invalid keywords to the underlying functions

Closed this issue · 0 comments

We use a list to filter the kwargs into those that need to go to Pydantic, and those that need to go to the json module:
https://github.com/iiif-prezi/iiif-prezi3/blob/main/iiif_prezi3/base.py#L55-L57

        pydantic_args = ["include", "exclude", "by_alias", "encoder"]
        dict_kwargs = dict([(arg, kwargs[arg]) for arg in kwargs.keys() if arg in pydantic_args])
        json_kwargs = dict([(arg, kwargs[arg]) for arg in kwargs.keys() if arg not in pydantic_args])

This was fine when we were passing through all valid Pydantic options, but now that we explicitly set exclude_unset, exclude_defaults etc, they have been removed from the list used for filtering.

This means that they now get included on the json_kwargs list instead and thus get passed through to the underlying json.dumps call, causing an error if they are supplied:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mike/projects/iiif-prezi3/iiif_prezi3/base.py", line 56, in jsonld
    dict_kwargs = dict([(arg, kwargs[arg]) for arg in kwargs.keys() if arg in pydantic_args])
  File "/usr/lib/python3.8/json/__init__.py", line 234, in dumps
    return cls(
TypeError: __init__() got an unexpected keyword argument 'exclude_unset'

This is easily solved by having an "excluded kwargs" list and ignoring anything on that list.