sopherapps / pydantic-redis

A simple Declarative ORM for Redis using pydantic Models
https://sopherapps.github.io/pydantic-redis
MIT License
39 stars 14 forks source link

Hotfix/json decode error on optional str fields #27

Closed Tinitto closed 6 months ago

Tinitto commented 6 months ago

Why

Models with optional string fields were raising orjson.JSONDecodeError errors.

orjson.JSONDecodeError: unexpected character: line 1 column 1 (char 0)

Cause

The parser was attempting to parse non-JSON string like 'hi' instead of strings like '"hi"' or '{"foo": "bar"}'. This would occur when the optional string fields actually had strings in them.

Remedy

Filter out any fields that are of type str or Optional[str] or str | None and avoid JSON-parsing their values in case their values are strings.

Thanks to @nmarusov for discovering this issue and doing the initial fixes.