Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Generated `State` structs now derive `Jason.Encoder`, fixing `Protocol.UndefinedError` when encoding state over Phoenix channels or other JSON serialization paths

## [0.3.0] - 2026-02-26

### Fixed
Expand Down
1 change: 1 addition & 0 deletions lib/durable_object/dsl/transformers/build_introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ defmodule DurableObject.Dsl.Transformers.BuildIntrospection do
quote do
defmodule State do
@moduledoc false
@derive Jason.Encoder
defstruct unquote(Macro.escape(struct_fields))
end
end
Expand Down
20 changes: 20 additions & 0 deletions test/durable_object/dsl/transformers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,25 @@ defmodule DurableObject.Dsl.TransformersTest do
get_participants = Enum.find(handlers, &(&1.name == :get_participants))
assert get_participants.args == []
end

test "State struct is JSON-encodable via Jason" do
state = %BasicCounter.State{id: "test-123", count: 42}
assert {:ok, json} = Jason.encode(state)
assert %{"id" => "test-123", "count" => 42} = Jason.decode!(json)
end

test "State struct with complex fields is JSON-encodable" do
state = %ChatRoom.State{
id: "room-1",
messages: [%{user: "alice", text: "hello"}],
participants: ["alice", "bob"],
created_at: nil
}

assert {:ok, json} = Jason.encode(state)
decoded = Jason.decode!(json)
assert decoded["id"] == "room-1"
assert length(decoded["participants"]) == 2
end
end
end
36 changes: 36 additions & 0 deletions test/durable_object/server_persistence_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,42 @@ defmodule DurableObject.ServerPersistenceTest do
end
end

defmodule SerdeCounter do
use DurableObject

state do
field(:count, :integer, default: 0)
end

handlers do
handler(:increment)
handler(:get_state)
end

def handle_increment(state) do
{:reply, :ok, %{state | count: state.count + 1}}
end

def handle_get_state(state) do
{:reply, state, state}
end
end

describe "state serde through sqlite" do
test "state persists and rehydrates correctly" do
id = unique_id("serde")

{:ok, :ok} = SerdeCounter.increment(id, repo: TestRepo)
{:ok, :ok} = SerdeCounter.increment(id, repo: TestRepo)
{:ok, :ok} = SerdeCounter.increment(id, repo: TestRepo)
DurableObject.stop(SerdeCounter, id)

{:ok, state} = SerdeCounter.get_state(id, repo: TestRepo)
assert state.id == id
assert state.count == 3
end
end

describe "without :repo option" do
test "works without persistence" do
id = unique_id("no-repo")
Expand Down