Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid allowing null String for state_key. #4895

Merged
merged 3 commits into from
Jan 13, 2022
Merged

Conversation

bmarty
Copy link
Member

@bmarty bmarty commented Jan 10, 2022

Should always be an empty String according to the Matrix specification.

There is no functional change, just a change in the SDK API for clarity regarding the Matrix specs.

…tring according to the Matrix specification.

There is no functional change, just a change in the SDK API for clarity regarding the Matrix specs.
@bmarty bmarty requested a review from giomfo January 10, 2022 16:50
@github-actions
Copy link

github-actions bot commented Jan 10, 2022

Unit Test Results

  66 files  ±0    66 suites  ±0   54s ⏱️ -5s
135 tests ±0  135 ✔️ ±0  0 💤 ±0  0 ±0 
418 runs  ±0  418 ✔️ ±0  0 💤 ±0  0 ±0 

Results for commit 7581a0b. ± Comparison against base commit 56ff5f7.

♻️ This comment has been updated with latest results.

@bmarty bmarty requested a review from ganfra January 11, 2022 08:48
@@ -39,7 +39,7 @@ internal class DefaultSendStateTask @Inject constructor(

override suspend fun execute(params: SendStateTask.Params) {
return executeRequest(globalErrorReceiver) {
if (params.stateKey == null) {
if (params.stateKey.isEmpty()) {
Copy link
Member Author

@bmarty bmarty Jan 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is an improvement, I am not sure what would happen if stateKey was an empty String on the build URL (it would end by /).

@@ -39,7 +39,7 @@ internal class DefaultSendStateTask @Inject constructor(

override suspend fun execute(params: SendStateTask.Params) {
return executeRequest(globalErrorReceiver) {
if (params.stateKey == null) {
if (params.stateKey.isEmpty()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we even need to test this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably if
/_matrix/client/r0/rooms/{roomId}/state/{eventType}/ is not equivalent to
/_matrix/client/r0/rooms/{roomId}/state/{eventType}

*/
suspend fun sendStateEvent(eventType: String, stateKey: String?, body: JsonDict)
suspend fun sendStateEvent(eventType: String, stateKey: String, body: JsonDict)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there other endpoints which use empty strings to represent the absence of a value?

thinking out loud, from a public api perspective I would prefer to pass a null than an empty string as it gives us some extra type safety (and then let the internal implementation convert to an empty string)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want the SDK API to follow as much as possible the Matrix specification/philosophy. In the spec, the stateKey cannot be null.
Ref: https://matrix.org/docs/spec/client_server/r0.6.1#put-matrix-client-r0-rooms-roomid-state-eventtype-statekey

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra type safety

I do not understand why?

Copy link
Contributor

@ouchadam ouchadam Jan 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a string can contain content or be empty, whereas a null can only be null

the spec is a little bit misleading here when it mentions empty strings because the value of "" is an implementation detail of building a url path, we never send "" to the api.

it actually means we should omit the stateKey segment, which means the stateKey required is actually a lie...

has a state key
/_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}/

doesn't have a state key
/_matrix/client/r0/rooms/{roomId}/state/{eventType}/

assuming I've understood correctly! not a blocker for the PR, something I found interesting.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In API paths we do not send "" (or we alsways do it, since its nothing!), but I am wondering if
/_matrix/client/r0/rooms/{roomId}/state/{eventType}/ is equivalent to
/_matrix/client/r0/rooms/{roomId}/state/{eventType} (note the missing trailing /).
Anyway I think the spec make a diff mainly for the Json of the Event content itself:
"state_key": "" is not equivalent to "state_key": null (or missing "state_key").
that's maybe why it is a bit confusing all along.

@giomfo giomfo self-assigned this Jan 12, 2022
Copy link
Member

@giomfo giomfo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - thx

@bmarty bmarty merged commit 1b24b9d into develop Jan 13, 2022
@bmarty bmarty deleted the feature/bma/empty_state_key branch January 13, 2022 11:00
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.

4 participants