Avoid Object of type DAEmpty is not JSON serializable error#902
Open
BryceStevenWilley wants to merge 1 commit intojhpyle:masterfrom
Open
Avoid Object of type DAEmpty is not JSON serializable error#902BryceStevenWilley wants to merge 1 commit intojhpyle:masterfrom
Object of type DAEmpty is not JSON serializable error#902BryceStevenWilley wants to merge 1 commit intojhpyle:masterfrom
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 beforeto_json()whensafe_jsonis converting objects to JSON serializeable types.However, when calling
getattr()on aDAEmpty, it will return anotherDAEmptyobject. SinceDAEmptys are also callable objects that return anotherDAEmpty, callingas_dict()first will still leave aDAEmptyin 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:hasattr(the_object, "to_json")...check before theas_dictone. 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.as_dictare 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.as_dictfunction to DAEmpty. This would work, butas_dictisn't used elsewhere in docassemble code much.