diff --git a/CHANGELOG.md b/CHANGELOG.md index 78be1c8..40b8abc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Fixes +- Restore literal false in parameter expansion (#68) + ### Breaks ## 2.0.1 - (2025-02-16) diff --git a/lib/restify/relation.rb b/lib/restify/relation.rb index c5f0cb1..9b469cd 100644 --- a/lib/restify/relation.rb +++ b/lib/restify/relation.rb @@ -113,8 +113,12 @@ def convert_param(value, nesting: true) def extract!(params) @template.variables.each_with_object({}) do |var, hash| - if (value = params.delete(var) { params.delete(var.to_sym) { nil } }) - hash[var] = value + sym = var.to_sym + if params.key?(var) + hash[var] = params.delete(var) + end + if params.key?(sym) + hash[sym] = params.delete(sym) end end end diff --git a/spec/restify/relation_spec.rb b/spec/restify/relation_spec.rb index 1a0da64..c54f519 100644 --- a/spec/restify/relation_spec.rb +++ b/spec/restify/relation_spec.rb @@ -38,7 +38,7 @@ def to_param context 'with false' do let(:params) { {id: false} } - it { expect(expanded.to_s).to eq 'http://test.host/resource/' } + it { expect(expanded.to_s).to eq 'http://test.host/resource/false' } end context 'with true' do @@ -59,6 +59,27 @@ def to_param it { expect(expanded.to_s).to eq 'http://test.host/resource/1,2' } end + context 'with nil query parameter' do + let(:pattern) { '/resource{?abc}' } + let(:params) { {abc: nil} } + + it { expect(expanded.to_s).to eq 'http://test.host/resource' } + end + + context 'with false query parameter' do + let(:pattern) { '/resource{?abc}' } + let(:params) { {abc: false} } + + it { expect(expanded.to_s).to eq 'http://test.host/resource?abc=false' } + end + + context 'with true query parameter' do + let(:pattern) { '/resource{?abc}' } + let(:params) { {abc: true} } + + it { expect(expanded.to_s).to eq 'http://test.host/resource?abc=true' } + end + context 'with unknown additional query parameter' do let(:pattern) { '/resource{?a,b}' } let(:params) { {a: 1, b: 2, c: 3} } @@ -119,6 +140,38 @@ def to_param it { expect { expanded }.to raise_exception TypeError, /Can't convert Hash into String./ } end + + context 'with string-keyed params' do + context 'with value' do + let(:params) { {'id' => 1} } + + it { expect(expanded.to_s).to eq 'http://test.host/resource/1' } + end + end + + context 'with mixed-keyed params' do + context 'non-overlapping' do + let(:params) { {'id' => 1, q: 2} } + + it { expect(expanded.to_s).to eq 'http://test.host/resource/1?q=2' } + end + + context 'overlapping (1)' do + let(:params) { {'id' => 1, id: 2} } + + it 'prefers symbol value' do + expect(expanded.to_s).to eq 'http://test.host/resource/2' + end + end + + context 'overlapping (2)' do + let(:params) { {id: 2, 'id' => 1} } + + it 'prefers symbol value' do + expect(expanded.to_s).to eq 'http://test.host/resource/2' + end + end + end end shared_examples 'non-data-request' do |method|