Skip to content

Avoid Object of type DAEmpty is not JSON serializable error#902

Open
BryceStevenWilley wants to merge 1 commit intojhpyle:masterfrom
SuffolkLITLab:daempty_json_serialization_issue
Open

Avoid Object of type DAEmpty is not JSON serializable error#902
BryceStevenWilley wants to merge 1 commit intojhpyle:masterfrom
SuffolkLITLab:daempty_json_serialization_issue

Conversation

@BryceStevenWilley
Copy link
Contributor

Due to a slight change in 0f5b49a (not mentioned in the description, so I don't know what the intention was for changing it), as_dict() is checked for and called before to_json() when safe_json is converting objects to JSON serializeable types.

However, when calling getattr() on a DAEmpty, it will return another DAEmpty object. Since DAEmptys are also callable objects that return another DAEmpty, calling as_dict() first will still leave a DAEmpty in the output, which is not safely JSON serializeable, leaving the issues raised in #877 unfixed.

Adding a special case for DAEmpty to call to_json() avoids this issue. Other approaches that could fix the issue include:

  • Just moving the hasattr(the_object, "to_json")... check before the as_dict one. While this is the simplest solution, it adds an implicit assumption in the code that will be easily missed on future refactorings of this function.
  • Making sure that the results of calling as_dict are still JSON safe. This would cause an infinite recursion on DAEmptys, and either wouldn't fix error or would result in max level nested entries for DAEmptys.
  • Add a as_dict function to DAEmpty. This would work, but as_dict isn't used elsewhere in docassemble code much.

Due to a slight change in jhpyle@0f5b49a
(not mentioned in the description, so I don't know what the intention was for changing it),
`as_dict()` is checked for and called before `to_json()` when `safe_json` is
converting objects to JSON serializeable types.

However, when calling `getattr()` on a `DAEmpty`, it will return another `DAEmpty` object.
Since `DAEmpty`s are also callable objects that return another `DAEmpty`, calling `as_dict()`
first will still leave a `DAEmpty` in the output, which is not safely JSON serializeable,
leaving the issues raised in jhpyle#877 unfixed.

Adding a special case for DAEmpty to call `to_json()` avoids this issue.
Other approaches that could fix the issue include:

* Just moving the `hasattr(the_object, "to_json")...` check before the `as_dict` one.
  While this is the simplest solution, it adds an implicit assumption in the code that
  will be easily missed on future refactorings of this function.
* Making sure that the results of calling `as_dict` are still JSON safe.
  This would cause an infinite recursion on DAEmptys, and either wouldn't fix error
  or would result in max level nested entries for DAEmptys.
* Add a `as_dict` function to DAEmpty. This would work, but `as_dict` isn't used
  elsewhere in docassemble code much.
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

Successfully merging this pull request may close these issues.

1 participant