From 72b962927f096f71c0029b61013017eea37a9569 Mon Sep 17 00:00:00 2001 From: Felix Tscheulin Date: Fri, 17 Dec 2021 11:39:13 +0100 Subject: [PATCH] Support Http OPTIONS method --- .../open_api_3/operation_wrapper.rb | 2 +- test/data/openapi3/normal.yaml | 26 +++++++++++++++++++ .../request_validation_open_api_3_test.rb | 12 +-------- .../response_validation_open_api_3_test.rb | 2 +- .../open_api_3/operation_wrapper_test.rb | 13 ++++++++++ 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/committee/schema_validator/open_api_3/operation_wrapper.rb b/lib/committee/schema_validator/open_api_3/operation_wrapper.rb index c5d6e9f2..00fce1bd 100644 --- a/lib/committee/schema_validator/open_api_3/operation_wrapper.rb +++ b/lib/committee/schema_validator/open_api_3/operation_wrapper.rb @@ -43,7 +43,7 @@ def validate_request_params(params, headers, validator_option) ret, err = case request_operation.http_method when 'get', 'delete', 'head' validate_get_request_params(params, headers, validator_option) - when 'post', 'put', 'patch' + when 'post', 'put', 'patch', 'options' validate_post_request_params(params, headers, validator_option) else raise "Committee OpenAPI3 not support #{request_operation.http_method} method" diff --git a/test/data/openapi3/normal.yaml b/test/data/openapi3/normal.yaml index dcea810b..5c127c26 100644 --- a/test/data/openapi3/normal.yaml +++ b/test/data/openapi3/normal.yaml @@ -394,6 +394,26 @@ paths: properties: string: type: string + options: + description: validate options metodhd + requestBody: + content: + application/json: + schema: + type: object + properties: + integer: + type: integer + responses: + '200': + description: success + content: + application/json: + schema: + type: object + properties: + string: + type: string /validate_content_types: post: @@ -570,6 +590,12 @@ paths: responses: '200': $ref: '#/components/responses/header_integer_required' + options: + parameters: + - $ref: '#/components/parameters/header_integer_required' + responses: + '200': + $ref: '#/components/responses/header_integer_required' /get_endpoint_with_requered_parameter: get: diff --git a/test/middleware/request_validation_open_api_3_test.rb b/test/middleware/request_validation_open_api_3_test.rb index 4483b9bb..b48194d5 100644 --- a/test/middleware/request_validation_open_api_3_test.rb +++ b/test/middleware/request_validation_open_api_3_test.rb @@ -457,16 +457,6 @@ def app assert_equal 204, last_response.status end - it "OpenAPI3 raise not support method" do - @app = new_rack_app(schema: open_api_3_schema) - - e = assert_raises(RuntimeError) { - options "/characters", {} - } - - assert_equal 'Committee OpenAPI3 not support options method', e.message - end - describe 'check header' do [ { check_header: true, description: 'valid value', value: 1, expected: { status: 200 } }, @@ -482,7 +472,7 @@ def app value = h[:value] expected = h[:expected] describe "when #{check_header}" do - %w(get post put patch delete).each do |method| + %w(get post put patch delete options).each do |method| describe method do describe description do it (expected[:error].nil? ? 'should pass' : 'should fail') do diff --git a/test/middleware/response_validation_open_api_3_test.rb b/test/middleware/response_validation_open_api_3_test.rb index 06974fc6..6707b8f8 100644 --- a/test/middleware/response_validation_open_api_3_test.rb +++ b/test/middleware/response_validation_open_api_3_test.rb @@ -142,7 +142,7 @@ def app header = h[:header] expected = h[:expected] describe "when #{check_header}" do - %w(get post put patch delete).each do |method| + %w(get post put patch delete options).each do |method| describe method do describe description do if expected[:error].nil? diff --git a/test/schema_validator/open_api_3/operation_wrapper_test.rb b/test/schema_validator/open_api_3/operation_wrapper_test.rb index 3bd2577f..0fa4ac56 100644 --- a/test/schema_validator/open_api_3/operation_wrapper_test.rb +++ b/test/schema_validator/open_api_3/operation_wrapper_test.rb @@ -177,6 +177,19 @@ def operation_object end end + it 'support options method' do + @method = "options" + operation_object.validate_request_params({"integer" => 1}, HEADER, @validator_option) + + e = assert_raises(Committee::InvalidRequest) { + operation_object.validate_request_params({"integer" => "str"}, HEADER, @validator_option) + } + + assert_match(/expected integer, but received String: "str"/i, e.message) + assert_kind_of(OpenAPIParser::OpenAPIError, e.original_error) + end + + describe '#content_types' do it 'returns supported content types' do @path = '/validate_content_types'