A platform for community discussion. Free, open, simple.
DEV: Unencode the brackets of a JSON:API link (#43144)
Loïc Guitaut21cb0e82586b
diff --git a/lib/json_api_kit/document/error_object.rb b/lib/json_api_kit/document/error_object.rbindex d0b8ca1c33d..c8dc3184082 100644--- a/lib/json_api_kit/document/error_object.rb+++ b/lib/json_api_kit/document/error_object.rb@@ -15,7 +15,7 @@ module JsonApiKitattr_reader :error- def links = error.type&.then { { type: it } }+ def links = error.type.try { { type: it } }endendenddiff --git a/lib/json_api_kit/linkage/to_one.rb b/lib/json_api_kit/linkage/to_one.rbindex 9163c82c747..45672fb118e 100644--- a/lib/json_api_kit/linkage/to_one.rb+++ b/lib/json_api_kit/linkage/to_one.rb@@ -3,7 +3,7 @@module JsonApiKitclass Linkageclass ToOne < Linkage- def collapse(&) = records.first&.then(&)+ def collapse(&) = records.first.try(&)endendenddiff --git a/lib/json_api_kit/pagination/order/segment.rb b/lib/json_api_kit/pagination/order/segment.rbindex 85e2b1fbf36..35808feafb9 100644--- a/lib/json_api_kit/pagination/order/segment.rb+++ b/lib/json_api_kit/pagination/order/segment.rb@@ -35,8 +35,9 @@ module JsonApiKitdef reverse = self.class.new(id:, keyset: keyset.reverse, digest:, condition:)- def narrowed_by(other) =+ def narrowed_by(other)self.class.new(id:, keyset:, digest:, condition: condition_after(other))+ endprivatediff --git a/lib/json_api_kit/url.rb b/lib/json_api_kit/url.rbindex b6a52ea04d3..0383677581a 100644--- a/lib/json_api_kit/url.rb+++ b/lib/json_api_kit/url.rb@@ -35,6 +35,6 @@ module JsonApiKitdef member_names(page) = page.transform_keys { KIT.member_name(it) }- def query_string = parameters.to_query.presence&.then { "?#{it}" }+ def query_string = Rack::Utils.build_nested_query(parameters).presence.try { "?#{it}" }endenddiff --git a/spec/integration/json_api_kit/documents_spec.rb b/spec/integration/json_api_kit/documents_spec.rbindex 03ee4b97ff8..e207f066a76 100644--- a/spec/integration/json_api_kit/documents_spec.rb+++ b/spec/integration/json_api_kit/documents_spec.rb@@ -13,7 +13,7 @@ RSpec.describe "a rendered document" doexpect(document).to eq(data: [topic_object(oldest)],included: [],- links: links_of(next: page_url(after: cursor_of_record(oldest), size: 1)),+ links: links_of(next: page_url(size: 1, after: cursor_of_record(oldest))),)end@@ -49,7 +49,7 @@ RSpec.describe "a rendered document" do),],included: [user_object(oldest.user)],- links: links_of(next: page_url(after: cursor_of_record(oldest), size: 1)),+ links: links_of(next: page_url(size: 1, after: cursor_of_record(oldest))),)endend@@ -79,8 +79,8 @@ RSpec.describe "a rendered document" doincluded: [],links:links_of(- prev: page_url(before: cursor_of_record(middle), size: 1),- next: page_url(after: cursor_of_record(middle), size: 1),+ prev: page_url(size: 1, before: cursor_of_record(middle)),+ next: page_url(size: 1, after: cursor_of_record(middle)),),)enddiff --git a/spec/integration/json_api_kit/endpoint_spec.rb b/spec/integration/json_api_kit/endpoint_spec.rbindex 06d9be62eec..e68e7393e72 100644--- a/spec/integration/json_api_kit/endpoint_spec.rb+++ b/spec/integration/json_api_kit/endpoint_spec.rb@@ -124,7 +124,7 @@ RSpec.describe "a JSON:API endpoint", type: :request do{data: [topic_object(newest, fields:), topic_object(middle, fields:)],included: [],- links: links_of(next: page_url(after: cursor_of_record(middle), size: 2)),+ links: links_of(next: page_url(size: 2, after: cursor_of_record(middle))),}.deep_stringify_keys,)enddiff --git a/spec/integration/json_api_kit/pagination_spec.rb b/spec/integration/json_api_kit/pagination_spec.rbindex d2153d407e1..8302c2ae9f1 100644--- a/spec/integration/json_api_kit/pagination_spec.rb+++ b/spec/integration/json_api_kit/pagination_spec.rb@@ -14,7 +14,7 @@ RSpec.describe "a page of a listing" doincluded: [],links:links_of(- next: page_url(after: cursor_of_record(middle, sort: { created_at: :asc }), size: 2),+ next: page_url(size: 2, after: cursor_of_record(middle, sort: { created_at: :asc })),),)end@@ -60,8 +60,8 @@ RSpec.describe "a page of a listing" doincluded: [],links:links_of(- prev: page_url(before: cursor_of_record(middle, sort: { created_at: :asc }), size: 1),- next: page_url(after: cursor_of_record(middle, sort: { created_at: :asc }), size: 1),+ prev: page_url(size: 1, before: cursor_of_record(middle, sort: { created_at: :asc })),+ next: page_url(size: 1, after: cursor_of_record(middle, sort: { created_at: :asc })),),)enddiff --git a/spec/integration/json_api_kit/relationships_spec.rb b/spec/integration/json_api_kit/relationships_spec.rbindex e7484bb71ea..fa2b450ce9a 100644--- a/spec/integration/json_api_kit/relationships_spec.rb+++ b/spec/integration/json_api_kit/relationships_spec.rb@@ -407,7 +407,7 @@ RSpec.describe "a document with related records" dodef next_page_of(row, post)"#{base}/topics/#{row.id}/relationships/posts?" +- { page: { after: cursor_of_record(post, of: JsonApiKitSpec::PostResource) } }.to_query+ query_of(page: { after: cursor_of_record(post, of: JsonApiKitSpec::PostResource) })endit "renders one page of them and links to the next" dodiff --git a/spec/integration/json_api_kit/support.rb b/spec/integration/json_api_kit/support.rbindex 0683acfab86..7717b7b2cf1 100644--- a/spec/integration/json_api_kit/support.rb+++ b/spec/integration/json_api_kit/support.rb@@ -205,13 +205,17 @@ RSpec.shared_context "with a listing of topics" dodef profile_link(name) = "https://jsonapi.org/profiles/ethanresnick/cursor-pagination/#{name}"def self_link- href = query.blank? ? current : "#{current}?#{query.to_query}"+ href = query.blank? ? current : "#{current}?#{query_of(query)}"{ href:, type: JsonApiKit::Pagination::Profile::MEDIA_TYPE }end+ def query_of(hash) = Rack::Utils.build_nested_query(hash)+def links_of(**pages) = { self: self_link, **{ prev: nil, next: nil }.merge(pages) }- def page_url(**page) = "#{current}?#{query.except("page").merge(page:).to_query}"+ def page_url(**page)+ "#{current}?#{query_of(query.merge("page" => page))}"+ enddef cursor_at(index, rendered = document) = cursor_of(rendered[:data][index])@@ -223,7 +227,7 @@ RSpec.shared_context "with a listing of topics" doenddef relationship_page_url(type, row, name, **page)- "#{base}/#{type}/#{row.id}/#{name}?#{{ page: page }.to_query}"+ "#{base}/#{type}/#{row.id}/#{name}?#{query_of(page: page)}"enddef listed_ids(rendered = document) = rendered[:data].map { it[:id] }diff --git a/spec/lib/json_api_kit/document/collection_spec.rb b/spec/lib/json_api_kit/document/collection_spec.rbindex f7a1cd70a4b..c93cf307499 100644--- a/spec/lib/json_api_kit/document/collection_spec.rb+++ b/spec/lib/json_api_kit/document/collection_spec.rb@@ -116,8 +116,8 @@ RSpec.describe JsonApiKit::Document::Collection doit "renders a link to the page at each end" doexpect(document.to_h[:links]).to eq(self: self_link,- prev: "https://example.com/api/topics?page%5Bbefore%5D=#{page_cursor}",- next: "https://example.com/api/topics?page%5Bafter%5D=#{page_cursor}",+ prev: "https://example.com/api/topics?page[before]=#{page_cursor}",+ next: "https://example.com/api/topics?page[after]=#{page_cursor}",)endenddiff --git a/spec/lib/json_api_kit/document/page_links_spec.rb b/spec/lib/json_api_kit/document/page_links_spec.rbindex c60a75ffbec..0f14db07865 100644--- a/spec/lib/json_api_kit/document/page_links_spec.rb+++ b/spec/lib/json_api_kit/document/page_links_spec.rb@@ -11,8 +11,8 @@ RSpec.describe JsonApiKit::Document::PageLinks doit "returns a link to the page at each end" doexpect(links).to eq(- prev: "https://example.com/api/topics?page%5Bbefore%5D=read-back-here",- next: "https://example.com/api/topics?page%5Bafter%5D=read-on-here",+ prev: "https://example.com/api/topics?page[before]=read-back-here",+ next: "https://example.com/api/topics?page[after]=read-on-here",)end@@ -30,7 +30,7 @@ RSpec.describe JsonApiKit::Document::PageLinks doit "returns a next link only" doexpect(links).to eq(prev: nil,- next: "https://example.com/api/topics?page%5Bafter%5D=read-on-here",+ next: "https://example.com/api/topics?page[after]=read-on-here",)endenddiff --git a/spec/lib/json_api_kit/document/relationship_object_spec.rb b/spec/lib/json_api_kit/document/relationship_object_spec.rbindex aa7cf914e0b..a0a5805ecc3 100644--- a/spec/lib/json_api_kit/document/relationship_object_spec.rb+++ b/spec/lib/json_api_kit/document/relationship_object_spec.rb@@ -73,8 +73,8 @@ RSpec.describe JsonApiKit::Document::RelationshipObject doexpect(relationship_object.to_h[:links]).to eq(self: relationship_url,related: related_url,- prev: "#{relationship_url}?page%5Bbefore%5D=read-back-here",- next: "#{relationship_url}?page%5Bafter%5D=read-on-here",+ prev: "#{relationship_url}?page[before]=read-back-here",+ next: "#{relationship_url}?page[after]=read-on-here",)endenddiff --git a/spec/lib/json_api_kit/url_spec.rb b/spec/lib/json_api_kit/url_spec.rbindex f2bdb15b1b6..32ac722d5f3 100644--- a/spec/lib/json_api_kit/url_spec.rb+++ b/spec/lib/json_api_kit/url_spec.rb@@ -8,7 +8,7 @@ RSpec.describe JsonApiKit::Url dodescribe "#to_s" doit "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=createdAt")+ expect(url.to_s).to eq("https://example.com/api/topics?page[size]=2&sort=createdAt")endcontext "when it carries no parameter" do@@ -26,14 +26,14 @@ RSpec.describe JsonApiKit::Url doit "writes it in camel case" doexpect(url.at(before_size: 2).to_s).to eq(- "https://example.com/api/topics?page%5BbeforeSize%5D=2",+ "https://example.com/api/topics?page[beforeSize]=2",)endendit "adds the cursor to the parameters it carries" doexpect(url.at(after: "a-cursor").to_s).to eq(- "https://example.com/api/topics?page%5Bafter%5D=a-cursor&page%5Bsize%5D=2&sort=createdAt",+ "https://example.com/api/topics?page[size]=2&page[after]=a-cursor&sort=createdAt",)end@@ -46,7 +46,7 @@ RSpec.describe JsonApiKit::Url doit "drops the other end" doexpect(url.at(before: "a-cursor").to_s).to eq(- "https://example.com/api/topics?page%5Bbefore%5D=a-cursor&page%5Bsize%5D=2",+ "https://example.com/api/topics?page[size]=2&page[before]=a-cursor",)endend@@ -68,7 +68,7 @@ RSpec.describe JsonApiKit::Url doit "drops the anchor and the window, and keeps the page size" doexpect(url.at(after: "a-cursor").to_s).to eq(- "https://example.com/api/topics?page%5Bafter%5D=a-cursor&page%5Bsize%5D=2",+ "https://example.com/api/topics?page[size]=2&page[after]=a-cursor",)endend@@ -78,7 +78,7 @@ RSpec.describe JsonApiKit::Url doit "carries only that cursor" doexpect(url.at(after: "a-cursor").to_s).to eq(- "https://example.com/api/topics?page%5Bafter%5D=a-cursor",+ "https://example.com/api/topics?page[after]=a-cursor",)endenddiff --git a/spec/lib/json_api_kit/urls_spec.rb b/spec/lib/json_api_kit/urls_spec.rbindex f8d917800a9..c5f7db98163 100644--- a/spec/lib/json_api_kit/urls_spec.rb+++ b/spec/lib/json_api_kit/urls_spec.rb@@ -9,9 +9,7 @@ RSpec.describe JsonApiKit::Urls dodescribe "#current" doit "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=createdAt",- )+ expect(urls.current.to_s).to eq("https://example.com/api/topics?page[size]=2&sort=createdAt")endend