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

Omit undefined within Channels' params to fix invalid JSON serialization #31

Merged
merged 2 commits into from
Jan 9, 2024

Conversation

ardecvz
Copy link
Contributor

@ardecvz ardecvz commented Jan 4, 2024

  • Omit undefined within Channels' params to fix invalid JSON serialization
    It's intended to mimic the ActionCable behavior when using JSON.stringify.

Projects currently migrating from the standard ActionCable client can use optional arguments when creating Channels:

// A simplified example
import { createConsumer } from '@anycable/web';

function createChannelForOnlineUsersOnPage(path, id) {
  createConsumer.subscriptions.create(
    {
      channel: 'OnlineUsersChannel',
      path: path,
      id: id,
    },
    ...
  )
}

createChannelForOnlineUsersOnPage('index')
createChannelForOnlineUsersOnPage('show', 1)

Without the change in PR, JSON is serialized incorrectly:

{"command":"subscribe","identifier":"{\"channel\":\"OnlineUsersChannel\",\"id\":undefined,\"path\":\"/index\"}}

As a result, this leads to a failure during JSON deserialization in AnyCable Go:

Command response: error_msg:"unexpected token at '{\"channel\":\"OnlineUsersChannel\",\"id\":undefined,\"path\":\"/index\"'

  • Also, extend the type signature to allow null values.
    It's a more controversial change as type checking will no longer catch undefined and null values, but it seems that there's no limitation on the ActionCable side.
    Therefore, users can rely on passing explicit null within params (technically, it's possible to pass Objects and Arrays in JSON as well, but I'm uncertain whether allowing these two is practical too).

…ation

It's intended to mimic the ActionCable behavior when using `JSON.stringify`.

Also, extend the type signature to allow `null` values.
@palkan
Copy link
Member

palkan commented Jan 9, 2024

Thanks!

@palkan palkan merged commit 665b188 into anycable:master Jan 9, 2024
2 checks passed
@palkan
Copy link
Member

palkan commented Jan 9, 2024

Released in @anycable/core 0.7.12

@ardecvz ardecvz deleted the feat/omit-undefined-in-params branch January 9, 2024 14:21
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.

2 participants