From 54f3860a290266a24b141ea9fbb239cc2bdf0864 Mon Sep 17 00:00:00 2001 From: "Daniel (dB.) Doubrovkine" Date: Sat, 21 Nov 2026 15:53:32 -0500 Subject: [PATCH] Add Link#_query to send undeclared query parameters HAL templated links only expand the variables they declare, so callers had no way to send extra query parameters, or any parameters on a link that is not templated. `_query(params)` returns a new link with the parameters appended to its URL after any existing query string, keeps them across `_expand`, repeats array values and drops nil values. `_expand` stays strict. Closes #84. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011Gr21mVM7TZeZyebArnTs9 --- CHANGELOG.md | 1 + README.md | 12 +++++++ lib/hyperclient/link.rb | 32 +++++++++++++++--- test/hyperclient/link_test.rb | 62 +++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 771b23e..bff010d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Your contribution here. +* [#334](https://github.com/codegram/hyperclient/pull/334): Added `Link#_query` to send query parameters that a link doesn't declare - [@dblock](https://github.com/dblock). * [#333](https://github.com/codegram/hyperclient/pull/333): Updated danger-pr-comment to 0.2.0 and fixed the hash conversion assertion for Minitest 6.1 - [@dblock](https://github.com/dblock). ### 2.1.0 (2026/10/14) diff --git a/README.md b/README.md index 113bc88..9c169fc 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ Hyperclient is a Hypermedia API client written in Ruby. It fully supports [JSON - [Resources and Attributes](#resources-and-attributes) - [Links and Embedded Resources](#links-and-embedded-resources) - [Templated Links](#templated-links) + - [Query Parameters](#query-parameters) - [Curies](#curies) - [Attributes](#attributes) - [HTTP](#http) @@ -156,6 +157,17 @@ Invoking `api.place(id: 'oxpoints:23232339').name` is equivalent to `api._links. The client is responsible for supplying all the necessary parameters. Templated links don't do any strict parameter name checking and don't support required vs. optional parameters. Parameters not declared by the API will be dropped and will not have any effect when passed to `_expand`. +## Query Parameters + +HAL APIs declare the query parameters they accept in templated links, and `_expand` only fills in those. To send query parameters that a link doesn't declare, or to add them to a link that isn't templated, use `_query`. It returns a new link with the parameters appended to its URL, after any existing query string. + +```ruby +api._links.places._query(sort: 'name')._get # GET /places/?sort=name +api._links.place._expand(id: 'oxpoints:23232339')._query(fields: 'name')._get # GET /places/oxpoints%3A23232339?fields=name +``` + +Array values are sent as repeated parameters, and parameters with `nil` values are dropped. + ## Curies Curies provide compact names for relation documentation. The local demo adds an `oxford` CURIE and an `oxford:places` relation. diff --git a/lib/hyperclient/link.rb b/lib/hyperclient/link.rb index 864e29b..3b7a40b 100644 --- a/lib/hyperclient/link.rb +++ b/lib/hyperclient/link.rb @@ -13,11 +13,14 @@ class Link # entry_point - The EntryPoint object to inject the configuration. # uri_variables - The optional Hash with the variables to expand the link # if it is templated. - def initialize(key, link, entry_point, uri_variables = nil) + # query_params - The optional Hash with query parameters to add to the + # link's URL. + def initialize(key, link, entry_point, uri_variables = nil, query_params = nil) @key = key @link = link @entry_point = entry_point @uri_variables = uri_variables + @query_params = query_params @resource = nil end @@ -54,14 +57,22 @@ def _templated? # # Returns a new Link with the expanded variables. def _expand(uri_variables = {}) - self.class.new(@key, @link, @entry_point, uri_variables) + self.class.new(@key, @link, @entry_point, uri_variables, @query_params) + end + + # Public: Adds query parameters to the Link's URL, whether or not the link + # is templated or declares them. Parameters with nil values are dropped. + # + # query_params - The Hash with the query parameters to add. + # + # Returns a new Link with the query parameters. + def _query(query_params = {}) + self.class.new(@key, @link, @entry_point, @uri_variables, (@query_params || {}).merge(query_params)) end # Public: Returns the url of the Link. def _url - return @link['href'] unless _templated? - - @url ||= _uri_template.expand(@uri_variables || {}).to_s + @url ||= _add_query_params(_templated? ? _uri_template.expand(@uri_variables || {}).to_s : @link['href']) end # Public: Returns an array of variables from the URITemplate. @@ -189,6 +200,17 @@ def _uri_template @uri_template ||= Addressable::Template.new(@link['href']) end + # Internal: Appends the query parameters to the URL, keeping its existing + # query string and fragment. + def _add_query_params(url) + params = (@query_params || {}).compact + return url if params.empty? + + uri = Addressable::URI.parse(url) + uri.query = [uri.query, Addressable::URI.form_encode(params)].compact.reject(&:empty?).join('&') + uri.to_s + end + def http_method(method, body = nil) response = @entry_point.connection.run_request(method, _url, body, nil) resource = Resource.new(response.body, @entry_point, response) diff --git a/test/hyperclient/link_test.rb b/test/hyperclient/link_test.rb index d2852e7..6f88b18 100644 --- a/test/hyperclient/link_test.rb +++ b/test/hyperclient/link_test.rb @@ -101,6 +101,68 @@ module Hyperclient end end + describe '_query' do + it 'adds query parameters to a link that is not templated' do + link = Link.new('key', { 'href' => '/orders' }, entry_point) + + _(link._query(sort: 'name')._url).must_equal '/orders?sort=name' + end + + it 'adds query parameters a templated link does not declare' do + link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point) + + _(link._expand(id: '1')._query(sort: 'name')._url).must_equal '/orders?id=1&sort=name' + end + + it 'keeps query parameters when expanded afterwards' do + link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point) + + _(link._query(sort: 'name')._expand(id: '1')._url).must_equal '/orders?id=1&sort=name' + end + + it 'keeps the existing query string and fragment' do + link = Link.new('key', { 'href' => '/orders?status=open&status=paid#top' }, entry_point) + + _(link._query(sort: 'name')._url).must_equal '/orders?status=open&status=paid&sort=name#top' + end + + it 'combines chained query parameters' do + link = Link.new('key', { 'href' => '/orders' }, entry_point) + + _(link._query(sort: 'name')._query(page: 2, sort: 'date')._url).must_equal '/orders?sort=date&page=2' + end + + it 'encodes values and repeats array parameters' do + link = Link.new('key', { 'href' => '/orders' }, entry_point) + + _(link._query(q: 'a&b c', ids: [1, 2])._url).must_equal '/orders?q=a%26b+c&ids=1&ids=2' + end + + it 'drops nil and empty query parameters' do + link = Link.new('key', { 'href' => '/orders' }, entry_point) + + _(link._query(sort: nil)._url).must_equal '/orders' + _(link._query({})._url).must_equal '/orders' + end + + it 'does not change the original link' do + link = Link.new('key', { 'href' => '/orders' }, entry_point) + link._query(sort: 'name') + + _(link._url).must_equal '/orders' + end + + it 'sends the query parameters with the request' do + link = Link.new('key', { 'href' => '/orders' }, entry_point) + + stub_request(entry_point.connection) do |stub| + stub.get('http://api.example.org/orders?sort=name') { [200, {}, { 'sorted' => true }] } + end + + _(link._query(sort: 'name')._get.sorted).must_equal true + end + end + describe '_url' do it 'expands an uri template without variables' do link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point)