Skip to content

Support Optional[Raw] when decoding JSON and MessagePack - #1155

Open
DSeaStar wants to merge 1 commit into
msgspec:mainfrom
DSeaStar:fix-optional-raw
Open

Support Optional[Raw] when decoding JSON and MessagePack#1155
DSeaStar wants to merge 1 commit into
msgspec:mainfrom
DSeaStar:fix-optional-raw

Conversation

@DSeaStar

Copy link
Copy Markdown

Fixes #659.

msgspec.Raw was collected with a type mask of 0, so Optional[Raw] / Raw | None never recorded MS_TYPE_NONE. JSON and MessagePack then treated null as a Raw payload (b"null") instead of None, which is the reproduction in #659.

This follows the design from the issue: Raw is allowed in a union only with None. null decodes as None; any other value decodes as Raw. Unions such as Raw | int now raise TypeError, matching the existing custom-type restriction.

Changes:

  • Give Raw its own MS_TYPE_RAW flag and reject unsupported Raw unions at type-collection time.
  • Peek for JSON null / MessagePack nil when decoding Raw | None.
  • Accept Raw | None in convert.
  • Add JSON, MessagePack, and convert tests for the null and non-null cases.

Raw was stored with a type mask of 0, so Optional[Raw] never recorded
MS_TYPE_NONE and json/msgpack decode treated null as a Raw payload.
Give Raw its own type flag, allow it only in a union with None, and
decode null as None while leaving other values as Raw.
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.

Optional[Raw] results in an unexpected validation error at json.decode time

1 participant