Skip to content

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

DEV: Convert between snake case and camel case for JSON:API (#43097)

Loïc Guitaut65e531222250
diff --git a/app/controllers/json_api_kit/base_controller.rb b/app/controllers/json_api_kit/base_controller.rb
index beb53cf695a..eaec020a32d 100644
--- a/app/controllers/json_api_kit/base_controller.rb
+++ b/app/controllers/json_api_kit/base_controller.rb
@@ -12,6 +12,6 @@ module JsonApiKit
include Negotiating
include Versioning
include Serving
- include Reading
+ include Fetching
end
end
diff --git a/app/controllers/json_api_kit/base_controller/reading.rb b/app/controllers/json_api_kit/base_controller/fetching.rb
similarity index 75%
rename from app/controllers/json_api_kit/base_controller/reading.rb
rename to app/controllers/json_api_kit/base_controller/fetching.rb
index ba1a0b74c0e..26f6849b2f1 100644
--- a/app/controllers/json_api_kit/base_controller/reading.rb
+++ b/app/controllers/json_api_kit/base_controller/fetching.rb
@@ -2,14 +2,20 @@
module JsonApiKit
class BaseController
- module Reading
+ module Fetching
extend ActiveSupport::Concern
ROOT = "/api"
def index
render_document(
- Document::Collection.for(request.query_parameters, resource:, guardian:, urls:),
+ Document::Collection.for(
+ request.query_parameters,
+ resource:,
+ guardian:,
+ urls:,
+ glossary:,
+ ),
)
end
@@ -21,6 +27,7 @@ module JsonApiKit
resource:,
guardian:,
urls:,
+ glossary:,
),
)
end
diff --git a/app/controllers/json_api_kit/base_controller/versioning.rb b/app/controllers/json_api_kit/base_controller/versioning.rb
index 174e175f8f1..4c4c515327a 100644
--- a/app/controllers/json_api_kit/base_controller/versioning.rb
+++ b/app/controllers/json_api_kit/base_controller/versioning.rb
@@ -16,6 +16,8 @@ module JsonApiKit
end
def requested_version = request.headers[ApiVersion::HEADER]
+
+ def glossary = @glossary ||= Glossary.resource
end
end
end
diff --git a/lib/json_api_kit/api_version.rb b/lib/json_api_kit/api_version.rb
index cecb51f62fd..b80d2e190c2 100644
--- a/lib/json_api_kit/api_version.rb
+++ b/lib/json_api_kit/api_version.rb
@@ -41,7 +41,9 @@ module JsonApiKit
class InTheFuture < Refusal
def title = "#{HEADER} is in the future"
- def detail = "The current version is #{version}."
+ def detail
+ "#{HEADER} must not be later than today in UTC. The current version is #{version}."
+ end
end
def self.parse(raw)
@@ -57,7 +59,10 @@ module JsonApiKit
@date = date
end
- def <=>(other) = date <=> other.date
+ def <=>(other)
+ return unless other.is_a?(self.class)
+ date <=> other.date
+ end
def future? = date.future?
diff --git a/lib/json_api_kit/document.rb b/lib/json_api_kit/document.rb
index c804bd57314..fcd42bc10c6 100644
--- a/lib/json_api_kit/document.rb
+++ b/lib/json_api_kit/document.rb
@@ -2,27 +2,31 @@
module JsonApiKit
class Document
- def self.build(parameters, resource:, urls:)
+ def self.build(parameters, resource:, urls:, glossary:)
+ declared_parameters = Request::Parameters.new(parameters.to_hash, glossary:).to_h
contract_class
.new(
- **parameters.to_hash.deep_dup,
+ **declared_parameters.deep_dup,
options: {
resource:,
- raw_parameters: parameters.with_indifferent_access,
+ raw_parameters: declared_parameters.with_indifferent_access,
},
)
.then do |contract|
- next Errors.new(*Request::Contract::Mapper.new(contract.errors).to_a) if contract.invalid?
- new(yield(contract.to_hash), urls:).tap(&:to_h)
+ if contract.invalid?
+ next(Errors.new(*Request::Contract::Mapper.new(contract.errors, glossary:).to_a))
+ end
+ new(yield(contract.to_hash), urls:, glossary:).tap(&:to_h)
end
rescue Error => error
Errors.new(error)
end
private_class_method :build
- def initialize(query, urls:)
+ def initialize(query, urls:, glossary:)
@query = query
@urls = urls
+ @glossary = glossary
end
def to_h = @to_h ||= { data:, included:, links: }
@@ -31,12 +35,12 @@ module JsonApiKit
private
- attr_reader :query, :urls
+ attr_reader :query, :urls, :glossary
def contents = @contents ||= Contents.new(primary_records, query.included)
def links = { self: { href: urls.current.to_s, type: Pagination::Profile::MEDIA_TYPE } }
- def included = contents.related.map { ResourceObject.new(it, urls:).to_h }
+ def included = contents.related.map { ResourceObject.new(it, urls:, glossary:).to_h }
end
end
diff --git a/lib/json_api_kit/document/collection.rb b/lib/json_api_kit/document/collection.rb
index 1b5d2e9a792..aad1850c428 100644
--- a/lib/json_api_kit/document/collection.rb
+++ b/lib/json_api_kit/document/collection.rb
@@ -6,8 +6,8 @@ module JsonApiKit
class << self
def contract_class = Request::Contract::Collection
- def for(parameters, resource:, guardian:, urls:, scoped_to: nil)
- build(parameters, resource:, urls:) { resource.all(it, guardian:, scoped_to:) }
+ def for(parameters, resource:, guardian:, urls:, glossary:, scoped_to: nil)
+ build(parameters, resource:, urls:, glossary:) { resource.all(it, guardian:, scoped_to:) }
end
end
@@ -15,7 +15,9 @@ module JsonApiKit
def primary_records = query.records
- def data = contents.primary.map { ResourceObject.new(it, urls:, meta: page_meta(it)).to_h }
+ def data
+ contents.primary.map { ResourceObject.new(it, urls:, glossary:, meta: page_meta(it)).to_h }
+ end
def page_meta(record)
return {} unless query.item_cursors?
diff --git a/lib/json_api_kit/document/individual.rb b/lib/json_api_kit/document/individual.rb
index b2ca489d58f..a0d1cf4f605 100644
--- a/lib/json_api_kit/document/individual.rb
+++ b/lib/json_api_kit/document/individual.rb
@@ -6,8 +6,8 @@ module JsonApiKit
class << self
def contract_class = Request::Contract::Individual
- def for(id, parameters, resource:, guardian:, urls:)
- build(parameters, resource:, urls:) { resource.find(id, it, guardian:) }
+ def for(id, parameters, resource:, guardian:, urls:, glossary:)
+ build(parameters, resource:, urls:, glossary:) { resource.find(id, it, guardian:) }
end
end
@@ -15,7 +15,7 @@ module JsonApiKit
def primary_records = [query.record]
- def data = ResourceObject.new(contents.primary.sole, urls:).to_h
+ def data = ResourceObject.new(contents.primary.sole, urls:, glossary:).to_h
end
end
end
diff --git a/lib/json_api_kit/document/resource_object.rb b/lib/json_api_kit/document/resource_object.rb
index ef1f1379894..dad3f279e9c 100644
--- a/lib/json_api_kit/document/resource_object.rb
+++ b/lib/json_api_kit/document/resource_object.rb
@@ -3,11 +3,12 @@
module JsonApiKit
class Document
class ResourceObject
- delegate :type, :id, :attributes, to: :record, private: true
+ delegate :type, :id, to: :record, private: true
- def initialize(record, urls:, meta: {})
+ def initialize(record, urls:, glossary:, meta: {})
@record = record
@urls = urls
+ @glossary = glossary
@meta = meta
end
@@ -15,11 +16,14 @@ module JsonApiKit
private
- attr_reader :record, :urls, :meta
+ attr_reader :record, :urls, :glossary, :meta
+
+ def attributes = record.attributes.transform_keys { glossary.member_name(it) }
def relationships
record.relationships.to_h do |name, linkage|
- [name, RelationshipObject.new(linkage, urls:, owner: record, name:).to_h]
+ member = glossary.member_name(name)
+ [member, RelationshipObject.new(linkage, urls:, owner: record, name: member).to_h]
end
end
diff --git a/lib/json_api_kit/glossary.rb b/lib/json_api_kit/glossary.rb
new file mode 100644
index 00000000000..7bf2bf809ef
--- /dev/null
+++ b/lib/json_api_kit/glossary.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+module JsonApiKit
+ class Glossary
+ class << self
+ def kit = new([CasingRule])
+
+ # TODO: add the versioning rule here (next PR)
+ def resource = kit
+ end
+
+ def initialize(rules)
+ @rules = rules
+ end
+
+ def declared_name(raw) = rules.reduce(raw) { |name, rule| rule.declared_name(name) }
+
+ def member_name(name)
+ rules.reverse_each.reduce(name) { |member, rule| rule.member_name(member) }
+ end
+
+ private
+
+ attr_reader :rules
+ end
+end
diff --git a/lib/json_api_kit/glossary/casing_rule.rb b/lib/json_api_kit/glossary/casing_rule.rb
new file mode 100644
index 00000000000..ea250f81939
--- /dev/null
+++ b/lib/json_api_kit/glossary/casing_rule.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+module JsonApiKit
+ class Glossary
+ module CasingRule
+ CAMEL_BOUNDARY = /(?<before>[a-z\d])(?<capital>[A-Z])/
+ SNAKE_BOUNDARY = /_(?<letter>[a-z\d])/
+
+ class NotAMemberName < BadRequest
+ attr_reader :raw, :member
+
+ def initialize(raw, member, parameter: nil)
+ @raw = raw
+ @member = member
+ @parameter = parameter
+ super("Use #{member}, not #{raw}.")
+ end
+
+ def title = "Invalid member name"
+
+ def source = { parameter: @parameter }.compact
+
+ def at(parameter) = self.class.new(raw, member, parameter:)
+ end
+
+ class << self
+ def declared_name(raw)
+ name = raw.to_s.gsub(CAMEL_BOUNDARY, '\k<before>_\k<capital>').downcase
+ member = member_name(name)
+ raise NotAMemberName.new(raw, member) unless member == raw.to_s
+ name
+ end
+
+ def member_name(name)
+ name.to_s.gsub(SNAKE_BOUNDARY) { Regexp.last_match[:letter].upcase }
+ end
+ end
+ end
+ end
+end
diff --git a/lib/json_api_kit/request/contract/error.rb b/lib/json_api_kit/request/contract/error.rb
index beb51a249a9..3c07da50bf8 100644
--- a/lib/json_api_kit/request/contract/error.rb
+++ b/lib/json_api_kit/request/contract/error.rb
@@ -4,8 +4,9 @@ module JsonApiKit
class Request
class Contract
class Error < BadRequest
- def initialize(error, title:, detail: nil, source: nil, type: nil, meta: nil)
+ def initialize(error, glossary:, title:, detail: nil, source: nil, type: nil, meta: nil)
@error = error
+ @glossary = glossary
@title = title
@detail = detail
@source = source
@@ -20,18 +21,33 @@ module JsonApiKit
def detail = @detail&.call(self) || error.message
- def source = { parameter: @source&.call(self) || parameter }
+ def source = { parameter: (@source&.call(self) || parameter).to_s }
def meta = @meta&.call(self) || {}
def parameter
- name, *nested = error.attribute.to_s.split(".")
- nested.reduce(name) { |named, part| "#{named}[#{part}]" }
+ ParameterName.new(*error.attribute.to_s.split(".").map { member_name(it) })
end
+ def name = member_name(options[:name])
+
+ def key = member_name(options[:key])
+
+ def member_parameter(member = name) = parameter.member(member)
+
+ def window_parameters = base.page.window_names.map { page_parameter(it) }
+
+ def cursor_parameter = page_parameter(base.page.cursor_name)
+
+ def anchor_count = base.page.anchor.size
+
private
- attr_reader :error
+ attr_reader :error, :glossary
+
+ def member_name(name) = glossary.member_name(name)
+
+ def page_parameter(name) = parameter.family.member(member_name(name))
end
end
end
diff --git a/lib/json_api_kit/request/contract/mapper.rb b/lib/json_api_kit/request/contract/mapper.rb
index 7c1d803afcf..cf6c4a533c8 100644
--- a/lib/json_api_kit/request/contract/mapper.rb
+++ b/lib/json_api_kit/request/contract/mapper.rb
@@ -25,7 +25,7 @@ module JsonApiKit
title: "Window size is negative",
detail: ->(error) { "#{error.parameter} must be 0 or greater." },
}.freeze
- WINDOW_PARAMETER = ->(error) { error.base.page.window_names.first }
+ WINDOW_PARAMETER = ->(error) { error.window_parameters.first }
UNKNOWN_PARAMETER = {
title: "No such parameter",
detail: ->(error) { "There is no parameter named #{error.parameter}." },
@@ -34,36 +34,35 @@ module JsonApiKit
RULES = {
[:"page.anchor", :wrong_length] => {
title: "Too many anchors",
- detail: ->(error) do
- "Use one anchor only. This request has #{error.base.page.anchor.size}."
- end,
+ detail: ->(error) { "Use one anchor only. This request has #{error.anchor_count}." },
},
[:"page.anchor", :present] => {
title: "Anchor and cursor cannot be combined",
- detail: ->(error) do
- "Use page[anchor] or page[#{error.base.page.cursor_name}], not both."
- end,
+ detail: ->(error) { "Use page[anchor] or #{error.cursor_parameter}, not both." },
},
[:"page.anchor", :blank] => {
title: "Anchor is required",
- detail: ->(error) { "#{error.base.page.window_names.join(", ")} need an anchor." },
+ detail: ->(error) do
+ "#{error.window_parameters.join(", ")} " \
+ "need an anchor."
+ end,
},
[:"page.anchor", :no_such_name] => {
title: "No such anchor",
- detail: ->(error) { "There is no anchor named #{error.options[:name]}." },
- source: ->(error) { "page[anchor][#{error.options[:name]}]" },
+ detail: ->(error) { "There is no anchor named #{error.name}." },
+ source: ->(error) { error.member_parameter },
},
[:"page.anchor", :not_the_sort] => {
title: "Anchor does not match the sort",
detail: ->(error) do
- "The anchor is #{error.options[:name]}, but this request sorts by " \
- "#{error.options[:key]}."
+ "The anchor is #{error.name}, but this request sorts " \
+ "by #{error.key}."
end,
- source: ->(error) { "page[anchor][#{error.options[:name]}]" },
+ source: ->(error) { error.member_parameter },
},
%i[sort no_such_name] => {
title: "No such sort",
- detail: ->(error) { "There is no sort named #{error.options[:name]}." },
+ detail: ->(error) { "There is no sort named #{error.name}." },
},
%i[sort unknown_direction] => {
title: "No such sort direction",
@@ -71,12 +70,12 @@ module JsonApiKit
},
%i[filter no_such_name] => {
title: "No such filter",
- detail: ->(error) { "There is no filter named #{error.options[:name]}." },
- source: ->(error) { "filter[#{error.options[:name]}]" },
+ detail: ->(error) { "There is no filter named #{error.name}." },
+ source: ->(error) { error.member_parameter },
},
%i[include no_such_name] => {
title: "No such relationship path",
- detail: ->(error) { "There is no relationship path named #{error.options[:name]}." },
+ detail: ->(error) { "There is no relationship path named #{error.name}." },
},
%i[fields bad_shape] => {
title: "Invalid fields parameter",
@@ -84,12 +83,14 @@ module JsonApiKit
},
%i[fields bad_value] => {
title: "Invalid fields value",
- detail: ->(error) { "fields[#{error.options[:name]}] must be a list of field names." },
- source: ->(error) { "fields[#{error.options[:name]}]" },
+ detail: ->(error) do
+ "#{error.member_parameter(error.options[:name])} must be a list of field names."
+ end,
+ source: ->(error) { error.member_parameter(error.options[:name]) },
},
%i[sort bad_shape] => {
title: "Invalid sort parameter",
- detail: ->(_) { "sort must be a comma-separated list, as in sort=-created_at." },
+ detail: ->(_) { "sort must be a comma-separated list, as in sort=-createdAt." },
},
%i[filter bad_shape] => {
title: "Invalid filter parameter",
@@ -97,15 +98,13 @@ module JsonApiKit
},
%i[filter bad_value] => {
title: "Invalid filter value",
- detail: ->(error) do
- "filter[#{error.options[:name]}] must be a value or a list of values."
- end,
- source: ->(error) { "filter[#{error.options[:name]}]" },
+ detail: ->(error) { "#{error.member_parameter} must be a value or a list of values." },
+ source: ->(error) { error.member_parameter },
},
[:"page.anchor", :bad_value] => {
title: "Invalid anchor value",
- detail: ->(error) { "page[anchor][#{error.options[:name]}] must be a single value." },
- source: ->(error) { "page[anchor][#{error.options[:name]}]" },
+ detail: ->(error) { "#{error.member_parameter} must be a single value." },
+ source: ->(error) { error.member_parameter },
},
%i[page bad_shape] => {
title: "Invalid page parameter",
@@ -152,15 +151,16 @@ module JsonApiKit
[:"page.before", :unreadable_cursor] => INVALID_CURSOR,
}.freeze
- def initialize(errors)
+ def initialize(errors, glossary:)
@errors = errors
+ @glossary = glossary
end
- def to_a = errors.map { Error.new(it, **rule_for(it)) }
+ def to_a = errors.map { Error.new(it, glossary:, **rule_for(it)) }
private
- attr_reader :errors
+ attr_reader :errors, :glossary
def rule_for(error) = RULES.fetch([error.attribute, error.type], UNKNOWN_PARAMETER)
end
diff --git a/lib/json_api_kit/request/contract/paging.rb b/lib/json_api_kit/request/contract/paging.rb
index ee1a1321f4e..a3997aab18e 100644
--- a/lib/json_api_kit/request/contract/paging.rb
+++ b/lib/json_api_kit/request/contract/paging.rb
@@ -69,7 +69,7 @@ module JsonApiKit
def window? = window_names.present?
def window_names
- { before_size:, after_size:, include_anchor: }.compact.keys.map { "page[#{it}]" }
+ { before_size:, after_size:, include_anchor: }.compact.keys
end
private
diff --git a/lib/json_api_kit/request/parameter_name.rb b/lib/json_api_kit/request/parameter_name.rb
new file mode 100644
index 00000000000..15eb53f3d6d
--- /dev/null
+++ b/lib/json_api_kit/request/parameter_name.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+module JsonApiKit
+ class Request
+ class ParameterName
+ def initialize(*parts)
+ @parts = parts.map(&:to_s)
+ end
+
+ def family = self.class.new(parts.first)
+
+ def member(part) = self.class.new(*parts, part)
+
+ def to_s = "#{parts.first}#{parts.drop(1).map { "[#{it}]" }.join}"
+
+ private
+
+ attr_reader :parts
+ end
+ end
+end
diff --git a/lib/json_api_kit/request/parameters.rb b/lib/json_api_kit/request/parameters.rb
new file mode 100644
index 00000000000..85653547a15
--- /dev/null
+++ b/lib/json_api_kit/request/parameters.rb
@@ -0,0 +1,59 @@
+# frozen_string_literal: true
+
+module JsonApiKit
+ class Request
+ class Parameters
+ FIELDSETS_AT = %w[fields].freeze
+ NAMES_AT = [%w[sort], %w[include], %w[page anchor]].freeze
+
+ def initialize(raw, glossary:)
+ @raw = raw
+ @glossary = glossary
+ end
+
+ def to_h = declared_names(raw, [])
+
+ private
+
+ attr_reader :raw, :glossary
+
+ def declared_names(parameters, path)
+ parameters.to_h do |name, value|
+ key = declared_at(name, path + [name.to_s])
+ [key, declared_value(value, path + [key])]
+ end
+ end
+
+ def declared_value(value, path)
+ return fieldsets(value, path) if path == FIELDSETS_AT
+ return names(value, path) if path.in?(NAMES_AT)
+ return declared_names(value, path) if value.is_a?(Hash)
+ value
+ end
+
+ def fieldsets(value, path)
+ return value unless value.is_a?(Hash)
+ value.to_h { |type, fields| [type, names(fields, path + [type.to_s])] }
+ end
+
+ def names(value, path)
+ case value
+ when Hash
+ value.transform_keys { declared_at(it, path + [it.to_s]) }
+ when Array
+ value.map { names(it, path) }
+ when String, Symbol
+ declared_at(value, path)
+ else
+ value
+ end
+ end
+
+ def declared_at(raw, path)
+ glossary.declared_name(raw)
+ rescue Glossary::CasingRule::NotAMemberName => error
+ raise error.at(ParameterName.new(*path).to_s)
+ end
+ end
+ end
+end
diff --git a/lib/json_api_kit/timeline.rb b/lib/json_api_kit/timeline.rb
index e235fb3ae40..930dba58dd6 100644
--- a/lib/json_api_kit/timeline.rb
+++ b/lib/json_api_kit/timeline.rb
@@ -3,7 +3,7 @@
module JsonApiKit
class Timeline
# The day this API was first released.
- FIRST_RELEASE = "2026-08-31"
+ FIRST_RELEASE = ApiVersion.parse("2026-08-31")
class << self
delegate :first, :current, :resolve, to: :core
@@ -13,8 +13,8 @@ module JsonApiKit
def core = @core ||= new([FIRST_RELEASE])
end
- def initialize(dates)
- @versions = dates.map { ApiVersion.parse(it) }.sort
+ def initialize(versions)
+ @versions = versions.sort
end
def first = versions.first
diff --git a/lib/json_api_kit/url.rb b/lib/json_api_kit/url.rb
index c9598ae31e3..b6a52ea04d3 100644
--- a/lib/json_api_kit/url.rb
+++ b/lib/json_api_kit/url.rb
@@ -4,8 +4,12 @@ module JsonApiKit
class Url
PAGE = "page"
- ENDS = %w[after before].freeze
- WINDOW = %w[anchor before_size after_size include_anchor].freeze
+ KIT = Glossary.kit
+ REPLACED_MEMBERS =
+ %w[after before anchor before_size after_size include_anchor]
+ .map { KIT.member_name(it) }
+ .freeze
+ .freeze
def initialize(address, parameters = {})
@address = address
@@ -16,7 +20,7 @@ module JsonApiKit
self.class.new(
address,
parameters.merge(
- PAGE => page_parameters.except(*ENDS, *WINDOW).merge(page.transform_keys(&:to_s)),
+ PAGE => page_parameters.except(*REPLACED_MEMBERS).merge(member_names(page)),
),
)
end
@@ -29,6 +33,8 @@ module JsonApiKit
def page_parameters = parameters.fetch(PAGE, {})
+ def member_names(page) = page.transform_keys { KIT.member_name(it) }
+
def query_string = parameters.to_query.presence&.then { "?#{it}" }
end
end
diff --git a/plugins/discourse-data-explorer/spec/requests/api/queries_spec.rb b/plugins/discourse-data-explorer/spec/requests/api/queries_spec.rb
index 318216baffc..09517e0d53d 100644
--- a/plugins/discourse-data-explorer/spec/requests/api/queries_spec.rb
+++ b/plugins/discourse-data-explorer/spec/requests/api/queries_spec.rb
@@ -80,10 +80,10 @@ RSpec.describe "JSON:API queries", type: :request do
"name" => query.name,
"description" => query.description,
"sql" => query.sql,
- "param_info" => [],
- "is_default" => false,
- "created_at" => query.created_at.as_json,
- "last_run_at" => query.last_run_at.as_json,
+ "paramInfo" => [],
+ "isDefault" => false,
+ "createdAt" => query.created_at.as_json,
+ "lastRunAt" => query.last_run_at.as_json,
},
"links" => {
"self" => "#{base}/queries/#{query.id}",
@@ -178,10 +178,10 @@ RSpec.describe "JSON:API queries", type: :request do
"name" => middle.name,
"description" => middle.description,
"sql" => middle.sql,
- "param_info" => [],
- "is_default" => false,
- "created_at" => middle.created_at.as_json,
- "last_run_at" => middle.last_run_at.as_json,
+ "paramInfo" => [],
+ "isDefault" => false,
+ "createdAt" => middle.created_at.as_json,
+ "lastRunAt" => middle.last_run_at.as_json,
},
"links" => {
"self" => "#{base}/queries/#{middle.id}",
diff --git a/spec/integration/json_api_kit/anchors_spec.rb b/spec/integration/json_api_kit/anchors_spec.rb
index 4e31dfd1763..16d903f9d2c 100644
--- a/spec/integration/json_api_kit/anchors_spec.rb
+++ b/spec/integration/json_api_kit/anchors_spec.rb
@@ -15,14 +15,14 @@ RSpec.describe "a listing entered at an anchor" do
let(:params) do
{
sort: {
- created_at: :asc,
+ createdAt: :asc,
},
page: {
anchor: {
id: middle.id,
},
- before_size: 1,
- after_size: 1,
+ beforeSize: 1,
+ afterSize: 1,
},
fields: {
topics: [:title],
@@ -49,13 +49,13 @@ RSpec.describe "a listing entered at an anchor" do
describe "the links either side of the window" do
let(:query) do
{
- "sort" => "created_at",
+ "sort" => "createdAt",
"page" => {
"anchor" => {
"id" => middle.id.to_s,
},
- "before_size" => "1",
- "after_size" => "1",
+ "beforeSize" => "1",
+ "afterSize" => "1",
},
}
end
@@ -76,7 +76,7 @@ RSpec.describe "a listing entered at an anchor" do
let(:params) do
{
sort: {
- created_at: :asc,
+ createdAt: :asc,
},
page: {
anchor: {
@@ -107,11 +107,11 @@ RSpec.describe "a listing entered at an anchor" do
let(:params) do
{
sort: {
- created_at: :asc,
+ createdAt: :asc,
},
page: {
anchor: {
- created_at: Time.utc(2026, 8, 2),
+ createdAt: Time.utc(2026, 8, 2),
},
size: 2,
},
@@ -138,11 +138,11 @@ RSpec.describe "a listing entered at an anchor" do
let(:params) do
{
sort: {
- created_at: :asc,
+ createdAt: :asc,
},
page: {
anchor: {
- created_at: Time.utc(2026, 8, 1, 12),
+ createdAt: Time.utc(2026, 8, 1, 12),
},
size: 2,
},
@@ -173,11 +173,11 @@ RSpec.describe "a listing entered at an anchor" do
let(:params) do
{
sort: {
- last_posted_at: :asc,
+ lastPostedAt: :asc,
},
page: {
anchor: {
- last_posted_at: Time.utc(2030, 1, 1),
+ lastPostedAt: Time.utc(2030, 1, 1),
},
size: 1,
},
@@ -241,7 +241,7 @@ RSpec.describe "a listing entered at an anchor" do
context "when the server calculates the anchor" do
let(:guardian) { Guardian.new(middle.user) }
let(:params) do
- { sort: { created_at: :asc }, page: { anchor: :mine, size: 1 }, fields: { topics: [:title] } }
+ { sort: { createdAt: :asc }, page: { anchor: :mine, size: 1 }, fields: { topics: [:title] } }
end
it "starts at the row it finds for the current user" do
@@ -271,7 +271,7 @@ RSpec.describe "a listing entered at an anchor" do
end
context "when the anchor is an id no row holds" do
- let(:params) { { sort: { created_at: :asc }, page: { anchor: { id: -1 } } } }
+ let(:params) { { sort: { createdAt: :asc }, page: { anchor: { id: -1 } } } }
it "renders the error as a document" do
expect(document).to eq(
@@ -287,14 +287,14 @@ RSpec.describe "a listing entered at an anchor" do
end
context "when the anchor is on another sort" do
- let(:params) { { sort: { created_at: :asc }, page: { anchor: { title: middle.title } } } }
+ let(:params) { { sort: { createdAt: :asc }, page: { anchor: { title: middle.title } } } }
it "renders the error as a document" do
expect(document).to eq(
errors: [
refusal(
title: "Anchor does not match the sort",
- detail: "The anchor is title, but this request sorts by created_at.",
+ detail: "The anchor is title, but this request sorts by createdAt.",
parameter: "page[anchor][title]",
),
],
@@ -307,14 +307,14 @@ RSpec.describe "a listing entered at an anchor" do
let(:params) do
{
sort: {
- created_at: :asc,
+ createdAt: :asc,
},
page: {
anchor: {
id: middle.id,
},
- before_size: 3_000,
- after_size: 3_000,
+ beforeSize: 3_000,
+ afterSize: 3_000,
},
}
end
@@ -325,7 +325,7 @@ RSpec.describe "a listing entered at an anchor" do
refusal(
title: "Window is too large",
detail: "A window of 6001 rows exceeds the maximum of 100.",
- parameter: "page[before_size]",
+ parameter: "page[beforeSize]",
links: {
type: profile_link("max-size-exceeded"),
},
@@ -342,16 +342,7 @@ RSpec.describe "a listing entered at an anchor" do
context "when the window is empty" do
let(:params) do
- {
- page: {
- anchor: {
- id: middle.id,
- },
- before_size: 0,
- after_size: 0,
- include_anchor: false,
- },
- }
+ { page: { anchor: { id: middle.id }, beforeSize: 0, afterSize: 0, includeAnchor: false } }
end
it "renders the error as a document" do
@@ -360,7 +351,7 @@ RSpec.describe "a listing entered at an anchor" do
refusal(
title: "Window is empty",
detail: "A window must be at least 1 row.",
- parameter: "page[before_size]",
+ parameter: "page[beforeSize]",
),
],
)
@@ -368,15 +359,15 @@ RSpec.describe "a listing entered at an anchor" do
end
context "when a side of the window is negative" do
- let(:params) { { page: { anchor: { id: middle.id }, before_size: -1, after_size: 2 } } }
+ let(:params) { { page: { anchor: { id: middle.id }, beforeSize: -1, afterSize: 2 } } }
it "renders the error as a document" do
expect(document).to eq(
errors: [
refusal(
title: "Window size is negative",
- detail: "page[before_size] must be 0 or greater.",
- parameter: "page[before_size]",
+ detail: "page[beforeSize] must be 0 or greater.",
+ parameter: "page[beforeSize]",
),
],
)
@@ -384,15 +375,15 @@ RSpec.describe "a listing entered at an anchor" do
end
context "when a side of the window is not an integer" do
- let(:params) { { page: { anchor: { id: middle.id }, after_size: "two" } } }
+ let(:params) { { page: { anchor: { id: middle.id }, afterSize: "two" } } }
it "renders the error as a document" do
expect(document).to eq(
errors: [
refusal(
title: "Window size is not an integer",
- detail: "page[after_size] must be an integer.",
- parameter: "page[after_size]",
+ detail: "page[afterSize] must be an integer.",
+ parameter: "page[afterSize]",
),
],
)
diff --git a/spec/integration/json_api_kit/casing_spec.rb b/spec/integration/json_api_kit/casing_spec.rb
new file mode 100644
index 00000000000..5612dabd3d3
--- /dev/null
+++ b/spec/integration/json_api_kit/casing_spec.rb
@@ -0,0 +1,132 @@
+# frozen_string_literal: true
+
+require_relative "support"
+
+module JsonApiKitSpec
+ class CasedTopicsController < JsonApiKit::BaseController
+ resource :topics
+ end
+end
+
+RSpec.describe "JSON:API member names", type: :request do
+ include_context "with a listing of topics"
+
+ let(:path) { "/api/topics" }
+ let(:query) { {} }
+ let(:parsed_body) { JSON.parse(response.body) }
+ let(:error) { parsed_body["errors"].sole }
+ let(:attributes) { parsed_body["data"].first["attributes"] }
+ let(:ids) { parsed_body["data"].map { it["id"] } }
+
+ before do
+ Rails.application.routes.disable_clear_and_finalize = true
+ Rails.application.routes.draw { get "/api/topics" => "json_api_kit_spec/cased_topics#index" }
+ Rails.application.routes.disable_clear_and_finalize = false
+ get path, headers: { "HTTP_API_VERSION" => JsonApiKit::Timeline.current.to_s }, params: query
+ end
+
+ after { Rails.application.reload_routes! }
+
+ it "sends attribute names in camel case" do
+ expect(attributes.keys).to contain_exactly("title", "createdAt")
+ end
+
+ context "when the request selects fields" do
+ let(:query) { { "fields" => { "topics" => "createdAt" } } }
+
+ it "sends that field only" do
+ expect(attributes.keys).to contain_exactly("createdAt")
+ end
+ end
+
+ context "when the request sorts" do
+ let(:query) { { "sort" => "createdAt" } }
+
+ it "orders the rows by that attribute" do
+ expect(ids).to eq([oldest.id.to_s, middle.id.to_s, newest.id.to_s])
+ end
+ end
+
+ context "when the request uses snake case" do
+ let(:query) { { "sort" => "created_at" } }
+
+ it { expect(response).to have_http_status(:bad_request) }
+
+ it "sends the reason" do
+ expect(error).to eq(
+ refusal(
+ title: "Invalid member name",
+ detail: "Use createdAt, not created_at.",
+ parameter: "sort",
+ ).deep_stringify_keys,
+ )
+ end
+ end
+
+ context "when the request anchors a window on an attribute" do
+ let(:query) do
+ {
+ "sort" => "createdAt",
+ "page" => {
+ "anchor" => {
+ "createdAt" => middle.created_at.iso8601(6),
+ },
+ "beforeSize" => "1",
+ "afterSize" => "0",
+ },
+ }
+ end
+
+ it "sends the rows of the window" do
+ expect(ids).to eq([oldest.id.to_s, middle.id.to_s])
+ end
+ end
+
+ context "when the request anchors on a computed position" do
+ let(:query) { { "page" => { "anchor" => "withoutReplies", "afterSize" => "1" } } }
+
+ it "sends the rows from that position" do
+ expect(ids).to eq([newest.id.to_s, middle.id.to_s])
+ end
+ end
+
+ context "when the request asks for the cursor of each row" do
+ let(:query) { { "page" => { "itemCursors" => "true" } } }
+
+ it "sends a cursor on every row" do
+ expect(parsed_body["data"].map { it.dig("meta", "page", "cursor") }).to all(be_present)
+ end
+ end
+
+ context "when the request includes a relationship" do
+ let(:query) { { "include" => "orderedPosts" } }
+ let(:relationship) { parsed_body["data"].first["relationships"]["orderedPosts"] }
+
+ it "sends the relationship under its camel case name" do
+ expect(relationship).to be_present
+ end
+
+ it "sends its links under that name" do
+ expect(relationship["links"]["related"]).to end_with("/orderedPosts")
+ end
+ end
+
+ context "when a page parameter is invalid" do
+ let(:query) do
+ {
+ "page" => {
+ "anchor" => {
+ "createdAt" => middle.created_at.iso8601(6),
+ },
+ "beforeSize" => "-1",
+ },
+ }
+ end
+
+ let(:parameters) { parsed_body["errors"].map { it.dig("source", "parameter") } }
+
+ it "sends the parameter in camel case" do
+ expect(parameters).to include("page[beforeSize]")
+ end
+ end
+end
diff --git a/spec/integration/json_api_kit/cursors_spec.rb b/spec/integration/json_api_kit/cursors_spec.rb
index 7ae480687b8..a6ed6493b08 100644
--- a/spec/integration/json_api_kit/cursors_spec.rb
+++ b/spec/integration/json_api_kit/cursors_spec.rb
@@ -5,9 +5,9 @@ require_relative "support"
RSpec.describe "a listing read from a cursor" do
include_context "with a listing of topics"
- let(:sort) { { created_at: :asc } }
+ let(:sort) { { createdAt: :asc } }
let(:cursor) { cursor_for(middle) }
- let(:sorted_listing) { listing_of({ sort:, page: { item_cursors: true } }) }
+ let(:sorted_listing) { listing_of({ sort:, page: { itemCursors: true } }) }
describe "the cursor of a row" do
it "is left out unless the request asks for it" do
@@ -69,8 +69,8 @@ RSpec.describe "a listing read from a cursor" do
end
end
- context "when the sort is created_at desc" do
- let(:sort) { { created_at: :desc } }
+ context "when the sort is createdAt desc" do
+ let(:sort) { { createdAt: :desc } }
let(:params) { { sort:, page: { after: cursor }, fields: { topics: [:title] } } }
it "returns the rows after the cursor" do
@@ -84,7 +84,7 @@ RSpec.describe "a listing read from a cursor" do
context "when the cursor comes from a backwards page" do
let(:cursor) do
- cursor_at(0, listing_of({ sort:, page: { before: super(), item_cursors: true } }))
+ cursor_at(0, listing_of({ sort:, page: { before: super(), itemCursors: true } }))
end
let(:params) { { sort:, page: { after: cursor }, fields: { topics: [:title] } } }
@@ -110,14 +110,14 @@ RSpec.describe "a listing read from a cursor" do
end
end
- context "when the sort is last_posted_at and one row holds none" do
+ context "when the sort is lastPostedAt and one row holds none" do
fab!(:never_posted) do
Fabricate(:topic, title: "Nothing has been posted here", last_posted_at: nil)
end
before { [oldest, middle, newest].each { it.update_columns(last_posted_at: it.created_at) } }
- let(:sort) { { last_posted_at: :asc } }
+ let(:sort) { { lastPostedAt: :asc } }
context "when the cursor comes from the last row holding a value" do
let(:cursor) { cursor_for(newest) }
@@ -149,10 +149,10 @@ RSpec.describe "a listing read from a cursor" do
end
end
- context "when the request sorts by created_at" do
+ context "when the request sorts by createdAt" do
let(:cursor) { cursor_for(never_posted) }
let(:params) do
- { sort: { created_at: :asc }, page: { after: cursor }, fields: { topics: [:title] } }
+ { sort: { createdAt: :asc }, page: { after: cursor }, fields: { topics: [:title] } }
end
it "refuses the cursor" do
@@ -169,7 +169,7 @@ RSpec.describe "a listing read from a cursor" do
context "when the cursor comes from another sort of the same shape" do
let(:cursor) { cursor_of_record(middle, sort: { title: :asc }) }
- let(:params) { { sort: { created_at: :asc }, page: { after: cursor } } }
+ let(:params) { { sort: { createdAt: :asc }, page: { after: cursor } } }
it "refuses the cursor" do
expect(rendered_error).to eq(
diff --git a/spec/integration/json_api_kit/documents_spec.rb b/spec/integration/json_api_kit/documents_spec.rb
index 2b0000caa55..03ee4b97ff8 100644
--- a/spec/integration/json_api_kit/documents_spec.rb
+++ b/spec/integration/json_api_kit/documents_spec.rb
@@ -6,7 +6,7 @@ RSpec.describe "a rendered document" do
include_context "with a listing of topics"
describe "a listing" do
- let(:params) { { sort: { created_at: :asc }, page: { size: 1 } } }
+ let(:params) { { sort: { createdAt: :asc }, page: { size: 1 } } }
let(:query) { { "page" => { "size" => "1" } } }
it "renders the first page of the listing" do
@@ -20,7 +20,7 @@ RSpec.describe "a rendered document" do
context "when all parameters are strings" do
let(:params) do
{
- "sort" => "created_at",
+ "sort" => "createdAt",
"include" => "user",
"fields" => {
"topics" => "title,user",
@@ -55,7 +55,7 @@ RSpec.describe "a rendered document" do
end
context "when the page holds the last row of the listing" do
- let(:params) { { sort: { created_at: :asc }, page: { size: 5 } } }
+ let(:params) { { sort: { createdAt: :asc }, page: { size: 5 } } }
let(:query) { { "page" => { "size" => "5" } } }
it "links to no page after it" do
@@ -69,7 +69,7 @@ RSpec.describe "a rendered document" do
context "when the page follows another page" do
let(:params) do
- { sort: { created_at: :asc }, page: { size: 1, after: cursor_of_record(oldest) } }
+ { sort: { createdAt: :asc }, page: { size: 1, after: cursor_of_record(oldest) } }
end
let(:query) { { "page" => { "size" => "1", "after" => cursor_of_record(oldest) } } }
@@ -88,7 +88,7 @@ RSpec.describe "a rendered document" do
context "when the listing holds no row" do
let(:params) do
- { sort: { created_at: :asc }, filter: { title: "No topic carries this title" } }
+ { sort: { createdAt: :asc }, filter: { title: "No topic carries this title" } }
end
let(:query) { {} }
@@ -165,7 +165,7 @@ RSpec.describe "a rendered document" do
describe "the status of a document" do
subject(:status) do
- JsonApiKit::Document::Collection.for(params, resource:, guardian:, urls:).status
+ JsonApiKit::Document::Collection.for(params, resource:, guardian:, urls:, glossary:).status
end
it "answers 200 for a rendered listing" do
@@ -174,7 +174,14 @@ RSpec.describe "a rendered document" do
context "when the document holds one record" do
subject(:status) do
- JsonApiKit::Document::Individual.for(middle.id, params, resource:, guardian:, urls:).status
+ JsonApiKit::Document::Individual.for(
+ middle.id,
+ params,
+ resource:,
+ guardian:,
+ urls:,
+ glossary:,
+ ).status
end
it "answers 200 for a rendered record" do
@@ -184,7 +191,14 @@ RSpec.describe "a rendered document" do
context "when no record has that id" do
subject(:status) do
- JsonApiKit::Document::Individual.for(-1, params, resource:, guardian:, urls:).status
+ JsonApiKit::Document::Individual.for(
+ -1,
+ params,
+ resource:,
+ guardian:,
+ urls:,
+ glossary:,
+ ).status
end
it "answers 404 for a not found error" do
diff --git a/spec/integration/json_api_kit/endpoint_spec.rb b/spec/integration/json_api_kit/endpoint_spec.rb
index 8b76b5b6964..06d9be62eec 100644
--- a/spec/integration/json_api_kit/endpoint_spec.rb
+++ b/spec/integration/json_api_kit/endpoint_spec.rb
@@ -132,13 +132,13 @@ RSpec.describe "a JSON:API endpoint", type: :request do
context "when the request anchors the page on one row" do
let(:query) do
{
- "sort" => "created_at",
+ "sort" => "createdAt",
"page" => {
"anchor" => {
"id" => oldest.id.to_s,
},
- "before_size" => "0",
- "after_size" => "1",
+ "beforeSize" => "0",
+ "afterSize" => "1",
},
}
end
diff --git a/spec/integration/json_api_kit/errors_spec.rb b/spec/integration/json_api_kit/errors_spec.rb
index 5a8e40f18af..8c11a1666c5 100644
--- a/spec/integration/json_api_kit/errors_spec.rb
+++ b/spec/integration/json_api_kit/errors_spec.rb
@@ -9,14 +9,14 @@ RSpec.describe "a refused request" do
describe "the sort parameter" do
context "when the sort is a list" do
- let(:params) { { sort: %w[created_at] } }
+ let(:params) { { sort: %w[createdAt] } }
it "refuses the sort" do
expect(document).to eq(
errors: [
refusal(
title: "Invalid sort parameter",
- detail: "sort must be a comma-separated list, as in sort=-created_at.",
+ detail: "sort must be a comma-separated list, as in sort=-createdAt.",
parameter: "sort",
),
],
@@ -57,13 +57,19 @@ RSpec.describe "a refused request" do
it "answers the status 400" do
expect(
- JsonApiKit::Document::Collection.for(params, resource:, guardian:, urls:).status,
+ JsonApiKit::Document::Collection.for(
+ params,
+ resource:,
+ guardian:,
+ urls:,
+ glossary:,
+ ).status,
).to eq("400")
end
end
context "when the sort direction is neither asc nor desc" do
- let(:params) { { sort: { created_at: :sideways } } }
+ let(:params) { { sort: { createdAt: :sideways } } }
it "refuses the sort" do
expect(document).to eq(
@@ -79,7 +85,7 @@ RSpec.describe "a refused request" do
end
context "when an unknown sort direction comes with an anchor" do
- let(:params) { { sort: { created_at: :sideways }, page: { anchor: { id: middle.id } } } }
+ let(:params) { { sort: { createdAt: :sideways }, page: { anchor: { id: middle.id } } } }
it "refuses the sort alone" do
expect(document).to eq(
@@ -95,7 +101,7 @@ RSpec.describe "a refused request" do
end
context "when an unknown sort direction comes with a cursor" do
- let(:params) { { sort: { created_at: :sideways }, page: { after: cursor } } }
+ let(:params) { { sort: { createdAt: :sideways }, page: { after: cursor } } }
it "refuses the sort alone" do
expect(document).to eq(
@@ -111,14 +117,14 @@ RSpec.describe "a refused request" do
end
context "when a sort sent as a list comes with an anchor" do
- let(:params) { { sort: %w[created_at], page: { anchor: { id: middle.id } } } }
+ let(:params) { { sort: %w[createdAt], page: { anchor: { id: middle.id } } } }
it "refuses the sort alone" do
expect(document).to eq(
errors: [
refusal(
title: "Invalid sort parameter",
- detail: "sort must be a comma-separated list, as in sort=-created_at.",
+ detail: "sort must be a comma-separated list, as in sort=-createdAt.",
parameter: "sort",
),
],
@@ -159,7 +165,7 @@ RSpec.describe "a refused request" do
end
context "when a request for one record has a sort" do
- let(:params) { { sort: { created_at: :asc } } }
+ let(:params) { { sort: { createdAt: :asc } } }
it "refuses the sort" do
expect(one_document(middle.id)).to eq(
@@ -295,7 +301,7 @@ RSpec.describe "a refused request" do
describe "an unknown parameter" do
context "when the parameter is at the top level" do
- let(:params) { { sorts: { created_at: :asc } } }
+ let(:params) { { sorts: { createdAt: :asc } } }
it "refuses the parameter" do
expect(document).to eq(
@@ -420,7 +426,7 @@ RSpec.describe "a refused request" do
let(:params) do
{
sort: {
- created_at: :asc,
+ createdAt: :asc,
},
page: {
after: cursor_of_record(middle, sort: { title: :asc }),
@@ -464,14 +470,14 @@ RSpec.describe "a refused request" do
describe "the window parameters" do
context "when the request has no anchor" do
- let(:params) { { page: { before_size: 2, after_size: 2 } } }
+ let(:params) { { page: { beforeSize: 2, afterSize: 2 } } }
it "refuses the window" do
expect(document).to eq(
errors: [
refusal(
title: "Anchor is required",
- detail: "page[before_size], page[after_size] need an anchor.",
+ detail: "page[beforeSize], page[afterSize] need an anchor.",
parameter: "page[anchor]",
),
],
@@ -483,14 +489,14 @@ RSpec.describe "a refused request" do
let(:params) do
{
sort: {
- created_at: :asc,
+ createdAt: :asc,
},
page: {
anchor: {
id: middle.id,
},
- before_size: 3_000,
- after_size: 3_000,
+ beforeSize: 3_000,
+ afterSize: 3_000,
},
}
end
@@ -501,7 +507,7 @@ RSpec.describe "a refused request" do
refusal(
title: "Window is too large",
detail: "A window of 6001 rows exceeds the maximum of 100.",
- parameter: "page[before_size]",
+ parameter: "page[beforeSize]",
links: {
type: profile_link("max-size-exceeded"),
},
@@ -518,16 +524,7 @@ RSpec.describe "a refused request" do
context "when the window is empty" do
let(:params) do
- {
- page: {
- anchor: {
- id: middle.id,
- },
- before_size: 0,
- after_size: 0,
- include_anchor: false,
- },
- }
+ { page: { anchor: { id: middle.id }, beforeSize: 0, afterSize: 0, includeAnchor: false } }
end
it "refuses the window" do
@@ -536,7 +533,7 @@ RSpec.describe "a refused request" do
refusal(
title: "Window is empty",
detail: "A window must be at least 1 row.",
- parameter: "page[before_size]",
+ parameter: "page[beforeSize]",
),
],
)
@@ -544,15 +541,15 @@ RSpec.describe "a refused request" do
end
context "when a side of the window is negative" do
- let(:params) { { page: { anchor: { id: middle.id }, before_size: -1, after_size: 2 } } }
+ let(:params) { { page: { anchor: { id: middle.id }, beforeSize: -1, afterSize: 2 } } }
it "refuses the window" do
expect(document).to eq(
errors: [
refusal(
title: "Window size is negative",
- detail: "page[before_size] must be 0 or greater.",
- parameter: "page[before_size]",
+ detail: "page[beforeSize] must be 0 or greater.",
+ parameter: "page[beforeSize]",
),
],
)
@@ -560,15 +557,15 @@ RSpec.describe "a refused request" do
end
context "when a side of the window is not a number" do
- let(:params) { { page: { anchor: { id: middle.id }, after_size: "two" } } }
+ let(:params) { { page: { anchor: { id: middle.id }, afterSize: "two" } } }
it "refuses the window" do
expect(document).to eq(
errors: [
refusal(
title: "Window size is not an integer",
- detail: "page[after_size] must be an integer.",
- parameter: "page[after_size]",
+ detail: "page[afterSize] must be an integer.",
+ parameter: "page[afterSize]",
),
],
)
@@ -626,7 +623,7 @@ RSpec.describe "a refused request" do
end
context "when the anchor value is not a number" do
- let(:params) { { sort: { created_at: :asc }, page: { anchor: { id: "not a number" } } } }
+ let(:params) { { sort: { createdAt: :asc }, page: { anchor: { id: "not a number" } } } }
it "refuses the anchor value" do
expect(document).to eq(
@@ -642,7 +639,7 @@ RSpec.describe "a refused request" do
end
context "when no row has the anchor id" do
- let(:params) { { sort: { created_at: :asc }, page: { anchor: { id: -1 } } } }
+ let(:params) { { sort: { createdAt: :asc }, page: { anchor: { id: -1 } } } }
it "refuses the anchor value" do
expect(document).to eq(
@@ -658,14 +655,14 @@ RSpec.describe "a refused request" do
end
context "when the anchor is not the sort" do
- let(:params) { { sort: { created_at: :asc }, page: { anchor: { title: middle.title } } } }
+ let(:params) { { sort: { createdAt: :asc }, page: { anchor: { title: middle.title } } } }
it "refuses the anchor" do
expect(document).to eq(
errors: [
refusal(
title: "Anchor does not match the sort",
- detail: "The anchor is title, but this request sorts by created_at.",
+ detail: "The anchor is title, but this request sorts by createdAt.",
parameter: "page[anchor][title]",
),
],
diff --git a/spec/integration/json_api_kit/filtering_spec.rb b/spec/integration/json_api_kit/filtering_spec.rb
index 675925c81cf..c897e6890d0 100644
--- a/spec/integration/json_api_kit/filtering_spec.rb
+++ b/spec/integration/json_api_kit/filtering_spec.rb
@@ -20,7 +20,7 @@ RSpec.describe "a filtered listing" do
end
context "when the filter value is a list" do
- let(:params) { { sort: { created_at: :asc }, filter: { title: [oldest.title, newest.title] } } }
+ let(:params) { { sort: { createdAt: :asc }, filter: { title: [oldest.title, newest.title] } } }
it "keeps a row for each value of the list" do
expect(document).to eq(
diff --git a/spec/integration/json_api_kit/pagination_spec.rb b/spec/integration/json_api_kit/pagination_spec.rb
index 271f763f379..d2153d407e1 100644
--- a/spec/integration/json_api_kit/pagination_spec.rb
+++ b/spec/integration/json_api_kit/pagination_spec.rb
@@ -5,7 +5,7 @@ require_relative "support"
RSpec.describe "a page of a listing" do
include_context "with a listing of topics"
- let(:params) { { sort: { created_at: :asc }, page: { size: 2 } } }
+ let(:params) { { sort: { createdAt: :asc }, page: { size: 2 } } }
let(:query) { { "page" => { "size" => "2" } } }
it "returns the first page and links to the next" do
@@ -20,7 +20,7 @@ RSpec.describe "a page of a listing" do
end
context "when the page size holds the whole listing" do
- let(:params) { { sort: { created_at: :asc }, page: { size: 5 } } }
+ let(:params) { { sort: { createdAt: :asc }, page: { size: 5 } } }
let(:query) { { "page" => { "size" => "5" } } }
it "returns every row and links to no other page" do
@@ -51,7 +51,7 @@ RSpec.describe "a page of a listing" do
context "when the page follows the first page" do
let(:cursor) { cursor_of_record(oldest, sort: { created_at: :asc }) }
- let(:params) { { sort: { created_at: :asc }, page: { size: 1, after: cursor } } }
+ let(:params) { { sort: { createdAt: :asc }, page: { size: 1, after: cursor } } }
let(:query) { { "page" => { "size" => "1", "after" => cursor } } }
it "returns the page and links either side of it" do
diff --git a/spec/integration/json_api_kit/relationships_spec.rb b/spec/integration/json_api_kit/relationships_spec.rb
index d3c222c6a8b..e7484bb71ea 100644
--- a/spec/integration/json_api_kit/relationships_spec.rb
+++ b/spec/integration/json_api_kit/relationships_spec.rb
@@ -391,7 +391,7 @@ RSpec.describe "a document with related records" do
context "when the page is centred on an anchor" do
let(:params) do
- { include: %w[user], page: { anchor: { id: topic.id }, before_size: 1, after_size: 1 } }
+ { include: %w[user], page: { anchor: { id: topic.id }, beforeSize: 1, afterSize: 1 } }
end
it "reads them in one query" do
diff --git a/spec/integration/json_api_kit/restricted_fields_spec.rb b/spec/integration/json_api_kit/restricted_fields_spec.rb
index 321c718d082..57ba2f1e133 100644
--- a/spec/integration/json_api_kit/restricted_fields_spec.rb
+++ b/spec/integration/json_api_kit/restricted_fields_spec.rb
@@ -33,7 +33,7 @@ RSpec.describe "a restricted field" do
end
context "when the field is explicitly requested" do
- let(:params) { { fields: { topics: %w[title created_at] } } }
+ let(:params) { { fields: { topics: %w[title createdAt] } } }
it "does not render it" do
expect(document).to eq(title_only)
@@ -67,11 +67,11 @@ RSpec.describe "a restricted field" do
let(:guardian) { middle.user.guardian }
context "when using sparse fieldsets excluding the one used in the rule" do
- let(:params) { { fields: { topics: %w[title created_at] } } }
+ let(:params) { { fields: { topics: %w[title createdAt] } } }
it "does not crash" do
expect(document[:data].map { it[:attributes].keys }).to eq(
- [%w[title], %w[title created_at], %w[title]],
+ [%w[title], %w[title createdAt], %w[title]],
)
end
end
diff --git a/spec/integration/json_api_kit/sorting_spec.rb b/spec/integration/json_api_kit/sorting_spec.rb
index ee721251117..14a861b0e7b 100644
--- a/spec/integration/json_api_kit/sorting_spec.rb
+++ b/spec/integration/json_api_kit/sorting_spec.rb
@@ -5,7 +5,7 @@ require_relative "support"
RSpec.describe "a sorted listing" do
include_context "with a listing of topics"
- let(:sort) { { created_at: :asc } }
+ let(:sort) { { createdAt: :asc } }
let(:params) { { sort:, fields: { topics: [:title] } } }
it "orders the rows by the sort of the request" do
@@ -20,8 +20,8 @@ RSpec.describe "a sorted listing" do
)
end
- context "when the sort is created_at desc" do
- let(:sort) { { created_at: :desc } }
+ context "when the sort is createdAt desc" do
+ let(:sort) { { createdAt: :desc } }
it "orders the rows in that direction" do
expect(document).to eq(
@@ -36,8 +36,8 @@ RSpec.describe "a sorted listing" do
end
end
- context "when the sort is title asc and created_at desc" do
- let(:sort) { { title: :asc, created_at: :desc } }
+ context "when the sort is title asc and createdAt desc" do
+ let(:sort) { { title: :asc, createdAt: :desc } }
it "orders the rows by each sort in turn" do
expect(document).to eq(
@@ -87,14 +87,14 @@ RSpec.describe "a sorted listing" do
end
end
- context "when the sort is last_posted_at and one row holds none" do
+ context "when the sort is lastPostedAt and one row holds none" do
fab!(:never_posted) do
Fabricate(:topic, title: "Nothing has been posted here", last_posted_at: nil)
end
before { [oldest, middle, newest].each { it.update_columns(last_posted_at: it.created_at) } }
- let(:sort) { { last_posted_at: :asc } }
+ let(:sort) { { lastPostedAt: :asc } }
it "puts the row with no value last" do
expect(document).to eq(
@@ -109,8 +109,8 @@ RSpec.describe "a sorted listing" do
)
end
- context "when the sort is last_posted_at desc" do
- let(:sort) { { last_posted_at: :desc } }
+ context "when the sort is lastPostedAt desc" do
+ let(:sort) { { lastPostedAt: :desc } }
it "puts the row with no value last" do
expect(document).to eq(
diff --git a/spec/integration/json_api_kit/sparse_fieldsets_spec.rb b/spec/integration/json_api_kit/sparse_fieldsets_spec.rb
index bb41c6ccb30..7ad61c8a4ac 100644
--- a/spec/integration/json_api_kit/sparse_fieldsets_spec.rb
+++ b/spec/integration/json_api_kit/sparse_fieldsets_spec.rb
@@ -5,7 +5,7 @@ require_relative "support"
RSpec.describe "a document with a sparse fieldset" do
include_context "with a listing of topics"
- let(:params) { { sort: { created_at: :asc }, page: { size: 1 }, fields: { topics: [:title] } } }
+ let(:params) { { sort: { createdAt: :asc }, page: { size: 1 }, fields: { topics: [:title] } } }
it "renders the field of the primary type it asks for" do
expect(document).to eq(
@@ -16,7 +16,7 @@ RSpec.describe "a document with a sparse fieldset" do
end
context "when the request has no fieldset" do
- let(:params) { { sort: { created_at: :asc }, page: { size: 1 } } }
+ let(:params) { { sort: { createdAt: :asc }, page: { size: 1 } } }
it "renders every field the resource declares" do
expect(document).to eq(
@@ -30,7 +30,7 @@ RSpec.describe "a document with a sparse fieldset" do
context "when the fieldset holds an unknown field" do
let(:params) do
- { sort: { created_at: :asc }, page: { size: 1 }, fields: { topics: [:secrets] } }
+ { sort: { createdAt: :asc }, page: { size: 1 }, fields: { topics: [:secrets] } }
end
it "renders no attribute" do
@@ -47,7 +47,7 @@ RSpec.describe "a document with a sparse fieldset" do
let(:params) do
{
sort: {
- created_at: :asc,
+ createdAt: :asc,
},
page: {
size: 1,
@@ -88,26 +88,26 @@ RSpec.describe "a document with a sparse fieldset" do
let(:params) do
{
sort: {
- created_at: :asc,
+ createdAt: :asc,
},
page: {
size: 1,
},
include: %w[posts],
fields: {
- posts: [:post_number],
+ posts: [:postNumber],
},
}
end
it "renders every related record with that field" do
- expect(document[:included]).to eq([post_object(first_post, fields: %w[post_number])])
+ expect(document[:included]).to eq([post_object(first_post, fields: %w[postNumber])])
end
end
context "when the fieldset of a related type is empty" do
let(:params) do
- { sort: { created_at: :asc }, page: { size: 1 }, include: %w[user], fields: { users: [] } }
+ { sort: { createdAt: :asc }, page: { size: 1 }, include: %w[user], fields: { users: [] } }
end
it "renders the related record with its type and its id" do
@@ -137,7 +137,7 @@ RSpec.describe "a document with a sparse fieldset" do
let(:params) do
{
sort: {
- created_at: :asc,
+ createdAt: :asc,
},
page: {
size: 1,
diff --git a/spec/integration/json_api_kit/support.rb b/spec/integration/json_api_kit/support.rb
index 927740848e1..0683acfab86 100644
--- a/spec/integration/json_api_kit/support.rb
+++ b/spec/integration/json_api_kit/support.rb
@@ -63,6 +63,7 @@ module JsonApiKitSpec
anchor :title
anchor :last_posted_at
anchor(:mine) { |topics, guardian| topics.where(user_id: guardian.user&.id) }
+ anchor(:without_replies) { |topics, _guardian| topics.where(posts_count: 0) }
attribute :title
attribute :created_at
@@ -71,6 +72,7 @@ module JsonApiKitSpec
has_one :category, resource: CategoryResource
has_many :tags, resource: TagResource
has_many :posts, resource: PostResource
+ has_many :ordered_posts, resource: PostResource
includes "user.groups", "user.groups.users", "posts.topic", "posts.user", "posts.user.groups"
end
@@ -96,14 +98,30 @@ RSpec.shared_context "with a listing of topics" do
let(:current) { "https://example.com/api/topics" }
let(:query) { {} }
let(:scoped_to) { nil }
+ let(:glossary) { JsonApiKit::Glossary.new([JsonApiKit::Glossary::CasingRule]) }
let(:urls) { JsonApiKit::Urls.new(base:, current:, parameters: query) }
let(:document) do
- JsonApiKit::Document::Collection.for(params, resource:, guardian:, urls:, scoped_to:).to_h
+ JsonApiKit::Document::Collection.for(
+ params,
+ resource:,
+ guardian:,
+ urls:,
+ glossary:,
+ scoped_to:,
+ ).to_h
end
def one_document(id, **options)
- JsonApiKit::Document::Individual.for(id, params, resource:, guardian:, urls:, **options).to_h
+ JsonApiKit::Document::Individual.for(
+ id,
+ params,
+ resource:,
+ guardian:,
+ urls:,
+ glossary:,
+ **options,
+ ).to_h
end
def listing_of(parameters)
@@ -112,17 +130,18 @@ RSpec.shared_context "with a listing of topics" do
resource:,
guardian:,
urls: JsonApiKit::Urls.new(base:, current:),
+ glossary:,
scoped_to:,
).to_h
end
def cursor_of(row) = row[:meta][:page][:cursor]
- def topic_object(topic, fields: %w[title created_at], **members)
+ def topic_object(topic, fields: %w[title createdAt], **members)
resource_object(
"topics",
topic,
- { "title" => topic.title, "created_at" => topic.created_at }.slice(*fields),
+ { "title" => topic.title, "createdAt" => topic.created_at }.slice(*fields),
**members,
)
end
@@ -143,8 +162,8 @@ RSpec.shared_context "with a listing of topics" do
resource_object("tags", tag, { "name" => tag.name }.slice(*fields), **members)
end
- def post_object(post, fields: %w[post_number], **members)
- resource_object("posts", post, { "post_number" => post.post_number }.slice(*fields), **members)
+ def post_object(post, fields: %w[postNumber], **members)
+ resource_object("posts", post, { "postNumber" => post.post_number }.slice(*fields), **members)
end
def resource_object(type, row, attributes, cursor: nil, relationships: nil)
diff --git a/spec/integration/json_api_kit/versions_spec.rb b/spec/integration/json_api_kit/versions_spec.rb
index 2cefcf8235d..f5ff197262e 100644
--- a/spec/integration/json_api_kit/versions_spec.rb
+++ b/spec/integration/json_api_kit/versions_spec.rb
@@ -71,7 +71,8 @@ RSpec.describe "JSON:API versioning", type: :request do
expect(error).to eq(
refusal(
title: "Api-Version is in the future",
- detail: "The current version is #{first_version}.",
+ detail:
+ "Api-Version must not be later than today in UTC. The current version is #{first_version}.",
header: "Api-Version",
).deep_stringify_keys,
)
diff --git a/spec/lib/json_api_kit/api_version_spec.rb b/spec/lib/json_api_kit/api_version_spec.rb
index fddd4a09243..b42b9b86073 100644
--- a/spec/lib/json_api_kit/api_version_spec.rb
+++ b/spec/lib/json_api_kit/api_version_spec.rb
@@ -52,6 +52,12 @@ RSpec.describe JsonApiKit::ApiVersion do
end
end
+ context "when the other is not a version" do
+ it "is not equal" do
+ expect(version).not_to eq("2026-09-01")
+ end
+ end
+
context "when the other version has the same date" do
let(:other) { described_class.parse(raw) }
diff --git a/spec/lib/json_api_kit/document/collection_spec.rb b/spec/lib/json_api_kit/document/collection_spec.rb
index d9e1ca8e66e..f7a1cd70a4b 100644
--- a/spec/lib/json_api_kit/document/collection_spec.rb
+++ b/spec/lib/json_api_kit/document/collection_spec.rb
@@ -1,7 +1,9 @@
# frozen_string_literal: true
RSpec.describe JsonApiKit::Document::Collection do
- subject(:document) { described_class.new(listing, urls:) }
+ subject(:document) { described_class.new(listing, urls:, glossary:) }
+
+ let(:glossary) { JsonApiKit::Glossary.new([JsonApiKit::Glossary::CasingRule]) }
fab!(:first_topic) do
Fabricate(:topic, title: "Segments of a listing", created_at: Time.utc(2026, 8, 1))
@@ -42,12 +44,14 @@ RSpec.describe JsonApiKit::Document::Collection do
expect(JsonApiKit::Document::ResourceObject).to have_received(:new).with(
first_record,
urls:,
+ glossary:,
meta: {
},
)
expect(JsonApiKit::Document::ResourceObject).to have_received(:new).with(
second_record,
urls:,
+ glossary:,
meta: {
},
)
@@ -62,6 +66,7 @@ RSpec.describe JsonApiKit::Document::Collection do
expect(JsonApiKit::Document::ResourceObject).to have_received(:new).with(
first_record,
urls:,
+ glossary:,
meta: {
page: {
cursor: first_record.cursor.to_s,
@@ -71,6 +76,7 @@ RSpec.describe JsonApiKit::Document::Collection do
expect(JsonApiKit::Document::ResourceObject).to have_received(:new).with(
second_record,
urls:,
+ glossary:,
meta: {
page: {
cursor: second_record.cursor.to_s,
diff --git a/spec/lib/json_api_kit/document/individual_spec.rb b/spec/lib/json_api_kit/document/individual_spec.rb
index 1ebf801b086..94f5ee6894b 100644
--- a/spec/lib/json_api_kit/document/individual_spec.rb
+++ b/spec/lib/json_api_kit/document/individual_spec.rb
@@ -1,7 +1,9 @@
# frozen_string_literal: true
RSpec.describe JsonApiKit::Document::Individual do
- subject(:document) { described_class.new(reading, urls:) }
+ subject(:document) { described_class.new(reading, urls:, glossary:) }
+
+ let(:glossary) { JsonApiKit::Glossary.new([JsonApiKit::Glossary::CasingRule]) }
fab!(:topic) { Fabricate(:topic, title: "One record read on its own") }
diff --git a/spec/lib/json_api_kit/document/resource_object_spec.rb b/spec/lib/json_api_kit/document/resource_object_spec.rb
index 38fe19014de..948993bf1a7 100644
--- a/spec/lib/json_api_kit/document/resource_object_spec.rb
+++ b/spec/lib/json_api_kit/document/resource_object_spec.rb
@@ -1,7 +1,9 @@
# frozen_string_literal: true
RSpec.describe JsonApiKit::Document::ResourceObject do
- subject(:resource_object) { described_class.new(record, urls:, meta:) }
+ subject(:resource_object) { described_class.new(record, urls:, glossary:, meta:) }
+
+ let(:glossary) { JsonApiKit::Glossary.new([JsonApiKit::Glossary::CasingRule]) }
fab!(:topic) { Fabricate(:topic, title: "A row a document renders") }
diff --git a/spec/lib/json_api_kit/document_spec.rb b/spec/lib/json_api_kit/document_spec.rb
index 83aec6f2255..6c58061f589 100644
--- a/spec/lib/json_api_kit/document_spec.rb
+++ b/spec/lib/json_api_kit/document_spec.rb
@@ -18,6 +18,7 @@ RSpec.describe JsonApiKit::Document do
end
end
let(:guardian) { Guardian.new }
+ let(:glossary) { JsonApiKit::Glossary.new([JsonApiKit::Glossary::CasingRule]) }
let(:urls) do
JsonApiKit::Urls.new(base: "https://example.com/api", current: "https://example.com/api/topics")
end
@@ -25,7 +26,14 @@ RSpec.describe JsonApiKit::Document do
describe "Collection.for" do
subject(:document) do
- JsonApiKit::Document::Collection.for(parameters, resource:, guardian:, urls:, scoped_to:)
+ JsonApiKit::Document::Collection.for(
+ parameters,
+ resource:,
+ guardian:,
+ urls:,
+ glossary:,
+ scoped_to:,
+ )
end
let(:scoped_to) { nil }
@@ -60,7 +68,7 @@ RSpec.describe JsonApiKit::Document do
describe "Individual.for" do
subject(:document) do
- JsonApiKit::Document::Individual.for(id, parameters, resource:, guardian:, urls:)
+ JsonApiKit::Document::Individual.for(id, parameters, resource:, guardian:, urls:, glossary:)
end
let(:id) { topic.id }
diff --git a/spec/lib/json_api_kit/glossary/casing_rule_spec.rb b/spec/lib/json_api_kit/glossary/casing_rule_spec.rb
new file mode 100644
index 00000000000..124ed1c60d8
--- /dev/null
+++ b/spec/lib/json_api_kit/glossary/casing_rule_spec.rb
@@ -0,0 +1,71 @@
+# frozen_string_literal: true
+
+RSpec.describe JsonApiKit::Glossary::CasingRule do
+ subject(:rule) { described_class }
+
+ describe "#declared_name" do
+ it "returns the name in snake case" do
+ expect(rule.declared_name("createdAt")).to eq("created_at")
+ end
+
+ context "when the value is a list of names" do
+ it "returns every name in snake case" do
+ expect(rule.declared_name("-createdAt,title")).to eq("-created_at,title")
+ end
+ end
+
+ context "when the value is a path" do
+ it "returns every segment" do
+ expect(rule.declared_name("solvedStatus.answeredAt")).to eq("solved_status.answered_at")
+ end
+ end
+
+ context "when the name has a hyphen" do
+ it "keeps the hyphen" do
+ expect(rule.declared_name("solved-statuses")).to eq("solved-statuses")
+ end
+ end
+
+ context "when the name starts with a capital" do
+ it do
+ expect { rule.declared_name("CreatedAt") }.to raise_error(described_class::NotAMemberName)
+ end
+ end
+
+ context "when the name ends with capitals" do
+ it do
+ expect { rule.declared_name("createdAT") }.to raise_error(described_class::NotAMemberName)
+ end
+ end
+
+ context "when the name has an underscore" do
+ it do
+ expect { rule.declared_name("created_at") }.to raise_error(described_class::NotAMemberName)
+ end
+
+ it "suggests the name to use instead" do
+ expect { rule.declared_name("created_at") }.to raise_error(
+ /Use createdAt, not created_at\./,
+ )
+ end
+ end
+ end
+
+ describe "#member_name" do
+ it "returns the name in camel case" do
+ expect(rule.member_name("created_at")).to eq("createdAt")
+ end
+
+ context "when the name is a path" do
+ it "returns every segment" do
+ expect(rule.member_name("last_poster.last_seen_at")).to eq("lastPoster.lastSeenAt")
+ end
+ end
+
+ context "when the name has a hyphen" do
+ it "keeps the hyphen" do
+ expect(rule.member_name("solved-statuses")).to eq("solved-statuses")
+ end
+ end
+ end
+end
diff --git a/spec/lib/json_api_kit/glossary_spec.rb b/spec/lib/json_api_kit/glossary_spec.rb
new file mode 100644
index 00000000000..4b7431bf01f
--- /dev/null
+++ b/spec/lib/json_api_kit/glossary_spec.rb
@@ -0,0 +1,70 @@
+# frozen_string_literal: true
+
+module JsonApiKitSpec
+ MarkingRule =
+ Struct.new(:mark) do
+ def declared_name(name) = "#{name}<#{mark}"
+
+ def member_name(name) = "#{name}>#{mark}"
+ end
+end
+
+RSpec.describe JsonApiKit::Glossary do
+ subject(:glossary) { described_class.new([JsonApiKit::Glossary::CasingRule]) }
+
+ describe ".kit" do
+ subject(:kit) { described_class.kit }
+
+ before { allow(described_class).to receive(:new).and_call_original }
+
+ it "builds a glossary with the wire format rules" do
+ kit
+ expect(described_class).to have_received(:new).with([JsonApiKit::Glossary::CasingRule])
+ end
+ end
+
+ describe ".resource" do
+ subject(:resource) { described_class.resource }
+
+ before { allow(described_class).to receive(:new).and_call_original }
+
+ it "builds a glossary with the rules for a resource" do
+ resource
+ expect(described_class).to have_received(:new).with([JsonApiKit::Glossary::CasingRule])
+ end
+ end
+
+ describe "#declared_name" do
+ it "returns the declared name" do
+ expect(glossary.declared_name("createdAt")).to eq("created_at")
+ end
+
+ context "with several rules" do
+ subject(:glossary) { described_class.new([mark_a, mark_b]) }
+
+ let(:mark_a) { JsonApiKitSpec::MarkingRule.new("a") }
+ let(:mark_b) { JsonApiKitSpec::MarkingRule.new("b") }
+
+ it "applies them in order" do
+ expect(glossary.declared_name("x")).to eq("x<a<b")
+ end
+ end
+ end
+
+ describe "#member_name" do
+ it "returns the member name" do
+ expect(glossary.member_name("created_at")).to eq("createdAt")
+ end
+
+ context "with several rules" do
+ subject(:glossary) { described_class.new([mark_a, mark_b]) }
+
+ let(:mark_a) { JsonApiKitSpec::MarkingRule.new("a") }
+ let(:mark_b) { JsonApiKitSpec::MarkingRule.new("b") }
+
+ it "applies them in reverse order" do
+ expect(glossary.member_name("x")).to eq("x>b>a")
+ end
+ end
+ end
+end
diff --git a/spec/lib/json_api_kit/request/parameter_name_spec.rb b/spec/lib/json_api_kit/request/parameter_name_spec.rb
new file mode 100644
index 00000000000..958d25383c1
--- /dev/null
+++ b/spec/lib/json_api_kit/request/parameter_name_spec.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+RSpec.describe JsonApiKit::Request::ParameterName do
+ subject(:parameter_name) { described_class.new("page", "anchor") }
+
+ describe "#to_s" do
+ it "writes the members in brackets after the family" do
+ expect(parameter_name.to_s).to eq("page[anchor]")
+ end
+
+ context "when the name has one part" do
+ subject(:parameter_name) { described_class.new("sort") }
+
+ it "writes the family alone" do
+ expect(parameter_name.to_s).to eq("sort")
+ end
+ end
+ end
+
+ describe "#family" do
+ it "returns the name without its members" do
+ expect(parameter_name.family.to_s).to eq("page")
+ end
+ end
+
+ describe "#member" do
+ it "returns the name one member deeper" do
+ expect(parameter_name.member("createdAt").to_s).to eq("page[anchor][createdAt]")
+ end
+ end
+end
diff --git a/spec/lib/json_api_kit/request/parameters_spec.rb b/spec/lib/json_api_kit/request/parameters_spec.rb
new file mode 100644
index 00000000000..81f240331a3
--- /dev/null
+++ b/spec/lib/json_api_kit/request/parameters_spec.rb
@@ -0,0 +1,103 @@
+# frozen_string_literal: true
+
+RSpec.describe JsonApiKit::Request::Parameters do
+ subject(:declared_parameters) { described_class.new(parameters, glossary:).to_h }
+
+ let(:glossary) { JsonApiKit::Glossary.new([JsonApiKit::Glossary::CasingRule]) }
+ let(:parameters) { { "sort" => "-createdAt" } }
+
+ it "converts the sort names" do
+ expect(declared_parameters).to eq("sort" => "-created_at")
+ end
+
+ context "when the sort is a hash" do
+ let(:parameters) { { "sort" => { "createdAt" => :desc } } }
+
+ it "converts the keys only" do
+ expect(declared_parameters).to eq("sort" => { "created_at" => :desc })
+ end
+ end
+
+ context "when the parameters have an include path" do
+ let(:parameters) { { "include" => "orderedPosts.lastPoster" } }
+
+ it "converts every segment of the path" do
+ expect(declared_parameters).to eq("include" => "ordered_posts.last_poster")
+ end
+ end
+
+ context "when the parameters have a fieldset" do
+ let(:parameters) { { "fields" => { "solved-statuses" => "answeredAt" } } }
+
+ it "converts the field names only" do
+ expect(declared_parameters).to eq("fields" => { "solved-statuses" => "answered_at" })
+ end
+ end
+
+ context "when a filter is named like a parameter" do
+ let(:parameters) { { "filter" => { "sort" => "created_at", "anchor" => "SomeValue" } } }
+
+ it "keeps every filter value" do
+ expect(declared_parameters).to eq(
+ "filter" => {
+ "sort" => "created_at",
+ "anchor" => "SomeValue",
+ },
+ )
+ end
+ end
+
+ context "when a name is not a member name" do
+ let(:parameters) { { "sort" => "created_at" } }
+
+ it "raises with the parameter that holds it" do
+ expect { declared_parameters }.to raise_error(
+ an_instance_of(JsonApiKit::Glossary::CasingRule::NotAMemberName).and(
+ having_attributes(source: { parameter: "sort" }),
+ ),
+ )
+ end
+
+ context "when the name is a key" do
+ let(:parameters) { { "page" => { "anchor" => { "created_at" => "2026-08-01" } } } }
+
+ it "raises with the parameter that holds it" do
+ expect { declared_parameters }.to raise_error(
+ having_attributes(source: { parameter: "page[anchor][created_at]" }),
+ )
+ end
+ end
+ end
+
+ context "when the parameters have a filter" do
+ let(:parameters) { { "filter" => { "postCount" => "SomeTopic Title" } } }
+
+ it "converts the filter name only" do
+ expect(declared_parameters).to eq("filter" => { "post_count" => "SomeTopic Title" })
+ end
+ end
+
+ context "when the parameters have page members" do
+ let(:parameters) { { "page" => { "beforeSize" => "1", "after" => "WyJhQiJd" } } }
+
+ it "converts the member names only" do
+ expect(declared_parameters).to eq("page" => { "before_size" => "1", "after" => "WyJhQiJd" })
+ end
+ end
+
+ context "when the parameters anchor on an attribute" do
+ let(:parameters) { { "page" => { "anchor" => { "createdAt" => "2026-08-01" } } } }
+
+ it "converts the anchor name only" do
+ expect(declared_parameters).to eq("page" => { "anchor" => { "created_at" => "2026-08-01" } })
+ end
+ end
+
+ context "when the parameters anchor on a computed position" do
+ let(:parameters) { { "page" => { "anchor" => "withoutReplies" } } }
+
+ it "converts the anchor name" do
+ expect(declared_parameters).to eq("page" => { "anchor" => "without_replies" })
+ end
+ end
+end
diff --git a/spec/lib/json_api_kit/timeline_spec.rb b/spec/lib/json_api_kit/timeline_spec.rb
index d2dcdf59f33..cc214ef4369 100644
--- a/spec/lib/json_api_kit/timeline_spec.rb
+++ b/spec/lib/json_api_kit/timeline_spec.rb
@@ -4,7 +4,7 @@ RSpec.describe JsonApiKit::Timeline do
let(:earliest) { JsonApiKit::ApiVersion.parse("2026-09-01") }
let(:middle) { JsonApiKit::ApiVersion.parse("2026-11-15") }
let(:latest) { JsonApiKit::ApiVersion.parse("2027-01-05") }
- let(:timeline) { described_class.new([earliest, middle, latest].map(&:to_s)) }
+ let(:timeline) { described_class.new([earliest, middle, latest]) }
before do
freeze_time(Date.new(2027, 1, 20))
diff --git a/spec/lib/json_api_kit/url_spec.rb b/spec/lib/json_api_kit/url_spec.rb
index a37b587aa6d..f2bdb15b1b6 100644
--- a/spec/lib/json_api_kit/url_spec.rb
+++ b/spec/lib/json_api_kit/url_spec.rb
@@ -4,11 +4,11 @@ RSpec.describe JsonApiKit::Url do
subject(:url) { described_class.new(address, parameters) }
let(:address) { "https://example.com/api/topics" }
- let(:parameters) { { "page" => { "size" => "2" }, "sort" => "created_at" } }
+ let(:parameters) { { "page" => { "size" => "2" }, "sort" => "createdAt" } }
describe "#to_s" do
it "returns the address with the parameters it carries" do
- expect(url.to_s).to eq("https://example.com/api/topics?page%5Bsize%5D=2&sort=created_at")
+ expect(url.to_s).to eq("https://example.com/api/topics?page%5Bsize%5D=2&sort=createdAt")
end
context "when it carries no parameter" do
@@ -21,9 +21,19 @@ RSpec.describe JsonApiKit::Url do
end
describe "#at" do
+ context "when the member it adds has several words" do
+ let(:parameters) { {} }
+
+ it "writes it in camel case" do
+ expect(url.at(before_size: 2).to_s).to eq(
+ "https://example.com/api/topics?page%5BbeforeSize%5D=2",
+ )
+ end
+ end
+
it "adds the cursor to the parameters it carries" do
expect(url.at(after: "a-cursor").to_s).to eq(
- "https://example.com/api/topics?page%5Bafter%5D=a-cursor&page%5Bsize%5D=2&sort=created_at",
+ "https://example.com/api/topics?page%5Bafter%5D=a-cursor&page%5Bsize%5D=2&sort=createdAt",
)
end
@@ -48,9 +58,9 @@ RSpec.describe JsonApiKit::Url do
"anchor" => {
"id" => "12",
},
- "before_size" => "1",
- "after_size" => "1",
- "include_anchor" => "false",
+ "beforeSize" => "1",
+ "afterSize" => "1",
+ "includeAnchor" => "false",
"size" => "2",
},
}
diff --git a/spec/lib/json_api_kit/urls_spec.rb b/spec/lib/json_api_kit/urls_spec.rb
index cc212b7886e..f8d917800a9 100644
--- a/spec/lib/json_api_kit/urls_spec.rb
+++ b/spec/lib/json_api_kit/urls_spec.rb
@@ -5,12 +5,12 @@ RSpec.describe JsonApiKit::Urls do
let(:base) { "https://example.com/api" }
let(:current) { "https://example.com/api/topics" }
- let(:parameters) { { "page" => { "size" => "2" }, "sort" => "created_at" } }
+ let(:parameters) { { "page" => { "size" => "2" }, "sort" => "createdAt" } }
describe "#current" do
it "returns the URL a caller read with the parameters they sent" do
expect(urls.current.to_s).to eq(
- "https://example.com/api/topics?page%5Bsize%5D=2&sort=created_at",
+ "https://example.com/api/topics?page%5Bsize%5D=2&sort=createdAt",
)
end
end
diff --git a/spec/resources/group_resource_spec.rb b/spec/resources/group_resource_spec.rb
index 2fa28291f84..ed399c4e189 100644
--- a/spec/resources/group_resource_spec.rb
+++ b/spec/resources/group_resource_spec.rb
@@ -4,11 +4,19 @@ RSpec.describe GroupResource do
fab!(:group) { Fabricate(:group, name: "a_group") }
let(:guardian) { Guardian.new }
+ let(:glossary) { JsonApiKit::Glossary.resource }
let(:urls) do
JsonApiKit::Urls.new(base: "https://example.com/api", current: "https://example.com/api/groups")
end
let(:document) do
- JsonApiKit::Document::Individual.for(group.id, {}, resource: described_class, guardian:, urls:)
+ JsonApiKit::Document::Individual.for(
+ group.id,
+ {},
+ resource: described_class,
+ guardian:,
+ urls:,
+ glossary:,
+ )
end
it "shows the name and nothing else" do
diff --git a/spec/resources/user_resource_spec.rb b/spec/resources/user_resource_spec.rb
index 3403f7fa216..7280d3f9bc3 100644
--- a/spec/resources/user_resource_spec.rb
+++ b/spec/resources/user_resource_spec.rb
@@ -4,11 +4,19 @@ RSpec.describe UserResource do
fab!(:user) { Fabricate(:user, username: "someone") }
let(:guardian) { Guardian.new }
+ let(:glossary) { JsonApiKit::Glossary.resource }
let(:urls) do
JsonApiKit::Urls.new(base: "https://example.com/api", current: "https://example.com/api/users")
end
let(:document) do
- JsonApiKit::Document::Individual.for(user.id, {}, resource: described_class, guardian:, urls:)
+ JsonApiKit::Document::Individual.for(
+ user.id,
+ {},
+ resource: described_class,
+ guardian:,
+ urls:,
+ glossary:,
+ )
end
it "shows the username and nothing else" do