From 7d36d930501964bbf59739475e11265297cf1f7e Mon Sep 17 00:00:00 2001 From: Don Buchanan <42761041+Don-CA@users.noreply.github.com> Date: Thu, 14 Sep 2023 16:33:05 +1000 Subject: [PATCH 1/4] fix: parse multiple parameters --- lib/openapi_contracts/doc/path.rb | 2 +- spec/openapi_contracts/doc/path_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/openapi_contracts/doc/path.rb b/lib/openapi_contracts/doc/path.rb index 6dc7902..8dee6ba 100644 --- a/lib/openapi_contracts/doc/path.rb +++ b/lib/openapi_contracts/doc/path.rb @@ -22,7 +22,7 @@ def operations def path_regexp @path_regexp ||= begin - re = /\{(\S+)\}/ + re = /\{([^\}]+)\}/ @path.gsub(re) { |placeholder| placeholder.match(re) { |m| "(?<#{m[1]}>[^/]*)" } }.then { |str| Regexp.new(str) } diff --git a/spec/openapi_contracts/doc/path_spec.rb b/spec/openapi_contracts/doc/path_spec.rb index a06f550..337c414 100644 --- a/spec/openapi_contracts/doc/path_spec.rb +++ b/spec/openapi_contracts/doc/path_spec.rb @@ -7,6 +7,9 @@ 'paths' => { '/messages/{id}' => { 'parameters' => [id_param].compact + }, + '/messages/{id}/{second_id}' => { + 'parameters' => [id_param, second_id_param].compact } } } @@ -19,6 +22,15 @@ 'schema' => id_schema } end + + let(:second_id_param) do + { + 'name' => 'second_id', + 'in' => 'path', + 'required' => true, + 'schema' => id_schema + } + end let(:id_schema) { {} } describe '#dynamic?' do @@ -46,4 +58,12 @@ end end end + + describe "#path_regexp" do + subject { doc.with_path("/messages/{id}/{second_id}").path_regexp.match("/messages/123/abc").captures } + + it "matches both parameters" do + expect(subject).to eq(["123", "abc"]) + end + end end From bc8e01b6551b1e7a6c4fac949f9ec00527ce594d Mon Sep 17 00:00:00 2001 From: Don Buchanan <42761041+Don-CA@users.noreply.github.com> Date: Thu, 14 Sep 2023 16:57:56 +1000 Subject: [PATCH 2/4] lint: fix rubocop offenses --- spec/openapi_contracts/doc/path_spec.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/openapi_contracts/doc/path_spec.rb b/spec/openapi_contracts/doc/path_spec.rb index 337c414..0c81bc3 100644 --- a/spec/openapi_contracts/doc/path_spec.rb +++ b/spec/openapi_contracts/doc/path_spec.rb @@ -5,7 +5,7 @@ let(:schema) do { 'paths' => { - '/messages/{id}' => { + '/messages/{id}' => { 'parameters' => [id_param].compact }, '/messages/{id}/{second_id}' => { @@ -59,11 +59,11 @@ end end - describe "#path_regexp" do - subject { doc.with_path("/messages/{id}/{second_id}").path_regexp.match("/messages/123/abc").captures } + describe '#path_regexp' do + subject { doc.with_path('/messages/{id}/{second_id}').path_regexp.match('/messages/123/abc').captures } - it "matches both parameters" do - expect(subject).to eq(["123", "abc"]) + it 'matches both parameters' do + expect(subject).to eq %w(123 abc) end end end From 4b86e2d45e47e46c51792a0fd9c424d8a3e153ce Mon Sep 17 00:00:00 2001 From: Don Buchanan <42761041+Don-CA@users.noreply.github.com> Date: Fri, 15 Sep 2023 17:07:53 +1000 Subject: [PATCH 3/4] fix: match exact paths --- lib/openapi_contracts/doc/path.rb | 2 +- spec/openapi_contracts/doc/path_spec.rb | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/openapi_contracts/doc/path.rb b/lib/openapi_contracts/doc/path.rb index 8dee6ba..d4a17ad 100644 --- a/lib/openapi_contracts/doc/path.rb +++ b/lib/openapi_contracts/doc/path.rb @@ -25,7 +25,7 @@ def path_regexp re = /\{([^\}]+)\}/ @path.gsub(re) { |placeholder| placeholder.match(re) { |m| "(?<#{m[1]}>[^/]*)" } - }.then { |str| Regexp.new(str) } + }.then { |str| Regexp.new( "^#{str}$") } end end diff --git a/spec/openapi_contracts/doc/path_spec.rb b/spec/openapi_contracts/doc/path_spec.rb index 0c81bc3..985031c 100644 --- a/spec/openapi_contracts/doc/path_spec.rb +++ b/spec/openapi_contracts/doc/path_spec.rb @@ -60,10 +60,21 @@ end describe '#path_regexp' do - subject { doc.with_path('/messages/{id}/{second_id}').path_regexp.match('/messages/123/abc').captures } + context "when there are two parameters" do + subject { doc.with_path('/messages/{id}/{second_id}').path_regexp.match('/messages/123/abc').captures } - it 'matches both parameters' do - expect(subject).to eq %w(123 abc) + it 'matches both parameters' do + expect(subject).to eq %w(123 abc) + end + + end + + context "when there is a trailing path" do + subject { doc.with_path('/messages/{id}').path_regexp.match?('/messages/123/trailing') } + + it 'it does not match' do + expect(subject).to be false + end end end end From 7203d08d255c38aae8e8a7d89f980790471df7fa Mon Sep 17 00:00:00 2001 From: Don Buchanan <42761041+Don-CA@users.noreply.github.com> Date: Wed, 27 Sep 2023 13:51:38 +1000 Subject: [PATCH 4/4] lint: fix rubocup offenses --- lib/openapi_contracts/doc/path.rb | 2 +- spec/openapi_contracts/doc/path_spec.rb | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/openapi_contracts/doc/path.rb b/lib/openapi_contracts/doc/path.rb index d4a17ad..04272aa 100644 --- a/lib/openapi_contracts/doc/path.rb +++ b/lib/openapi_contracts/doc/path.rb @@ -25,7 +25,7 @@ def path_regexp re = /\{([^\}]+)\}/ @path.gsub(re) { |placeholder| placeholder.match(re) { |m| "(?<#{m[1]}>[^/]*)" } - }.then { |str| Regexp.new( "^#{str}$") } + }.then { |str| Regexp.new("^#{str}$") } end end diff --git a/spec/openapi_contracts/doc/path_spec.rb b/spec/openapi_contracts/doc/path_spec.rb index 985031c..50f0b36 100644 --- a/spec/openapi_contracts/doc/path_spec.rb +++ b/spec/openapi_contracts/doc/path_spec.rb @@ -60,19 +60,18 @@ end describe '#path_regexp' do - context "when there are two parameters" do + context 'when there are two parameters' do subject { doc.with_path('/messages/{id}/{second_id}').path_regexp.match('/messages/123/abc').captures } it 'matches both parameters' do expect(subject).to eq %w(123 abc) end - end - context "when there is a trailing path" do + context 'when there is a trailing path' do subject { doc.with_path('/messages/{id}').path_regexp.match?('/messages/123/trailing') } - it 'it does not match' do + it 'does not match' do expect(subject).to be false end end