From 7cef451183710e6488ef9c9df194f430eb892c10 Mon Sep 17 00:00:00 2001 From: ausimian Date: Sun, 14 Dec 2025 14:26:27 +1100 Subject: [PATCH] Fix fields accumulating across multiple typedrecord declarations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed a bug where module attributes (@ts_fields, @ts_types, etc.) persisted across multiple typedrecord declarations in the same module, causing fields from earlier records to incorrectly appear in later ones. The fix adds cleanup code to delete accumulating attributes after each typedrecord definition, ensuring each record starts with a clean slate. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- lib/typed_struct.ex | 5 +++++ test/typed_record_test.exs | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/typed_struct.ex b/lib/typed_struct.ex index bdb2619..97ed4ad 100644 --- a/lib/typed_struct.ex +++ b/lib/typed_struct.ex @@ -178,6 +178,11 @@ defmodule TypedStruct do unquote(name), unquote(tag) ) + + # Clean up accumulating attributes after each record definition + Enum.each(unquote(@record_accumulating_attrs), fn attr -> + Module.delete_attribute(__MODULE__, attr) + end) end end diff --git a/test/typed_record_test.exs b/test/typed_record_test.exs index 282792a..f117f1b 100644 --- a/test/typed_record_test.exs +++ b/test/typed_record_test.exs @@ -110,4 +110,23 @@ defmodule TypedRecordTest do end end end + + defmodule MultipleRecords do + use TypedStruct + + typedrecord :first do + field :a, integer(), default: 42 + end + + typedrecord :second do + field :b, String.t(), default: "hello" + end + end + + test "multiple typedrecords in the same module" do + import MultipleRecords, only: [first: 0, second: 0] + + assert first() == {:first, 42} + assert second() == {:second, "hello"} + end end