From 6de758350e2a7d0b1a7a49720637451d177cf8a3 Mon Sep 17 00:00:00 2001 From: chibicco Date: Sat, 11 May 2024 16:18:56 +0900 Subject: [PATCH] Rename options from permit_blank_structures to allow_blank_structures --- lib/committee/schema_validator/hyper_schema.rb | 2 +- .../hyper_schema/response_validator.rb | 6 +++--- lib/committee/schema_validator/option.rb | 8 ++++---- test/middleware/response_validation_test.rb | 12 ++++++------ 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/committee/schema_validator/hyper_schema.rb b/lib/committee/schema_validator/hyper_schema.rb index 320ba0ce..1419b01e 100644 --- a/lib/committee/schema_validator/hyper_schema.rb +++ b/lib/committee/schema_validator/hyper_schema.rb @@ -33,7 +33,7 @@ def response_validate(status, headers, response, _test_method = false) data = JSON.parse(full_body) if parse_to_json end - Committee::SchemaValidator::HyperSchema::ResponseValidator.new(link, validate_success_only: validator_option.validate_success_only, permit_blank_structures: validator_option.permit_blank_structures).call(status, headers, data) + Committee::SchemaValidator::HyperSchema::ResponseValidator.new(link, validate_success_only: validator_option.validate_success_only, allow_blank_structures: validator_option.allow_blank_structures).call(status, headers, data) end def link_exist? diff --git a/lib/committee/schema_validator/hyper_schema/response_validator.rb b/lib/committee/schema_validator/hyper_schema/response_validator.rb index 59bb77df..d396219c 100644 --- a/lib/committee/schema_validator/hyper_schema/response_validator.rb +++ b/lib/committee/schema_validator/hyper_schema/response_validator.rb @@ -4,12 +4,12 @@ module Committee module SchemaValidator class HyperSchema class ResponseValidator - attr_reader :validate_success_only, :permit_blank_structures + attr_reader :allow_blank_structures, :validate_success_only def initialize(link, options = {}) @link = link @validate_success_only = options[:validate_success_only] - @permit_blank_structures = options[:permit_blank_structures] + @allow_blank_structures = options[:allow_blank_structures] @validator = JsonSchema::Validator.new(target_schema(link)) end @@ -40,7 +40,7 @@ def call(status, headers, data) return if data == nil end - if permit_blank_structures && @link.is_a?(Committee::Drivers::OpenAPI2::Link) && !@link.target_schema + if allow_blank_structures && @link.is_a?(Committee::Drivers::OpenAPI2::Link) && !@link.target_schema return if data.nil? end diff --git a/lib/committee/schema_validator/option.rb b/lib/committee/schema_validator/option.rb index d428eee8..8b2fae77 100644 --- a/lib/committee/schema_validator/option.rb +++ b/lib/committee/schema_validator/option.rb @@ -4,7 +4,8 @@ module Committee module SchemaValidator class Option # Boolean Options - attr_reader :allow_form_params, + attr_reader :allow_blank_structures, + :allow_form_params, :allow_get_body, :allow_query_params, :check_content_type, @@ -17,8 +18,7 @@ class Option :optimistic_json, :validate_success_only, :parse_response_by_content_type, - :parameter_overwite_by_rails_rule, - :permit_blank_structures + :parameter_overwite_by_rails_rule # Non-boolean options: attr_reader :headers_key, @@ -39,6 +39,7 @@ def initialize(options, schema, schema_type) @prefix = options[:prefix] # Boolean options and have a common value by default + @allow_blank_structures = options.fetch(:allow_blank_structures, false) @allow_form_params = options.fetch(:allow_form_params, true) @allow_query_params = options.fetch(:allow_query_params, true) @check_content_type = options.fetch(:check_content_type, true) @@ -47,7 +48,6 @@ def initialize(options, schema, schema_type) @optimistic_json = options.fetch(:optimistic_json, false) @parse_response_by_content_type = options.fetch(:parse_response_by_content_type, true) @parameter_overwite_by_rails_rule = options.fetch(:parameter_overwite_by_rails_rule, true) - @permit_blank_structures = options.fetch(:permit_blank_structures, false) # Boolean options and have a different value by default @allow_get_body = options.fetch(:allow_get_body, schema.driver.default_allow_get_body) diff --git a/test/middleware/response_validation_test.rb b/test/middleware/response_validation_test.rb index 6901726c..c5d743df 100644 --- a/test/middleware/response_validation_test.rb +++ b/test/middleware/response_validation_test.rb @@ -136,24 +136,24 @@ def app assert_equal 200, last_response.status end - it "passes through a valid response for OpenAPI when data=nil, target_schema=empty, permit_blank_structures=true" do + it "passes through a valid response for OpenAPI when data=nil, target_schema=empty, allow_blank_structures=true" do @app = new_rack_app("null", {}, - permit_blank_structures: true, schema: open_api_2_schema) + allow_blank_structures: true, schema: open_api_2_schema) get "/api/pets/cat" assert_equal 200, last_response.status end - it "invalid responses for OpenAPI when data=nil, target_schema=empty, permit_blank_structures=false" do + it "invalid responses for OpenAPI when data=nil, target_schema=empty, allow_blank_structures=false" do @app = new_rack_app("null", {}, - permit_blank_structures: false, schema: open_api_2_schema) + allow_blank_structures: false, schema: open_api_2_schema) get "/api/pets/cat" assert_equal 500, last_response.status assert_match(/Invalid response/i, last_response.body) end - it "passes through a valid response for OpenAPI when data=nil, target_schema=present, permit_blank_structures=true" do + it "passes through a valid response for OpenAPI when data=nil, target_schema=present, allow_blank_structures=true" do @app = new_rack_app("null", {}, - permit_blank_structures: true, schema: open_api_2_schema) + allow_blank_structures: true, schema: open_api_2_schema) get "/api/pets/dog" assert_equal 500, last_response.status assert_match(/nil is not an array/i, last_response.body)