Skip to content

StateManager: a stored state that is JSON but not an object raises TypeError instead of StateDataError #368

Description

@allen0099

Found during the docs audit (#367).

StateManager._decode_state (fastapi_cachex/state/manager.py, return StateData(**state_dict)) catches ValueError only. When the stored state is valid JSON but not an object (for example [1, 2] or "x"), StateData(**state_dict) raises TypeError: argument after ** must be a mapping, which escapes:

Method Documented Actual
validate_state(state) False for malformed data TypeError
get_state_metadata(state) None for malformed data TypeError
consume_state(state) StateDataError TypeError

Repro: create a state on a MemoryBackend, overwrite its entry with CacheEntry(fingerprint, b"[1, 2]", media_type), then call the three methods.

Proposal

Check isinstance(state_dict, dict) after json.loads (or also catch TypeError) and raise StateDataError. Add a test for each method with a non-object JSON value.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingoauth-stateStateManager: one-time OAuth state tokens

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions