Skip to content

[REF-3014] Bug in serializing lists/dict of rx.Base #3438

Closed

Description

@picklelo

Noticed an issue with our serializers - using lists of rx.Base is removing the outer braces:

class Test(rx.Base):
    key: str
    value: str

comp = Test(key="1", value="text1")

print(rx.utils.serializers.serialize(comp)) # prints {"key": "1", "value": "text1"}
print(rx.utils.serializers.serialize([comp])) # prints ["key": "1", "value": "text1"]

The second one is not valid JSON.

From SyncLinear.com | REF-3014

Activity

  1. added
    bug
    on Jun 4, 2024
  2. changed the title [-]Bug in serializing lists of `rx.Base` [/-] [+][REF-3014] Bug in serializing lists of `rx.Base` [/+] on Jun 4, 2024
  3. modified the milestones: , on Jun 4, 2024
  4. guidocalvano commented on Jun 5, 2024

    @guidocalvano

    I suspect
    For example, "{var}" will be unwrapped to "var".

    in unwrap_vars,

    specifcally this:
    {(.*?)} # extract the value between curly braces (non-greedy)
    from the regex

    and if I remove format.unwrap_vars from the serialize_list callback I get this:
    {"key": "1", "value": "text1"}
    ["{"key": "1", "value": "text1"}"]

    so that is a smoking gun...

    removing this stops the curly braces from disappearing
    {(.*?)} # extract the value between curly braces (non-greedy)

  5. masenf commented on Jun 5, 2024

    @masenf
    Collaborator

    we need to unwrap vars inside lists and dicts when they are real JS values (like functions), but for things that serialize to dictionaries, the unwrapping is taking away the outer curly braces where it shouldn't.

  6. guidocalvano commented on Jun 5, 2024

    @guidocalvano

    yeah, so you need to know either the format of a var string so you can more precisely target it, with the regex.
    Or you need to somehow leverage the recursion that json.dumps creates, and maybe detect when you are serializing a var, so you can take away the curly braces at that point. I would prefer that second option, because it follows the semantics of what you are trying to serialize. Is there specific serialization logic for vars?

  7. masenf commented on Jun 5, 2024

    @masenf
    Collaborator

    vars are serialized up front using reflex.utils.serializers and related functions... the _var_name is already the serialized form

  8. masenf commented on Jun 5, 2024

    @masenf
    Collaborator

    The problem with touching anything in this formatting code is that it's going to have fallout for other parts of the framework that have been working fine. Any changes must be very carefully considered to limit the scope of the change to the bug in question without introducing other regressions.

    The entire Var system is begging for a refactor because it's too hard to special case serialization for different types of vars. I was just running into this myself while trying to get nested dict serialization to work.

  9. guidocalvano commented on Jun 5, 2024

    @guidocalvano

    Whenever I am in doubt whether it is time for a refactor it is usually means I should have refactored 2 weeks ago. Refactoring is often considered terrifying. The fear usually stems from the following reasoning: "Before I can refactor I must understand how this code works". But these days I take a different perspective: Refactoring is restructuring code that leads to understanding. I start by making small simplifications (that do not change behavior) and then work my way up, never making any big change. I also see refactoring as fundamentally different from solving bugs.

  10. guidocalvano commented on Jun 5, 2024

    @guidocalvano

    But... to work towards a solution: wouldn't removing the part of the regex while adding the following work?

    @serializer
    def serialize_var(var: rx.Var) -> str:
    
        # solve the problem here
        # You can make more fine grained serializers if you want
    
        return format_var(var)  # maybe not even format?
  11. guidocalvano commented on Jun 5, 2024

    @guidocalvano

    The serialization code in the reflex code looks pretty organized....

  12. guidocalvano commented on Jun 5, 2024

    @guidocalvano

    What should a var look like in the output?

  13. 6 remaining items

  14. modified the milestones: , on Jun 16, 2024
  15. modified the milestones: , on Jun 23, 2024
  16. modified the milestones: , , , on Jun 30, 2024
  17. adhami3310 commented on Aug 26, 2024

    @adhami3310
    Member

    The new immutable var system seems to handle this just fine.

  18. added this to the milestone on Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions