Repository navigation
fix(rich-text): serialize a text link as an object - #105
Merged
Merged
Conversation
Link did not extend AbstractJsonSerializable, so its protected properties
were invisible to json_encode and a linked rich text serialized as
"link": []. The API rejects that outright on create:
body.children[0].paragraph.rich_text[0].text.link should be an object,
`null`, or `undefined`, instead was `[]`
Callers could work around it by setting the rich text's href instead, but
the API silently ignores href on create — it returns 200, drops the link
and merges the run into its neighbours, so the text arrives unlinked.
Extending the base class makes the link serialize as { "url": ... },
which is the only shape that produces a real hyperlink.
The nullsafe operator is 8.0+, and the library supports ^7.4 || ^8.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Linkdid not extendAbstractJsonSerializable, so itsprotectedproperties were invisible tojson_encode. Every other property resource in the library extends that base class;Linkwas the exception.The consequence is that a rich text carrying a link serialized as
"link": []instead of"link": {"url": "..."}.This one-line change makes
LinkextendAbstractJsonSerializable, which is all that is needed for the existingtoArray()machinery to serialize it. Reading is unaffected:Link::fromRawData()already worked, and a round-trip test covers it.Motivation and context
The empty array is rejected by the Notion API, so creating a block with a linked rich text fails outright:
Setting the rich text's
hrefinstead looks like a viable workaround, but it is not: the API acceptshrefon create, returns200, and then silently discards it. The text arrives unlinked and merged into its adjacent runs, so callers get a success response and no link. Serializingtext.linkas an object is the only shape that produces a hyperlink.How has this been tested?
tests/Resource/LinkTest.phpcovering the serialized shape, the omitted emptytype, a linked rich text insidetoArrayForCreate(), an unlinked rich text (nolinkkey), and afromRawData()round-trip.phpcsandphpstanclean.text.link.urlreturns200and the created block comes back with the rich-text runs kept separate andhrefpopulated on the linked run. The same payload built withhrefalone comes back withhref: nulland the runs merged.Types of changes
PR checklist