Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse multiple parameters #69

Merged
merged 4 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/openapi_contracts/doc/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ 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) }
}.then { |str| Regexp.new("^#{str}$") }
end
end

Expand Down
32 changes: 31 additions & 1 deletion spec/openapi_contracts/doc/path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
let(:schema) do
{
'paths' => {
'/messages/{id}' => {
'/messages/{id}' => {
'parameters' => [id_param].compact
},
'/messages/{id}/{second_id}' => {
'parameters' => [id_param, second_id_param].compact
}
}
}
Expand All @@ -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
Expand Down Expand Up @@ -46,4 +58,22 @@
end
end
end

describe '#path_regexp' 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
subject { doc.with_path('/messages/{id}').path_regexp.match?('/messages/123/trailing') }

it 'does not match' do
expect(subject).to be false
end
end
end
end