Skip to content

Commit

Permalink
requestBody should not merge requestBody from error examples (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Dec 8, 2023
1 parent de5106c commit 27dd2a9
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/rspec/openapi/schema_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def build_parameter_name(key, value)
def build_request_body(record)
return nil if record.request_content_type.nil?
return nil if record.request_params.empty?
return nil if record.status >= 400

{
content: {
Expand Down
26 changes: 26 additions & 0 deletions spec/integration_tests/rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
}

class TablesIndexTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!

# Patch minitest's ordering of examples to match RSpec's
Expand Down Expand Up @@ -63,6 +64,7 @@ def test_does_not_return_tables_if_unauthorized
end

class TablesShowTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!

# Patch minitest's ordering of examples to match RSpec's
Expand All @@ -88,6 +90,7 @@ def test_returns_a_table
end

class TablesCreateTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!

test 'returns a table' do
Expand All @@ -98,9 +101,27 @@ class TablesCreateTest < ActionDispatch::IntegrationTest
}.to_json
assert_response 201
end

test 'fails to create a table' do
post '/tables', headers: { authorization: 'k0kubun', 'Content-Type': 'application/json' }, params: {
name: 'some_invalid_name',
description: 'description',
database_id: 2,
}.to_json
assert_response 422
end

test 'fails to create a table (2)' do
post '/tables', headers: { authorization: 'k0kubun', 'Content-Type': 'application/json' }, params: {
description: 'description',
database_id: 2,
}.to_json
assert_response 422
end
end

class TablesUpdateTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!

test 'returns a table' do
Expand All @@ -110,6 +131,7 @@ class TablesUpdateTest < ActionDispatch::IntegrationTest
end

class TablesDestroyTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!

test 'returns a table' do
Expand All @@ -124,6 +146,7 @@ class TablesDestroyTest < ActionDispatch::IntegrationTest
end

class ImageTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!

test 'returns a image payload' do
Expand Down Expand Up @@ -174,6 +197,7 @@ class ImageTest < ActionDispatch::IntegrationTest
end

class ExtraRoutesTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!

test 'returns the block content' do
Expand All @@ -183,6 +207,7 @@ class ExtraRoutesTest < ActionDispatch::IntegrationTest
end

class EngineTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!

test 'returns some content from the engine' do
Expand All @@ -192,6 +217,7 @@ class EngineTest < ActionDispatch::IntegrationTest
end

class EngineExtraRoutesTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!

test 'returns the block content' do
Expand Down
1 change: 1 addition & 0 deletions spec/integration_tests/roda_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class RodaTest < Minitest::Test
include Rack::Test::Methods

i_suck_and_my_tests_are_order_dependent!
openapi!

def app
Expand Down
6 changes: 5 additions & 1 deletion spec/rails/app/controllers/tables_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ def show
end

def create
render json: find_table, status: 201
if params[:name].blank? || params[:name] == 'some_invalid_name'
render json: { error: 'invalid name parameter' }, status: 422
else
render json: find_table, status: 201
end
end

def update
Expand Down
21 changes: 21 additions & 0 deletions spec/rails/doc/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,27 @@
}
}
}
},
"422": {
"description": "fails to create a table (2)",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"error": {
"type": "string"
}
},
"required": [
"error"
]
},
"example": {
"error": "invalid name parameter"
}
}
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions spec/rails/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,19 @@ paths:
storage_size: 12.3
created_at: '2020-07-17T00:00:00+00:00'
updated_at: '2020-07-17T00:00:00+00:00'
'422':
description: fails to create a table (2)
content:
application/json:
schema:
type: object
properties:
error:
type: string
required:
- error
example:
error: invalid name parameter
"/tables/{id}":
get:
summary: show
Expand Down
17 changes: 17 additions & 0 deletions spec/requests/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@
}.to_json
expect(response.status).to eq(201)
end

it 'fails to create a table' do
post '/tables', headers: { authorization: 'k0kubun', 'Content-Type': 'application/json' }, params: {
description: 'description',
database_id: 2,
}.to_json
expect(response.status).to eq(422)
end

it 'fails to create a table (2)' do
post '/tables', headers: { authorization: 'k0kubun', 'Content-Type': 'application/json' }, params: {
name: 'some_invalid_name',
description: 'description',
database_id: 2,
}.to_json
expect(response.status).to eq(422)
end
end

describe '#update' do
Expand Down

0 comments on commit 27dd2a9

Please sign in to comment.