Skip to content

A platform for community discussion. Free, open, simple.

DEV: Preserve removed filters for older API versions (#43826)

Loïc Guitautf7196b2334e9
diff --git a/lib/json_api_kit/client.rb b/lib/json_api_kit/client.rb
index b3755a80b5c..5691ced8ed6 100644
--- a/lib/json_api_kit/client.rb
+++ b/lib/json_api_kit/client.rb
@@ -1,6 +1,5 @@
# frozen_string_literal: true
module JsonApiKit
- Client =
- Data.define(:guardian, :edition, :urls) { delegate :glossary, :default_sorts, to: :edition }
+ Client = Data.define(:guardian, :edition, :urls) { delegate :glossary, to: :edition }
end
diff --git a/lib/json_api_kit/declarations/filters.rb b/lib/json_api_kit/declarations/filters.rb
index fd3780bfc41..eb43a95fd67 100644
--- a/lib/json_api_kit/declarations/filters.rb
+++ b/lib/json_api_kit/declarations/filters.rb
@@ -5,10 +5,14 @@ module JsonApiKit
class Filters
delegate :fetch, to: :filters
- def initialize(filters)
- @filters = filters.index_by(&:name)
+ def initialize(declarations)
+ @declarations = declarations
end
+ def names = filters.keys
+
+ def with(additional_filters) = self.class.new(declarations.chain(additional_filters))
+
def apply(scope, filtering = {})
return scope if filtering.blank?
filtering.reduce(scope) { |narrowed, (name, value)| fetch(name).apply(narrowed, value) }
@@ -16,7 +20,9 @@ module JsonApiKit
private
- attr_reader :filters
+ attr_reader :declarations
+
+ def filters = @filters ||= declarations.index_by(&:name)
end
end
end
diff --git a/lib/json_api_kit/declarations/relationship.rb b/lib/json_api_kit/declarations/relationship.rb
index cb98c00e6e4..6a7189b236c 100644
--- a/lib/json_api_kit/declarations/relationship.rb
+++ b/lib/json_api_kit/declarations/relationship.rb
@@ -16,8 +16,8 @@ module JsonApiKit
verify_readable_block
end
- def listing(params, guardian:, scoped_to:, default_sorts:)
- resource.all(params, guardian:, scoped_to:, default_sorts:)
+ def listing(params, guardian:, scoped_to:, edition:)
+ resource.all(params, guardian:, scoped_to:, edition:)
end
def resolves?(path) = resource.resolves?(path)
diff --git a/lib/json_api_kit/default_sorts.rb b/lib/json_api_kit/default_sorts.rb
index a9d8b07dd62..e06306a0f98 100644
--- a/lib/json_api_kit/default_sorts.rb
+++ b/lib/json_api_kit/default_sorts.rb
@@ -7,7 +7,7 @@ module JsonApiKit
@orderings = {}
end
- def for(resource) = orderings[resource] ||= History.new(resource:, changes:).ordering
+ def for(resource) = orderings[resource.class] ||= History.new(resource:, changes:).ordering
private
diff --git a/lib/json_api_kit/document.rb b/lib/json_api_kit/document.rb
index 45c16a142d7..d28794184ae 100644
--- a/lib/json_api_kit/document.rb
+++ b/lib/json_api_kit/document.rb
@@ -6,9 +6,10 @@ module JsonApiKit
private
def build(raw, resource:, client:)
+ resource = resource.new(guardian: client.guardian, edition: client.edition)
input = input_class.new(raw, resource:, edition: client.edition)
return Errors.new(*input.refusals) if input.invalid?
- assemble(new(yield(input.to_h), client:, fieldsets: input.fieldsets))
+ assemble(new(yield(input.to_h, resource), client:, fieldsets: input.fieldsets))
rescue Error => error
Errors.new(error)
end
diff --git a/lib/json_api_kit/document/collection.rb b/lib/json_api_kit/document/collection.rb
index 3822b27089a..0833c3c4076 100644
--- a/lib/json_api_kit/document/collection.rb
+++ b/lib/json_api_kit/document/collection.rb
@@ -7,13 +7,8 @@ module JsonApiKit
def input_class = Request::Input::Collection
def for(parameters, resource:, client:, scoped_to: nil)
- build(parameters, resource:, client:) do
- resource.all(
- it,
- guardian: client.guardian,
- scoped_to:,
- default_sorts: client.default_sorts,
- )
+ build(parameters, resource:, client:) do |params, instance|
+ instance.all(params, scoped_to:)
end
end
end
diff --git a/lib/json_api_kit/document/individual.rb b/lib/json_api_kit/document/individual.rb
index dcca09bc2f4..a86859fb842 100644
--- a/lib/json_api_kit/document/individual.rb
+++ b/lib/json_api_kit/document/individual.rb
@@ -7,9 +7,7 @@ module JsonApiKit
def input_class = Request::Input::Individual
def for(id, parameters, resource:, client:)
- build(parameters, resource:, client:) do
- resource.find(id, it, guardian: client.guardian, default_sorts: client.default_sorts)
- end
+ build(parameters, resource:, client:) { |params, instance| instance.find(id, params) }
end
end
diff --git a/lib/json_api_kit/edition.rb b/lib/json_api_kit/edition.rb
index 21161fc3d53..6b38abe84d9 100644
--- a/lib/json_api_kit/edition.rb
+++ b/lib/json_api_kit/edition.rb
@@ -18,6 +18,8 @@ module JsonApiKit
def default_sorts = @default_sorts ||= DefaultSorts.new(changes)
+ def removed_filters = @removed_filters ||= RemovedFilters.new(changes)
+
private
attr_reader :changes
diff --git a/lib/json_api_kit/removed_filters.rb b/lib/json_api_kit/removed_filters.rb
new file mode 100644
index 00000000000..d5d5a9e87e6
--- /dev/null
+++ b/lib/json_api_kit/removed_filters.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+module JsonApiKit
+ class RemovedFilters
+ def initialize(changes)
+ @changes = changes
+ end
+
+ def for(type) = enum_for(:each_removed_filter, type)
+
+ private
+
+ attr_reader :changes
+
+ def each_removed_filter(type)
+ return if changes.empty?
+
+ changes
+ .reverse_each
+ .zip(types_at_changes(type, changes.reverse_each)) do |change, historical_type|
+ change.each_removed_filter(historical_type) { yield(it) }
+ end
+ end
+
+ def types_at_changes(type, changes)
+ Enumerator.produce(type) { changes.next.previous_resource_type(it) }
+ end
+ end
+end
diff --git a/lib/json_api_kit/request.rb b/lib/json_api_kit/request.rb
index 02d62d1d0e9..3b4a7289786 100644
--- a/lib/json_api_kit/request.rb
+++ b/lib/json_api_kit/request.rb
@@ -5,13 +5,13 @@ module JsonApiKit
LIST = ","
SORT_DIRECTIONS = { "-" => :desc, "" => :asc }.freeze
- attr_reader :guardian, :default_sorts
+ attr_reader :guardian, :edition
- def initialize(params = {}, guardian:, default_sorts:)
+ def initialize(params = {}, guardian:, edition:)
@params = ActiveSupport::HashWithIndifferentAccess.new(params)
@page_params = @params[:page].to_h.symbolize_keys
@guardian = guardian
- @default_sorts = default_sorts
+ @edition = edition
end
def ordering = params.fetch(:sort)
diff --git a/lib/json_api_kit/resource.rb b/lib/json_api_kit/resource.rb
index 21ef260078f..d81c27941f3 100644
--- a/lib/json_api_kit/resource.rb
+++ b/lib/json_api_kit/resource.rb
@@ -10,5 +10,12 @@ module JsonApiKit
include Fields
include Including
include QueryInterface
+
+ def initialize(guardian:, edition: Edition.current)
+ @guardian = guardian
+ @edition = edition
+ end
+
+ attr_reader :guardian, :edition
end
end
diff --git a/lib/json_api_kit/resource/anchoring.rb b/lib/json_api_kit/resource/anchoring.rb
index 0af61567190..e9f703cc9f8 100644
--- a/lib/json_api_kit/resource/anchoring.rb
+++ b/lib/json_api_kit/resource/anchoring.rb
@@ -34,6 +34,8 @@ module JsonApiKit
def declared_anchor(name) = declared_anchors.detect { it.name == name.to_s }
end
+
+ delegate :anchor_names, :anchor_accepts?, :anchored_by?, :anchors, to: :class
end
end
end
diff --git a/lib/json_api_kit/resource/fields.rb b/lib/json_api_kit/resource/fields.rb
index 529d2d3893b..5995963dff7 100644
--- a/lib/json_api_kit/resource/fields.rb
+++ b/lib/json_api_kit/resource/fields.rb
@@ -60,6 +60,8 @@ module JsonApiKit
]
end
end
+
+ delegate :attribute_names, :relationships, :fields, :resolves?, to: :class
end
end
end
diff --git a/lib/json_api_kit/resource/filtering.rb b/lib/json_api_kit/resource/filtering.rb
index 6a8d5b044a6..231c9cb3ee2 100644
--- a/lib/json_api_kit/resource/filtering.rb
+++ b/lib/json_api_kit/resource/filtering.rb
@@ -18,14 +18,16 @@ module JsonApiKit
self.declared_filters = declared_filters + [Declarations::Filter.new(name, &condition)]
end
- def apply_filters(rows, filtering = {}) = filters.apply(rows, filtering)
-
def filter_names = declared_filters.map(&:name)
- private
-
def filters = Declarations::Filters.new(declared_filters)
end
+
+ delegate :names, to: :filters, prefix: :filter
+
+ def apply_filters(rows, filtering = {}) = filters.apply(rows, filtering)
+
+ def filters = @filters ||= self.class.filters.with(edition.removed_filters.for(type))
end
end
end
diff --git a/lib/json_api_kit/resource/including.rb b/lib/json_api_kit/resource/including.rb
index 9cf88788f26..7c2e3c38008 100644
--- a/lib/json_api_kit/resource/including.rb
+++ b/lib/json_api_kit/resource/including.rb
@@ -27,6 +27,8 @@ module JsonApiKit
Declarations::IncludePaths.new(declared_include_paths, relationships:)
end
end
+
+ delegate :allow, :paths_include?, to: :class
end
end
end
diff --git a/lib/json_api_kit/resource/naming.rb b/lib/json_api_kit/resource/naming.rb
index 8a4bc9d5bcc..b5b1876b37d 100644
--- a/lib/json_api_kit/resource/naming.rb
+++ b/lib/json_api_kit/resource/naming.rb
@@ -65,6 +65,8 @@ module JsonApiKit
"#{inspect} is not named after what it exposes: declare `model SomeModel` and `type :things`"
end
end
+
+ delegate :model, :schema, :type, :namespace, to: :class
end
end
end
diff --git a/lib/json_api_kit/resource/paging.rb b/lib/json_api_kit/resource/paging.rb
index b07de6e8f83..a65b21a297e 100644
--- a/lib/json_api_kit/resource/paging.rb
+++ b/lib/json_api_kit/resource/paging.rb
@@ -27,6 +27,8 @@ module JsonApiKit
def paged_from?(cursor, ordering:) = order(ordering).compatible_with?(cursor:)
end
+
+ delegate :page_limits, :page_size, :max_page_size, :paged_from?, to: :class
end
end
end
diff --git a/lib/json_api_kit/resource/query_interface.rb b/lib/json_api_kit/resource/query_interface.rb
index 92f74e81f45..b67e7af01c5 100644
--- a/lib/json_api_kit/resource/query_interface.rb
+++ b/lib/json_api_kit/resource/query_interface.rb
@@ -11,8 +11,6 @@ module JsonApiKit
end
class_methods do
- delegate :default_sorts, to: "JsonApiKit::Edition.current", prefix: :current, private: true
-
def scope(&block)
self.declared_scope = block
end
@@ -22,29 +20,40 @@ module JsonApiKit
declared_scope.call(guardian)
end
- def all(params = {}, guardian:, scoped_to: nil, default_sorts: current_default_sorts)
- Query::Collection.new(
- self,
- Request::Collection.new(
- Request::Input.with_defaults(params, resource: self, default_sorts:),
- guardian:,
- default_sorts:,
- ),
- scoped_to:,
- )
+ def all(params = {}, guardian:, scoped_to: nil, edition: Edition.current)
+ new(guardian:, edition:).all(params, scoped_to:)
end
- def find(id, params = {}, guardian:, default_sorts: current_default_sorts)
- Query::Individual.new(
- self,
- Request::Individual.new(
- Request::Input.with_defaults(params, resource: self, default_sorts:).merge(id:),
- guardian:,
- default_sorts:,
- ),
- )
+ def find(id, params = {}, guardian:, edition: Edition.current)
+ new(guardian:, edition:).find(id, params)
end
end
+
+ delegate :scope_for, to: :class
+ delegate :default_sorts, to: :edition, private: true
+
+ def all(params = {}, scoped_to: nil)
+ Query::Collection.new(
+ self,
+ Request::Collection.new(
+ Request::Input.with_defaults(params, resource: self, default_sorts:),
+ guardian:,
+ edition:,
+ ),
+ scoped_to:,
+ )
+ end
+
+ def find(id, params = {})
+ Query::Individual.new(
+ self,
+ Request::Individual.new(
+ Request::Input.with_defaults(params, resource: self, default_sorts:).merge(id:),
+ guardian:,
+ edition:,
+ ),
+ )
+ end
end
end
end
diff --git a/lib/json_api_kit/resource/sorting.rb b/lib/json_api_kit/resource/sorting.rb
index 64e85dc531e..920277bf915 100644
--- a/lib/json_api_kit/resource/sorting.rb
+++ b/lib/json_api_kit/resource/sorting.rb
@@ -66,6 +66,8 @@ module JsonApiKit
"declare it with `sort` before naming it as the default"
end
end
+
+ delegate :sort_names, :order, :sortable_by?, :default_ordering, to: :class
end
end
end
diff --git a/lib/json_api_kit/sideload.rb b/lib/json_api_kit/sideload.rb
index 1584cbb3406..412dfd62cf7 100644
--- a/lib/json_api_kit/sideload.rb
+++ b/lib/json_api_kit/sideload.rb
@@ -25,7 +25,7 @@ module JsonApiKit
relationship.listing(
request.for_sideload(paths),
guardian: request.guardian,
- default_sorts: request.default_sorts,
+ edition: request.edition,
scoped_to: relationship.scoping(association),
)
end
diff --git a/lib/json_api_kit/version_change.rb b/lib/json_api_kit/version_change.rb
index eaf72a39440..607113adcec 100644
--- a/lib/json_api_kit/version_change.rb
+++ b/lib/json_api_kit/version_change.rb
@@ -22,6 +22,7 @@ module JsonApiKit
.then do
transformations.concat(it.transformations)
default_sorts.concat(it.default_sorts)
+ removed_filters.concat(it.removed_filters)
end
end
@@ -37,6 +38,8 @@ module JsonApiKit
def type_renames = @type_renames ||= []
def default_sorts = @default_sorts ||= []
+
+ def removed_filters = @removed_filters ||= []
end
delegate :version, :description, to: :class
@@ -48,6 +51,7 @@ module JsonApiKit
@transformations = Transformations.new(self.class.transformations)
@type_renames = TypeRenames.new(self.class.type_renames)
@default_sorts = DefaultSorts.new(self.class.default_sorts)
+ @removed_filters = RemovedFilters.new(self.class.removed_filters)
end
def verify!
@@ -84,6 +88,8 @@ module JsonApiKit
def current_default_sort(default) = default.convert_names { current_names(it).sole }
+ def each_removed_filter(type, &) = removed_filters.each_for(type, &)
+
def each_current_default_sort(type)
default_sorts.each_for(type) do |default|
yield default.convert_names { transformations.current_names(it).sole }
@@ -92,7 +98,7 @@ module JsonApiKit
private
- attr_reader :transformations, :type_renames, :default_sorts
+ attr_reader :transformations, :type_renames, :default_sorts, :removed_filters
def current_type(name) = name.convert_type { current_resource_type(it) }
diff --git a/lib/json_api_kit/version_change/declarations.rb b/lib/json_api_kit/version_change/declarations.rb
index 5a51d99f4ad..ebb873bc47a 100644
--- a/lib/json_api_kit/version_change/declarations.rb
+++ b/lib/json_api_kit/version_change/declarations.rb
@@ -7,12 +7,19 @@ module JsonApiKit
@type = type
@transformations = []
@default_sorts = []
+ @removed_filters = []
end
- attr_reader :transformations, :default_sorts
+ attr_reader :default_sorts, :removed_filters
+
+ def transformations = @transformations + removed_filters.flat_map(&:transformations)
def changed_default_sort(from:) = default_sorts << DefaultSort.new(type, from)
+ def removed_filter(name, &condition)
+ removed_filters << RemovedFilter.new(type, name, &condition)
+ end
+
def renamed_attribute(**) = declare(Declaration::RenamedAttribute.new(type, **))
def merged_attributes(**) = declare(Declaration::MergedAttributes.new(type, **))
@@ -35,7 +42,7 @@ module JsonApiKit
attr_reader :type
- def declare(declaration) = transformations.concat(declaration.transformations)
+ def declare(declaration) = @transformations.concat(declaration.transformations)
end
end
end
diff --git a/lib/json_api_kit/version_change/removed_filter.rb b/lib/json_api_kit/version_change/removed_filter.rb
new file mode 100644
index 00000000000..936caffaaa3
--- /dev/null
+++ b/lib/json_api_kit/version_change/removed_filter.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+module JsonApiKit
+ class VersionChange
+ class RemovedFilter
+ attr_reader :type, :name
+
+ delegate :apply, to: :filter
+
+ def initialize(type, name, &condition)
+ @type = type.to_s
+ @name = "__removed_filter_#{object_id}"
+ @filter = JsonApiKit::Declarations::Filter.new(name, &condition)
+ end
+
+ def transformations
+ Declaration::RenamedName.new(
+ type,
+ [Name::Filter],
+ from: filter.name,
+ to: name,
+ ).transformations
+ end
+
+ private
+
+ attr_reader :filter
+ end
+ end
+end
diff --git a/lib/json_api_kit/version_change/removed_filters.rb b/lib/json_api_kit/version_change/removed_filters.rb
new file mode 100644
index 00000000000..3acc5700eda
--- /dev/null
+++ b/lib/json_api_kit/version_change/removed_filters.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+module JsonApiKit
+ class VersionChange
+ class RemovedFilters
+ def initialize(filters)
+ @filters = filters.group_by(&:type)
+ @filters.default = [].freeze
+ end
+
+ def each_for(type, &) = filters[type].each(&)
+
+ private
+
+ attr_reader :filters
+ end
+ end
+end
diff --git a/spec/integration/json_api_kit/removed_filters_spec.rb b/spec/integration/json_api_kit/removed_filters_spec.rb
new file mode 100644
index 00000000000..0d289c2f2fb
--- /dev/null
+++ b/spec/integration/json_api_kit/removed_filters_spec.rb
@@ -0,0 +1,294 @@
+# frozen_string_literal: true
+
+module JsonApiKitSpec
+ class RemovedFilterTopicResource < JsonApiKit::Resource
+ model Topic
+ type :topics
+
+ scope { |guardian| Topic.where(user_id: guardian.user&.id) }
+ attribute :title
+ attribute :closed
+ filter :title
+ sort :created_at
+ default_sort created_at: :asc
+ end
+
+ class RemovedFilterTopicsController < JsonApiKit::BaseController
+ resource RemovedFilterTopicResource
+ end
+end
+
+RSpec.describe JsonApiKitSpec::RemovedFilterTopicsController, type: :request do
+ fab!(:author, :user)
+ fab!(:other_author, :user)
+ fab!(:closed_topic) do
+ Fabricate(:topic, user: author, closed: true, archived: false, created_at: Time.utc(2026, 8, 1))
+ end
+ fab!(:open_topic) do
+ Fabricate(:topic, user: author, closed: false, archived: true, created_at: Time.utc(2026, 8, 2))
+ end
+ fab!(:other_topic) { Fabricate(:topic, user: other_author, closed: true) }
+
+ let(:resource) { JsonApiKitSpec::RemovedFilterTopicResource }
+ let(:version) { JsonApiKit::Timeline::FIRST_RELEASE.to_s }
+ let(:parameters) { { filter: { closed: "true" } } }
+ let(:body) { JSON.parse(response.body) }
+ let(:ids) { body.fetch("data").map { it.fetch("id") } }
+ let(:error) { body.fetch("errors").sole }
+ let(:removal_class) do
+ Class.new(JsonApiKit::VersionChange) do
+ version "2026-09-10"
+ description "Topics remove their closed and author filters."
+
+ resource :topics do
+ removed_filter :closed
+ removed_filter(:by_author) { |scope, value| scope.where(user_id: value) }
+ removed_filter(:matching_title) { |scope, value| scope.where(title: value) }
+ end
+ end
+ end
+ let(:removal) { removal_class.new(__FILE__) }
+ let(:changes) { [removal] }
+ let(:version_changes) { JsonApiKit::VersionChanges.new(changes) }
+
+ before do
+ freeze_time(Date.new(2026, 9, 18))
+ sign_in(author)
+ allow(JsonApiKit::VersionChanges).to receive(:core).and_return(version_changes)
+ allow(described_class).to receive(:declared_resource).and_return(resource)
+ Rails.application.routes.disable_clear_and_finalize = true
+ Rails.application.routes.draw do
+ get "/api/removed-filter-topics" => "json_api_kit_spec/removed_filter_topics#index"
+ get "/api/removed-filter-topics/:id" => "json_api_kit_spec/removed_filter_topics#show"
+ end
+ Rails.application.routes.disable_clear_and_finalize = false
+ end
+
+ after { Rails.application.reload_routes! }
+
+ describe "#index" do
+ before do
+ get "/api/removed-filter-topics",
+ headers: {
+ "HTTP_API_VERSION" => version,
+ },
+ params: parameters
+ end
+
+ it "applies the removed equality filter within the resource's scope" do
+ expect(ids).to eq([closed_topic.id.to_s])
+ end
+
+ it "preserves the resource's current declarations" do
+ expect(resource.filter_names).to eq(["title"])
+ end
+
+ it "preserves the attribute with the same name" do
+ expect(body.fetch("data").sole.fetch("attributes")).to include("closed" => true)
+ end
+
+ context "when no filter is requested" do
+ let(:parameters) { {} }
+
+ it "returns every row in the resource's scope" do
+ expect(ids).to eq([closed_topic.id.to_s, open_topic.id.to_s])
+ end
+ end
+
+ context "when the client follows a page link" do
+ fab!(:later_closed_topic) do
+ Fabricate(:topic, user: author, closed: true, created_at: Time.utc(2026, 8, 3))
+ end
+
+ let(:parameters) { super().merge(page: { size: 1 }) }
+ let(:next_page_url) { JSON.parse(response.body).fetch("links").fetch("next") }
+
+ before do
+ get URI.parse(next_page_url).request_uri, headers: { "HTTP_API_VERSION" => version }
+ end
+
+ it "keeps the removed filter in the client's vocabulary" do
+ expect(ids).to eq([later_closed_topic.id.to_s])
+ end
+ end
+
+ context "when the pin includes the removal" do
+ let(:version) { "2026-09-10" }
+
+ it "refuses the removed filter" do
+ expect(error).to include("status" => "400", "title" => "No such filter")
+ end
+ end
+
+ context "when the request uses a custom removed filter" do
+ let(:parameters) { { filter: { matchingTitle: closed_topic.title } } }
+
+ it "runs the retained implementation" do
+ expect(ids).to eq([closed_topic.id.to_s])
+ end
+ end
+
+ context "when the removed filter selects rows outside the resource's scope" do
+ let(:parameters) { { filter: { byAuthor: other_author.id } } }
+
+ it "preserves the scope restriction" do
+ expect(ids).to be_empty
+ end
+ end
+
+ context "when the request uses a current filter" do
+ let(:parameters) { { filter: { title: open_topic.title } } }
+
+ it "uses the current declaration" do
+ expect(ids).to eq([open_topic.id.to_s])
+ end
+ end
+
+ context "when the request supplies an invalid filter value" do
+ let(:parameters) { { filter: { byAuthor: { invalid: "value" } } } }
+
+ it "reports the error in the client's vocabulary" do
+ expect(error).to include(
+ "status" => "400",
+ "title" => "Invalid filter value",
+ "source" => {
+ "parameter" => "filter[byAuthor]",
+ },
+ )
+ end
+ end
+
+ context "when the filter was renamed before removal" do
+ let(:parameters) { { filter: { isClosed: "true" } } }
+ let(:changes) do
+ [
+ Class
+ .new(JsonApiKit::VersionChange) do
+ version "2026-09-05"
+ resource(:topics) { renamed_filter from: :is_closed, to: :closed }
+ end
+ .new(__FILE__),
+ removal,
+ ]
+ end
+
+ it "resolves the earlier name to the retained definition" do
+ expect(ids).to eq([closed_topic.id.to_s])
+ end
+ end
+
+ context "when the current resource reuses the removed name" do
+ let(:resource) do
+ Class.new(super()) { filter(:closed) { |scope, value| scope.where(archived: value) } }
+ end
+
+ it "keeps the retained behavior for the older pin" do
+ expect(ids).to eq([closed_topic.id.to_s])
+ end
+
+ context "when the pin includes the removal" do
+ let(:version) { "2026-09-10" }
+
+ it "uses the current implementation" do
+ expect(ids).to eq([open_topic.id.to_s])
+ end
+ end
+ end
+
+ context "when a later rename reuses the removed spelling" do
+ let(:resource) { Class.new(super()) { filter :archived } }
+ let(:changes) do
+ [
+ removal,
+ Class
+ .new(JsonApiKit::VersionChange) do
+ version "2026-09-15"
+ resource(:topics) { renamed_filter from: :closed, to: :archived }
+ end
+ .new(__FILE__),
+ ]
+ end
+
+ it "keeps the request bound to the removed implementation" do
+ expect(ids).to eq([closed_topic.id.to_s])
+ end
+ end
+
+ context "when the resource type changes after removal" do
+ let(:resource) { Class.new(super()) { type :threads } }
+ let(:changes) do
+ [
+ removal,
+ Class
+ .new(JsonApiKit::VersionChange) do
+ version "2026-09-15"
+ renamed_type from: :topics, to: :threads
+ end
+ .new(__FILE__),
+ ]
+ end
+
+ it "restores the filter for the renamed resource" do
+ expect(ids).to eq([closed_topic.id.to_s])
+ end
+ end
+
+ context "when the resource type changes in the removal's own change" do
+ let(:removal_class) { super().tap { it.renamed_type from: :discussions, to: :topics } }
+
+ it "uses the declaration's type after the rename" do
+ expect(ids).to eq([closed_topic.id.to_s])
+ end
+ end
+
+ context "when the same name is removed more than once" do
+ let(:changes) do
+ [
+ removal,
+ Class
+ .new(JsonApiKit::VersionChange) do
+ version "2026-09-15"
+ resource(:topics) do
+ removed_filter(:closed) { |scope, value| scope.where(archived: value) }
+ end
+ end
+ .new(__FILE__),
+ ]
+ end
+
+ it "uses the first removed implementation for the oldest pin" do
+ expect(ids).to eq([closed_topic.id.to_s])
+ end
+
+ context "when the pin is between the removals" do
+ let(:version) { "2026-09-10" }
+
+ it "uses the second removed implementation" do
+ expect(ids).to eq([open_topic.id.to_s])
+ end
+ end
+
+ context "when the pin includes both removals" do
+ let(:version) { "2026-09-15" }
+
+ it "refuses the filter" do
+ expect(error).to include("status" => "400", "title" => "No such filter")
+ end
+ end
+ end
+ end
+
+ describe "#show" do
+ before do
+ get "/api/removed-filter-topics/#{closed_topic.id}",
+ headers: {
+ "HTTP_API_VERSION" => version,
+ },
+ params: parameters
+ end
+
+ it "refuses collection parameters on an individual request" do
+ expect(error).to include("status" => "400", "source" => { "parameter" => "filter" })
+ end
+ end
+end
diff --git a/spec/lib/json_api_kit/declarations/filters_spec.rb b/spec/lib/json_api_kit/declarations/filters_spec.rb
index 441ddde111d..9507084228a 100644
--- a/spec/lib/json_api_kit/declarations/filters_spec.rb
+++ b/spec/lib/json_api_kit/declarations/filters_spec.rb
@@ -29,6 +29,19 @@ RSpec.describe JsonApiKit::Declarations::Filters do
end
end
+ describe "#with" do
+ subject(:combined) { filters.with([filter_class.new(:archived)]) }
+
+ it "includes the additional declaration" do
+ expect(combined.names).to contain_exactly("title", "closed", "archived")
+ end
+
+ it "preserves the original collection" do
+ combined.names
+ expect(filters.names).to contain_exactly("title", "closed")
+ end
+ end
+
describe "#apply" do
subject(:kept_ids) { filters.apply(scope, filtering).map(&:id) }
@@ -50,6 +63,14 @@ RSpec.describe JsonApiKit::Declarations::Filters do
it "returns the same scope" do
expect(filters.apply(scope)).to be(scope)
end
+
+ context "when declarations are evaluated lazily" do
+ let(:declarations) { Enumerator.new { raise "Filters were evaluated." } }
+
+ it "returns the scope without evaluating the declarations" do
+ expect(filters.apply(scope)).to be(scope)
+ end
+ end
end
context "when the resource declares no filter by that name" do
diff --git a/spec/lib/json_api_kit/declarations/relationship_spec.rb b/spec/lib/json_api_kit/declarations/relationship_spec.rb
index 9b1e25769f3..9ff9688a3df 100644
--- a/spec/lib/json_api_kit/declarations/relationship_spec.rb
+++ b/spec/lib/json_api_kit/declarations/relationship_spec.rb
@@ -40,27 +40,20 @@ RSpec.describe JsonApiKit::Declarations::Relationship do
let(:requested) { JsonApiKit::Page::Requested.for }
describe "#listing" do
- subject(:related_listing) do
- relationship.listing(params, guardian:, scoped_to:, default_sorts:)
- end
+ subject(:related_listing) { relationship.listing(params, guardian:, scoped_to:, edition:) }
let(:params) { { fields: { "users" => %w[username] } } }
let(:guardian) { Guardian.new(author) }
let(:scoped_to) { JsonApiKit::Scoping.for(User.where(id: author.id)) }
let(:listing) { instance_double(JsonApiKit::Query::Collection) }
- let(:default_sorts) { JsonApiKit::Edition.current.default_sorts }
+ let(:edition) { JsonApiKit::Edition.current }
before { allow(users_resource).to receive(:all).and_return(listing) }
it "asks the resource on the other side for a listing" do
related_listing
- expect(users_resource).to have_received(:all).with(
- params,
- guardian:,
- scoped_to:,
- default_sorts:,
- )
+ expect(users_resource).to have_received(:all).with(params, guardian:, scoped_to:, edition:)
end
it "returns the listing that resource reads" do
diff --git a/spec/lib/json_api_kit/default_sorts/history_spec.rb b/spec/lib/json_api_kit/default_sorts/history_spec.rb
index 472f6c4a273..08ba61f38c1 100644
--- a/spec/lib/json_api_kit/default_sorts/history_spec.rb
+++ b/spec/lib/json_api_kit/default_sorts/history_spec.rb
@@ -3,7 +3,10 @@
RSpec.describe JsonApiKit::DefaultSorts::History do
subject(:history) { described_class.new(resource:, changes:) }
- let(:resource) do
+ let(:resource) { resource_class.new(guardian:, edition:) }
+ let(:guardian) { Guardian.new }
+ let(:edition) { JsonApiKit::Edition.new(changes) }
+ let(:resource_class) do
Class.new(JsonApiKit::Resource) do
model Topic
type :topics
@@ -211,7 +214,9 @@ RSpec.describe JsonApiKit::DefaultSorts::History do
resources.to_h { [it.type, described_class.new(resource: it, changes:).ordering] }
end
- let(:resources) { [resource, Class.new(resource) { type :users }] }
+ let(:resources) do
+ [resource, Class.new(resource_class) { type :users }.new(guardian:, edition:)]
+ end
let(:changes) do
[
Class
diff --git a/spec/lib/json_api_kit/default_sorts_spec.rb b/spec/lib/json_api_kit/default_sorts_spec.rb
index cca9a486368..d82c1417706 100644
--- a/spec/lib/json_api_kit/default_sorts_spec.rb
+++ b/spec/lib/json_api_kit/default_sorts_spec.rb
@@ -12,7 +12,10 @@ RSpec.describe JsonApiKit::DefaultSorts do
.new(__FILE__),
]
end
- let(:resource) do
+ let(:resource) { resource_class.new(guardian:, edition:) }
+ let(:guardian) { Guardian.new }
+ let(:edition) { JsonApiKit::Edition.new(changes) }
+ let(:resource_class) do
Class.new(JsonApiKit::Resource) do
model Topic
type :topics
@@ -32,9 +35,32 @@ RSpec.describe JsonApiKit::DefaultSorts do
expect(defaults.for(resource)).to equal(ordering)
end
+ context "when another instance uses the same resource class" do
+ let(:other_resource) { resource_class.new(guardian:, edition:) }
+
+ before { ordering }
+
+ it "reuses the resolved ordering" do
+ expect(defaults.for(other_resource)).to equal(ordering)
+ end
+ end
+
+ context "when another request uses the current edition" do
+ let(:current_edition) { JsonApiKit::Edition.current }
+ let(:current_resource) { resource_class.new(guardian:, edition: current_edition) }
+
+ before { edition.default_sorts.for(resource) }
+
+ it "resolves that edition's default" do
+ expect(current_edition.default_sorts.for(current_resource)).to eq("created_at" => :asc)
+ end
+ end
+
context "when two resource classes share a type" do
let(:changes) { [] }
- let(:other_resource) { Class.new(resource) { default_sort created_at: :desc } }
+ let(:other_resource) do
+ Class.new(resource_class) { default_sort created_at: :desc }.new(guardian:, edition:)
+ end
before { ordering }
diff --git a/spec/lib/json_api_kit/document_spec.rb b/spec/lib/json_api_kit/document_spec.rb
index f1d08ec1fd5..78ed8bda7cc 100644
--- a/spec/lib/json_api_kit/document_spec.rb
+++ b/spec/lib/json_api_kit/document_spec.rb
@@ -41,6 +41,75 @@ RSpec.describe JsonApiKit::Document do
expect(document.status).to eq("200")
end
+ context "when the resource supplies request context" do
+ let(:instance) { resource.new(guardian:, edition:) }
+
+ before do
+ allow(resource).to receive(:new).and_return(instance)
+ allow(JsonApiKit::Request::Input::Collection).to receive(:new).and_call_original
+ allow(JsonApiKit::Query::Collection).to receive(:new).and_call_original
+ document
+ end
+
+ it "uses the instance for input validation" do
+ expect(JsonApiKit::Request::Input::Collection).to have_received(:new).with(
+ parameters,
+ resource: instance,
+ edition:,
+ )
+ end
+
+ it "uses the same instance for execution" do
+ expect(JsonApiKit::Query::Collection).to have_received(:new).with(
+ instance,
+ an_instance_of(JsonApiKit::Request::Collection),
+ scoped_to:,
+ )
+ end
+
+ it "creates one instance for the request" do
+ expect(resource).to have_received(:new).once
+ end
+ end
+
+ context "when the request includes related records" do
+ let(:parameters) { { include: "user" } }
+ let(:users_resource) do
+ Class.new(JsonApiKit::Resource) do
+ model User
+ type :users
+ attribute :username
+ end
+ end
+ let(:resource) { Class.new(super()).tap { it.has_one(:user, resource: users_resource) } }
+ let(:users) { users_resource.new(guardian:, edition:) }
+
+ before do
+ allow(users_resource).to receive(:new).and_return(users)
+ allow(JsonApiKit::Query::Collection).to receive(:new).and_call_original
+ document
+ end
+
+ it "creates one resource instance for the related listing" do
+ expect(users_resource).to have_received(:new).with(guardian:, edition:).once
+ end
+
+ it "queries the related records through that instance" do
+ expect(JsonApiKit::Query::Collection).to have_received(:new).with(
+ users,
+ an_instance_of(JsonApiKit::Request::Collection),
+ scoped_to: an_instance_of(JsonApiKit::Scoping::PerOwner),
+ )
+ end
+
+ it "renders all related records" do
+ expect(document.to_h[:included].map { it[:id] }).to contain_exactly(
+ topic.user_id.to_s,
+ other_topic.user_id.to_s,
+ )
+ end
+ end
+
context "when a caller narrows the listing with a scope" do
let(:scoped_to) { Topic.where(id: topic.id) }
@@ -69,6 +138,36 @@ RSpec.describe JsonApiKit::Document do
expect(document.to_h[:data][:id]).to eq(topic.id.to_s)
end
+ context "when the resource supplies request context" do
+ let(:instance) { resource.new(guardian:, edition:) }
+
+ before do
+ allow(resource).to receive(:new).and_return(instance)
+ allow(JsonApiKit::Request::Input::Individual).to receive(:new).and_call_original
+ allow(JsonApiKit::Query::Individual).to receive(:new).and_call_original
+ document
+ end
+
+ it "uses the instance for input validation" do
+ expect(JsonApiKit::Request::Input::Individual).to have_received(:new).with(
+ parameters,
+ resource: instance,
+ edition:,
+ )
+ end
+
+ it "uses the same instance for execution" do
+ expect(JsonApiKit::Query::Individual).to have_received(:new).with(
+ instance,
+ an_instance_of(JsonApiKit::Request::Individual),
+ )
+ end
+
+ it "creates one instance for the request" do
+ expect(resource).to have_received(:new).once
+ end
+ end
+
context "when no row holds that id" do
let(:id) { -1 }
diff --git a/spec/lib/json_api_kit/edition_spec.rb b/spec/lib/json_api_kit/edition_spec.rb
index 65121722806..bf491ce6e1e 100644
--- a/spec/lib/json_api_kit/edition_spec.rb
+++ b/spec/lib/json_api_kit/edition_spec.rb
@@ -18,7 +18,9 @@ RSpec.describe JsonApiKit::Edition do
.new(__FILE__)
end
let(:changes) { [version_change] }
- let(:resource) do
+ let(:resource) { resource_class.new(guardian:, edition:) }
+ let(:guardian) { Guardian.new }
+ let(:resource_class) do
Class.new(JsonApiKit::Resource) do
model Topic
type :topics
@@ -83,4 +85,10 @@ RSpec.describe JsonApiKit::Edition do
expect(edition.default_sorts).to equal(edition.default_sorts)
end
end
+
+ describe "#removed_filters" do
+ it "retains the resolver for the edition" do
+ expect(edition.removed_filters).to equal(edition.removed_filters)
+ end
+ end
end
diff --git a/spec/lib/json_api_kit/query/collection_spec.rb b/spec/lib/json_api_kit/query/collection_spec.rb
index a573762415b..a59717c58b8 100644
--- a/spec/lib/json_api_kit/query/collection_spec.rb
+++ b/spec/lib/json_api_kit/query/collection_spec.rb
@@ -13,7 +13,8 @@ RSpec.describe JsonApiKit::Query::Collection do
Fabricate(:topic, title: "Bands read one at a time", created_at: Time.utc(2026, 8, 3))
end
- let(:resource) do
+ let(:resource) { resource_class.new(guardian:, edition:) }
+ let(:resource_class) do
Class.new(JsonApiKit::Resource) do
model Topic
type :topics
@@ -25,10 +26,11 @@ RSpec.describe JsonApiKit::Query::Collection do
JsonApiKit::Request::Collection.new(
JsonApiKit::Request::Input.with_defaults(params, resource:, default_sorts:),
guardian:,
- default_sorts:,
+ edition:,
)
end
- let(:default_sorts) { JsonApiKit::Edition.current.default_sorts }
+ let(:edition) { JsonApiKit::Edition.current }
+ let(:default_sorts) { edition.default_sorts }
let(:params) { {} }
let(:guardian) { Guardian.new }
let(:scoped_to) { nil }
@@ -37,7 +39,7 @@ RSpec.describe JsonApiKit::Query::Collection do
JsonApiKit::Request::Collection.new(
JsonApiKit::Request::Input.with_defaults(params, resource:, default_sorts:),
guardian: Guardian.new,
- default_sorts:,
+ edition:,
)
describe ".new" do
@@ -111,7 +113,7 @@ RSpec.describe JsonApiKit::Query::Collection do
describe "the fields it renders" do
subject(:fields) { query.records.map(&:attributes) }
- let(:resource) do
+ let(:resource_class) do
Class.new(JsonApiKit::Resource) do
model Topic
type :topics
@@ -146,7 +148,7 @@ RSpec.describe JsonApiKit::Query::Collection do
end
context "when the fields hold a block attribute" do
- let(:resource) do
+ let(:resource_class) do
Class.new(JsonApiKit::Resource) do
model Topic
type :topics
@@ -196,7 +198,7 @@ RSpec.describe JsonApiKit::Query::Collection do
end
context "when the filter holds a value" do
- let(:resource) { Class.new(super()) { filter :title } }
+ let(:resource_class) { Class.new(super()) { filter :title } }
let(:params) { { filter: { title: second_topic.title } } }
it "returns only the rows that filter keeps" do
@@ -205,7 +207,7 @@ RSpec.describe JsonApiKit::Query::Collection do
end
context "when the resource declares a scope" do
- let(:resource) do
+ let(:resource_class) do
Class.new(super()) { scope { |guardian| Topic.where(user_id: guardian.user&.id) } }
end
let(:guardian) { Guardian.new(second_topic.user) }
diff --git a/spec/lib/json_api_kit/query/individual_spec.rb b/spec/lib/json_api_kit/query/individual_spec.rb
index ad8ccc1566f..3c18b9bb5bc 100644
--- a/spec/lib/json_api_kit/query/individual_spec.rb
+++ b/spec/lib/json_api_kit/query/individual_spec.rb
@@ -6,7 +6,8 @@ RSpec.describe JsonApiKit::Query::Individual do
fab!(:topic) { Fabricate(:topic, title: "The row an id names") }
fab!(:other_topic) { Fabricate(:topic, title: "A row nobody named") }
- let(:resource) do
+ let(:resource) { resource_class.new(guardian:, edition:) }
+ let(:resource_class) do
Class.new(JsonApiKit::Resource) do
model Topic
type :topics
@@ -17,10 +18,11 @@ RSpec.describe JsonApiKit::Query::Individual do
JsonApiKit::Request::Individual.new(
JsonApiKit::Request::Input.with_defaults(params, resource:, default_sorts:),
guardian:,
- default_sorts:,
+ edition:,
)
end
- let(:default_sorts) { JsonApiKit::Edition.current.default_sorts }
+ let(:edition) { JsonApiKit::Edition.current }
+ let(:default_sorts) { edition.default_sorts }
let(:params) { { id: topic.id } }
let(:guardian) { Guardian.new }
let(:title) { JsonApiKit::Name::Field.new(value: "title", type: "topics") }
@@ -43,7 +45,7 @@ RSpec.describe JsonApiKit::Query::Individual do
end
context "when the scope withholds the row" do
- let(:resource) { Class.new(super()) { scope { Topic.where(closed: true) } } }
+ let(:resource_class) { Class.new(super()) { scope { Topic.where(closed: true) } } }
it "raises not found instead of answering with nothing" do
expect { reading.record }.to raise_error(JsonApiKit::NotFound)
diff --git a/spec/lib/json_api_kit/removed_filters_spec.rb b/spec/lib/json_api_kit/removed_filters_spec.rb
new file mode 100644
index 00000000000..5a8d260feaa
--- /dev/null
+++ b/spec/lib/json_api_kit/removed_filters_spec.rb
@@ -0,0 +1,73 @@
+# frozen_string_literal: true
+
+RSpec.describe JsonApiKit::RemovedFilters do
+ subject(:removed_filters) { described_class.new(changes) }
+
+ let(:changes) { [removal] }
+ let(:removal) { removal_class.new(__FILE__) }
+ let(:removal_class) do
+ Class.new(JsonApiKit::VersionChange) do
+ resource(:topics) { removed_filter :closed }
+ resource(:groups) { removed_filter :name }
+ end
+ end
+ let(:topic_filter) { removal_class.removed_filters.first }
+
+ describe "#for" do
+ subject(:filters) { removed_filters.for(type).to_a }
+
+ let(:type) { "topics" }
+
+ it "returns the resource's retained filters" do
+ expect(filters).to contain_exactly(topic_filter)
+ end
+
+ context "when the edition has no changes" do
+ let(:changes) { [] }
+
+ it "returns no retained filters" do
+ expect(filters).to be_empty
+ end
+ end
+
+ context "when the resource has no removed filters" do
+ let(:type) { "users" }
+
+ it "returns no retained filters" do
+ expect(filters).to be_empty
+ end
+ end
+
+ context "when the resource type changes after removal" do
+ let(:type) { "threads" }
+ let(:changes) do
+ [
+ removal,
+ Class
+ .new(JsonApiKit::VersionChange) { renamed_type from: :topics, to: :threads }
+ .new(__FILE__),
+ ]
+ end
+
+ it "finds the filters under the resource's earlier type" do
+ expect(filters).to contain_exactly(topic_filter)
+ end
+
+ context "when another resource acquires the earlier type" do
+ let(:type) { "topics" }
+ let(:changes) do
+ super() +
+ [
+ Class
+ .new(JsonApiKit::VersionChange) { renamed_type from: :groups, to: :topics }
+ .new(__FILE__),
+ ]
+ end
+
+ it "returns only that resource's retained filters" do
+ expect(filters).to contain_exactly(removal_class.removed_filters.last)
+ end
+ end
+ end
+ end
+end
diff --git a/spec/lib/json_api_kit/request/input/collection_spec.rb b/spec/lib/json_api_kit/request/input/collection_spec.rb
index 9ac8426a3bc..d010d9c2b13 100644
--- a/spec/lib/json_api_kit/request/input/collection_spec.rb
+++ b/spec/lib/json_api_kit/request/input/collection_spec.rb
@@ -15,7 +15,9 @@ RSpec.describe JsonApiKit::Request::Input::Collection do
end
.new(__FILE__)
end
- let(:resource) do
+ let(:resource) { resource_class.new(guardian:, edition:) }
+ let(:guardian) { Guardian.new }
+ let(:resource_class) do
Class.new(JsonApiKit::Resource) do
model Topic
type :topics
diff --git a/spec/lib/json_api_kit/request/input/individual_spec.rb b/spec/lib/json_api_kit/request/input/individual_spec.rb
index e443f92b957..88793352d4c 100644
--- a/spec/lib/json_api_kit/request/input/individual_spec.rb
+++ b/spec/lib/json_api_kit/request/input/individual_spec.rb
@@ -5,7 +5,9 @@ RSpec.describe JsonApiKit::Request::Input::Individual do
let(:parameters) { {} }
let(:edition) { JsonApiKit::Edition.current }
- let(:resource) do
+ let(:resource) { resource_class.new(guardian:, edition:) }
+ let(:guardian) { Guardian.new }
+ let(:resource_class) do
Class.new(JsonApiKit::Resource) do
model Topic
type :topics
diff --git a/spec/lib/json_api_kit/request/input_spec.rb b/spec/lib/json_api_kit/request/input_spec.rb
index 96ac22f1bd2..8bf985f37ad 100644
--- a/spec/lib/json_api_kit/request/input_spec.rb
+++ b/spec/lib/json_api_kit/request/input_spec.rb
@@ -1,7 +1,10 @@
# frozen_string_literal: true
RSpec.describe JsonApiKit::Request::Input do
- let(:resource) do
+ let(:resource) { resource_class.new(guardian:, edition:) }
+ let(:guardian) { Guardian.new }
+ let(:edition) { JsonApiKit::Edition.current }
+ let(:resource_class) do
Class.new(JsonApiKit::Resource) do
model Topic
type :topics
@@ -9,7 +12,7 @@ RSpec.describe JsonApiKit::Request::Input do
default_sort title: :asc
end
end
- let(:default_sorts) { JsonApiKit::Edition.current.default_sorts }
+ let(:default_sorts) { edition.default_sorts }
describe ".with_defaults" do
subject(:parameters) { described_class.with_defaults(input, resource:, default_sorts:) }
diff --git a/spec/lib/json_api_kit/request_spec.rb b/spec/lib/json_api_kit/request_spec.rb
index 8da237e7264..825b77bd270 100644
--- a/spec/lib/json_api_kit/request_spec.rb
+++ b/spec/lib/json_api_kit/request_spec.rb
@@ -2,11 +2,7 @@
RSpec.describe JsonApiKit::Request do
subject(:request) do
- described_class::Collection.new(
- params,
- guardian:,
- default_sorts: JsonApiKit::Edition.current.default_sorts,
- )
+ described_class::Collection.new(params, guardian:, edition: JsonApiKit::Edition.current)
end
let(:params) { {} }
@@ -23,11 +19,7 @@ RSpec.describe JsonApiKit::Request do
context "when a caller asks for one record" do
subject(:request) do
- described_class::Individual.new(
- params,
- guardian:,
- default_sorts: JsonApiKit::Edition.current.default_sorts,
- )
+ described_class::Individual.new(params, guardian:, edition: JsonApiKit::Edition.current)
end
let(:params) { { id: topic.id } }
@@ -73,11 +65,7 @@ RSpec.describe JsonApiKit::Request do
context "when a caller asks for one record" do
subject(:request) do
- described_class::Individual.new(
- params,
- guardian:,
- default_sorts: JsonApiKit::Edition.current.default_sorts,
- )
+ described_class::Individual.new(params, guardian:, edition: JsonApiKit::Edition.current)
end
let(:params) { { id: 12, fields: { users: %w[username] } } }
diff --git a/spec/lib/json_api_kit/resource/instance_spec.rb b/spec/lib/json_api_kit/resource/instance_spec.rb
new file mode 100644
index 00000000000..18bfc26cd4e
--- /dev/null
+++ b/spec/lib/json_api_kit/resource/instance_spec.rb
@@ -0,0 +1,106 @@
+# frozen_string_literal: true
+
+RSpec.describe JsonApiKit::Resource do
+ subject(:resource) { resource_class.new(guardian:, edition:) }
+
+ let(:guardian) { Guardian.new }
+
+ let(:edition) { JsonApiKit::Edition.new(changes) }
+ let(:changes) { [removal_class.new(__FILE__)] }
+ let(:removal_class) do
+ Class.new(JsonApiKit::VersionChange) { resource(:topics) { removed_filter :closed } }
+ end
+ let(:resource_class) do
+ Class.new(JsonApiKit::Resource) do
+ model Topic
+ type :topics
+ filter :title
+ sort :created_at
+ default_sort created_at: :desc
+ end
+ end
+ let(:filter_name) do
+ edition
+ .glossary
+ .declared_name(JsonApiKit::Name::Filter.new(value: "closed", type: "topics"))
+ .value
+ end
+
+ describe "#apply_filters" do
+ subject(:kept_ids) { resource.apply_filters(Topic.all, filtering).map(&:id) }
+
+ fab!(:kept_topic) { Fabricate(:topic, title: "The rows a filter keeps", closed: false) }
+ fab!(:dropped_topic) { Fabricate(:topic, title: "The rows it leaves behind", closed: true) }
+
+ let(:changes) { [] }
+ let(:filtering) { { "title" => kept_topic.title } }
+
+ it "narrows the listing with the declared filter" do
+ expect(kept_ids).to contain_exactly(kept_topic.id)
+ end
+
+ context "when a filter is declared before the next request" do
+ subject(:kept_ids) { next_resource.apply_filters(Topic.all, filtering).map(&:id) }
+
+ let(:next_resource) { resource_class.new(guardian:, edition:) }
+ let(:filtering) { { "closed" => false } }
+
+ before do
+ resource.apply_filters(Topic.all)
+ resource_class.filter(:closed)
+ end
+
+ it "uses the new declaration in the next resource instance" do
+ expect(kept_ids).to contain_exactly(kept_topic.id)
+ end
+ end
+ end
+
+ describe "#filter_names" do
+ subject(:names) { resource.filter_names }
+
+ it "combines current declarations with the retained binding" do
+ expect(names).to contain_exactly("title", filter_name)
+ end
+ end
+
+ describe "#filters" do
+ it "retains the collection for the resource instance" do
+ expect(resource.filters).to equal(resource.filters)
+ end
+
+ context "when another request uses the current edition" do
+ let(:current_resource) { resource_class.new(guardian:, edition: JsonApiKit::Edition.current) }
+
+ before { resource.filter_names }
+
+ it "uses only current declarations" do
+ expect(current_resource.filter_names).to eq(["title"])
+ end
+
+ it "preserves the class declarations" do
+ expect(resource_class.filter_names).to eq(["title"])
+ end
+ end
+
+ context "when two resource classes share a type" do
+ let(:other_resource) do
+ Class.new(resource_class) { filter :archived }.new(guardian:, edition:)
+ end
+
+ before { resource.filter_names }
+
+ it "uses each resource's own declarations" do
+ expect(other_resource.filter_names).to contain_exactly("title", "archived", filter_name)
+ end
+ end
+ end
+
+ describe "#default_ordering" do
+ subject(:ordering) { resource.default_ordering }
+
+ it "preserves the resource's default sort" do
+ expect(ordering).to eq("created_at" => :desc)
+ end
+ end
+end
diff --git a/spec/lib/json_api_kit/resource_spec.rb b/spec/lib/json_api_kit/resource_spec.rb
index bfefb5f756f..eaa5e7b75b0 100644
--- a/spec/lib/json_api_kit/resource_spec.rb
+++ b/spec/lib/json_api_kit/resource_spec.rb
@@ -365,32 +365,6 @@ RSpec.describe JsonApiKit::Resource do
end
end
- describe ".filter" do
- subject(:kept_ids) do
- topic_resource.apply_filters(Topic.all, "title" => kept_topic.title).map(&:id)
- end
-
- fab!(:kept_topic) { Fabricate(:topic, title: "The rows a filter keeps") }
- fab!(:dropped_topic) { Fabricate(:topic, title: "The rows it leaves behind") }
-
- it "lets a request narrow the listing by the name it declares" do
- expect(kept_ids).to contain_exactly(kept_topic.id)
- end
-
- context "when the resource declares one more after a reading" do
- subject(:kept_ids) { topic_resource.apply_filters(Topic.all, "closed" => false).map(&:id) }
-
- before do
- topic_resource.apply_filters(Topic.all)
- topic_resource.filter(:closed)
- end
-
- it "narrows by the new filter too" do
- expect(kept_ids).to include(kept_topic.id)
- end
- end
- end
-
describe ".attribute" do
subject(:attribute_values) { topic_resource.fields(guardian:).attributes.values_for(topic) }
@@ -637,7 +611,7 @@ RSpec.describe JsonApiKit::Resource do
topic_resource.all({ sort: { created_at: :asc } }, guardian:)
expect(JsonApiKit::Query::Collection).to have_received(:new).with(
- topic_resource,
+ an_instance_of(topic_resource),
an_object_having_attributes(ordering: { "created_at" => :asc }, guardian:),
scoped_to: nil,
)
@@ -667,7 +641,7 @@ RSpec.describe JsonApiKit::Resource do
topic_resource.find(12, guardian:)
expect(JsonApiKit::Query::Individual).to have_received(:new).with(
- topic_resource,
+ an_instance_of(topic_resource),
an_object_having_attributes(ordering: { "ran_at" => :desc }, guardian:),
)
end
diff --git a/spec/lib/json_api_kit/sideload_spec.rb b/spec/lib/json_api_kit/sideload_spec.rb
index 8d62fc70978..a47d032c75c 100644
--- a/spec/lib/json_api_kit/sideload_spec.rb
+++ b/spec/lib/json_api_kit/sideload_spec.rb
@@ -24,7 +24,7 @@ RSpec.describe JsonApiKit::Sideload do
JsonApiKit::Request::Collection.new(
params,
guardian: Guardian.new,
- default_sorts: JsonApiKit::Edition.current.default_sorts,
+ edition: JsonApiKit::Edition.current,
)
end
let(:schema) { JsonApiKit::Schema.new(Topic) }
diff --git a/spec/lib/json_api_kit/sideloads_spec.rb b/spec/lib/json_api_kit/sideloads_spec.rb
index 3adff9fe196..966d1060f4a 100644
--- a/spec/lib/json_api_kit/sideloads_spec.rb
+++ b/spec/lib/json_api_kit/sideloads_spec.rb
@@ -43,7 +43,7 @@ RSpec.describe JsonApiKit::Sideloads do
JsonApiKit::Request::Collection.new(
{},
guardian: Guardian.new,
- default_sorts: JsonApiKit::Edition.current.default_sorts,
+ edition: JsonApiKit::Edition.current,
)
end
let(:schema) { JsonApiKit::Schema.new(Topic) }
diff --git a/spec/lib/json_api_kit/version_change/removed_filters_spec.rb b/spec/lib/json_api_kit/version_change/removed_filters_spec.rb
new file mode 100644
index 00000000000..8560a1f3912
--- /dev/null
+++ b/spec/lib/json_api_kit/version_change/removed_filters_spec.rb
@@ -0,0 +1,68 @@
+# frozen_string_literal: true
+
+RSpec.describe JsonApiKit::VersionChange do
+ subject(:version_change) { change_class.new("2026-09-10_remove_filters.rb") }
+
+ let(:change_class) do
+ Class.new(described_class) do
+ version "2026-09-10"
+ description "Topics remove their closed filter."
+ resource(:topics) { removed_filter :closed }
+ end
+ end
+
+ describe "#verify!" do
+ subject(:verify) { version_change.verify! }
+
+ before { freeze_time(Date.new(2026, 9, 18)) }
+
+ it "accepts a removed filter" do
+ expect { verify }.not_to raise_error
+ end
+
+ context "when the same filter is removed twice" do
+ before { change_class.resource(:topics) { removed_filter :closed } }
+
+ it "reports the conflicting name and source" do
+ expect { verify }.to raise_error(
+ ArgumentError,
+ "2026-09-10_remove_filters.rb changes closed twice.",
+ )
+ end
+ end
+
+ context "when the same filter is both renamed and removed" do
+ before { change_class.resource(:topics) { renamed_filter from: :closed, to: :archived } }
+
+ it "rejects the conflicting declarations" do
+ expect { verify }.to raise_error(ArgumentError, /changes closed twice/)
+ end
+ end
+
+ context "when two resources remove the same filter name" do
+ before { change_class.resource(:other_topics) { removed_filter :closed } }
+
+ it "accepts the independent declarations" do
+ expect { verify }.not_to raise_error
+ end
+ end
+ end
+
+ describe "#each_removed_filter" do
+ subject(:filters) { version_change.enum_for(:each_removed_filter, type).to_a }
+
+ let(:type) { "topics" }
+
+ it "yields the retained declaration" do
+ expect(filters).to contain_exactly(change_class.removed_filters.sole)
+ end
+
+ context "when the resource has no removed filters" do
+ let(:type) { "users" }
+
+ it "yields no filters" do
+ expect(filters).to be_empty
+ end
+ end
+ end
+end