diff --git a/lib/committee/schema_validator/open_api_3.rb b/lib/committee/schema_validator/open_api_3.rb index b1483b13..ebee996d 100644 --- a/lib/committee/schema_validator/open_api_3.rb +++ b/lib/committee/schema_validator/open_api_3.rb @@ -50,6 +50,10 @@ def link_exist? !@operation_object.nil? end + # Expose the operation matched for the request, or nil if none matched + # @return [Committee::SchemaValidator::OpenAPI3::OperationWrapper, nil] + attr_reader :operation_object + private attr_reader :validator_option diff --git a/test/schema_validator/open_api_3_test.rb b/test/schema_validator/open_api_3_test.rb new file mode 100644 index 00000000..c33b9779 --- /dev/null +++ b/test/schema_validator/open_api_3_test.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require "test_helper" + +describe Committee::SchemaValidator::OpenAPI3 do + before do + @router = open_api_3_schema.build_router(schema: open_api_3_schema) + end + + def validator_for(path, method) + request = Rack::Request.new({ "REQUEST_METHOD" => method, "PATH_INFO" => path, "rack.input" => StringIO.new("") }) + @router.build_schema_validator(request) + end + + describe "#operation_object" do + it "returns the matched operation wrapper" do + operation_object = validator_for("/validate", "POST").operation_object + + assert_kind_of Committee::SchemaValidator::OpenAPI3::OperationWrapper, operation_object + assert_equal "/validate", operation_object.original_path + end + + it "returns nil when the request matches no operation" do + assert_nil validator_for("/no-such-path", "GET").operation_object + end + end +end