Skip to content

Commit

Permalink
correctly normalize rails paths with optional bound params
Browse files Browse the repository at this point in the history
  • Loading branch information
mercedesb committed Jan 10, 2024
1 parent 8b60147 commit 38b3f01
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rspec/openapi/schema_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def adjust_params(value)
end

def normalize_path(path)
path.gsub(%r{/:([^:/]+)}, '/{\1}')
path.gsub(%r{/:([^:/)]+)}, '/{\1}')
end

def normalize_content_type(content_type)
Expand Down
15 changes: 15 additions & 0 deletions spec/integration_tests/rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ class ImageTest < ActionDispatch::IntegrationTest
end
end

class OptionalParamTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!

test 'returns a payload when no id param provided' do
get '/optional_params'
assert_response 200
end

test 'returns a payload when id param is provided' do
get '/optional_params/17'
assert_response 200
end
end

class SecretKeyTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!
Expand Down
5 changes: 5 additions & 0 deletions spec/rails/app/controllers/optional_params_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class OptionalParamsController < ApplicationController
def display
render json: { id: params[:id] || 'id' }
end
end
2 changes: 2 additions & 0 deletions spec/rails/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@
get '/test_block' => ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['A TEST']] }

get '/secret_items' => 'secret_items#index'

get 'optional_params(/:id)', to: 'optional_params#display'
end
end
42 changes: 42 additions & 0 deletions spec/rails/doc/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,48 @@
}
}
},
"/optional_params(/{id})": {
"get": {
"summary": "display",
"tags": [
"OptionalParam"
],
"responses": {
"200": {
"description": "returns a payload when id param is provided",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
},
"required": [
"id"
]
},
"example": {
"id": "17"
}
}
}
}
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": {
"type": "integer"
},
"example": 17
}
]
}
},
"/secret_items": {
"get": {
"summary": "index",
Expand Down
26 changes: 26 additions & 0 deletions spec/rails/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,32 @@ paths:
schema:
type: string
example: ANOTHER TEST
"/optional_params(/{id})":
get:
summary: display
tags:
- OptionalParam
parameters:
- name: id
in: path
required: true
schema:
type: integer
example: 17
responses:
'200':
description: returns a payload when id param is provided
content:
application/json:
schema:
type: object
properties:
id:
type: string
required:
- id
example:
id: '17'
"/secret_items":
get:
summary: index
Expand Down
14 changes: 14 additions & 0 deletions spec/requests/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,20 @@
end
end

RSpec.describe 'OptionalParams', type: :request do
describe '#display' do
it 'returns a payload when no id param provided' do
get '/optional_params'
expect(response.status).to eq(200)
end

it 'returns a payload when id param is provided' do
get '/optional_params/17'
expect(response.status).to eq(200)
end
end
end

RSpec.describe 'SecretKey securityScheme',
type: :request,
openapi: { security: [{ 'SecretApiKeyAuth' => [] }] } do
Expand Down

0 comments on commit 38b3f01

Please sign in to comment.