Skip to content

fix(types): keep plain JSON text in RichBlockPreformatted - #2647

Open
beduldul wants to merge 1 commit into
eternnoir:masterfrom
beduldul:fix/richtext-plain-json-text
Open

beduldul wants to merge 1 commit into
eternnoir:masterfrom
beduldul:fix/richtext-plain-json-text

Conversation

@beduldul

Copy link
Copy Markdown
Contributor

Description

RichText.de_json treated any string that parses to a JSON object as a serialized RichText. A pre block holding a code sample like {"foo": "bar"} has no type key, so the dispatch fell through to None and the block lost its text:

>>> types.RichBlockPreformatted.de_json({'text': '{"foo": "bar"}', 'language': 'json'}).text
None

Sending that message back then fails with RICH_MESSAGE_CONTENT_REQUIRED (#2646).

Only a dict that resolves to a known RichText counts as serialized now; anything else keeps the original string. The type dispatch moved into _from_dict so the string branch can fall back to it. The [56] case from #2635 still works, and so do {}, {"a": [1, 2]} and {"type": "not_a_richtext"}.

Describe your tests

Added test_json_richtext_plain_json_object_stays_text in tests/test_types.py. It covers the preformatted block from the issue plus the other JSON-shaped strings, and asserts a real serialized RichText still deserializes.

Full suite: 308 passed, 73 skipped.

Python version: 3.12.3

OS: macOS 27.2

Checklist:

  • I added/edited example on new feature/change (if exists)
  • My changes won't break backward compatibility
  • I made changes both for sync and async

RichText.de_json treated any string that parses to a JSON object as a
serialized RichText. A pre block holding a code sample like {"foo": "bar"}
has no "type" key, so the lookup fell through to None and the block lost its
text. Sending that message back failed with RICH_MESSAGE_CONTENT_REQUIRED.

Only a dict that resolves to a known RichText counts as serialized now, and
anything else keeps the original string. The type dispatch moved into
_from_dict so the string branch can fall back to it.

Fixes eternnoir#2646.
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.

1 participant