Skip to content

Commit f4d3a21

Browse files
committed
fix: insert middleware after Rack::runtime
1 parent 4352d7b commit f4d3a21

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

config/application.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ class Application < Rails::Application
3131
config.api_only = true
3232

3333
# Add the request and response validation middleware to the application.
34-
# TODO: This can be changed later to add middleware in exact position e.g config.middleware.after Rack::Runtime, OpenApiSchema::RequestValidatorMiddleware
35-
config.middleware.use ::OpenApiSchema::RequestValidatorMiddleware
36-
config.middleware.use ::OpenApiSchema::ResponseValidatorMiddleware
34+
config.middleware.insert_after Rack::Runtime, ::OpenApiSchema::RequestValidatorMiddleware
35+
config.middleware.insert_after Rack::Runtime, ::OpenApiSchema::ResponseValidatorMiddleware
3736
end
3837
end

lib/open_api_schema/response_validator_middleware.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,32 @@ class ResponseValidatorMiddleware
3131
# @param app [Object] The Rack application.
3232
def initialize(app)
3333
@app = app
34-
error_handler = Proc.new do |error, env|
34+
35+
error_handler = Proc.new do |error, env|
3536
logger = Logger.new(Rails.root.join("log", "committee_validation.log"))
3637
logger.error("Committee Validation Error: #{error.message}")
3738
end
3839

3940
@response_validator = Committee::Middleware::ResponseValidation.new(app, schema_path: "docs/openapi.json", ignore_error: true, error_handler: error_handler)
4041
end
4142

42-
# Sets up the middleware to validate the response schema if the "VALIDATE_SCHEMA" header is present.
43+
# Sets up the middleware to validate the response schema if the "VALIDATE_SCHEMA" header is present log the error.
4344
#
4445
# @param env [Hash] The Rack environment hash.
4546
# @return [Array] The status, headers, and response.
4647
def call(env)
4748
status, headers, response = @app.call(env)
4849

4950
if condition_for_response_validation?(env)
50-
status, headers, response = @response_validator.call(env)
51+
@response_validator.call(env)
5152
end
5253

5354
[ status, headers, response ]
5455
end
5556

5657
private
5758
def condition_for_response_validation?(env)
58-
true # env.key?("HTTP_VALIDATE_SCHEMA")
59+
env.key?("HTTP_VALIDATE_SCHEMA")
5960
end
6061
end
6162
end

0 commit comments

Comments
 (0)