Skip to content

Commit

Permalink
Merge pull request #318 from interagent/feature/path_hash
Browse files Browse the repository at this point in the history
save path parameter to path hash
  • Loading branch information
ota42y authored May 15, 2021
2 parents 724cf50 + 1825363 commit f009322
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 28 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ Non-boolean options:
|schema_path| String | supported | supported | Defines the location of the schema file to use for validation. |
|error_handler| Proc Object | supported | supported | A proc which will be called when error occurs. Take an Error instance as first argument, and request.env as second argument. (e.g. `-> (ex, env) { Raven.capture_exception(ex, extra: { rack_env: env }) }`) |
|accept_request_filter | Proc Object | supported | supported | A proc that accepts a Request and returns a boolean. It indicates whether to validate the current request, or not. (e.g. `-> (request) { request.path.start_with?('/something') }`) |
|params_key| String | supported | supported | Save checked parameter value to request.env using this key. Default value is `committee.params` |
|params_key| String | supported | supported | Save checked and merged parameter value to request.env using this key. Default value is `committee.params` |
|headers_key| String | supported | supported | Save checked header value to request.env using this key. Default value is `committee.headers` |
|query_hash_key| String | supported | supported | Save checked query parameter value to request.env using this key. Default value is `rack.request.query_hash` but we will change `committee.query_hash` in next version |
|path_hash_key| String | supported | supported | Save checked path parameter value to request.env using this key. Default value is `committee.path_hash` |


Note that Hyper-Schema and OpenAPI 2 get the same defaults for options.

Expand Down
3 changes: 3 additions & 0 deletions examples/openapi3_rails/app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ class PostsController < ApplicationController
def create
render status: 201, json: {title: "abc", page: params[:page], year: params[:year]}
end
def create_with_id
render status: 201, json: {title: "abc", page: params[:page], year: params[:year]}
end
end
1 change: 1 addition & 0 deletions examples/openapi3_rails/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html

post :posts, to: 'posts#create'
post '/posts/:id', to: 'posts#create_with_id'
end
52 changes: 27 additions & 25 deletions lib/committee/request_unpacker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

module Committee
class RequestUnpacker
class << self
# Creates a Hash with indifferent access.
#
# (Copied from Sinatra)
def indifferent_hash
Hash.new { |hash,key| hash[key.to_s] if Symbol === key }
end

# Enable string or symbol key access to the nested params hash.
#
# (Copied from Sinatra)
def indifferent_params(object)
case object
when Hash
new_hash = indifferent_hash
object.each { |key, value| new_hash[key] = indifferent_params(value) }
new_hash
when Array
object.map { |item| indifferent_params(item) }
else
object
end
end
end

def initialize(request, options={})
@request = request

Expand Down Expand Up @@ -41,37 +66,14 @@ def call
end

if @allow_query_params
[indifferent_params(@request.GET).merge(params), headers]
[self.class.indifferent_params(@request.GET).merge(params), headers]
else
[params, headers]
end
end

private

# Creates a Hash with indifferent access.
#
# (Copied from Sinatra)
def indifferent_hash
Hash.new { |hash,key| hash[key.to_s] if Symbol === key }
end

# Enable string or symbol key access to the nested params hash.
#
# (Copied from Sinatra)
def indifferent_params(object)
case object
when Hash
new_hash = indifferent_hash
object.each { |key, value| new_hash[key] = indifferent_params(value) }
new_hash
when Array
object.map { |item| indifferent_params(item) }
else
object
end
end

def parse_json
return nil if @request.request_method == "GET" && !@allow_get_body

Expand All @@ -87,7 +89,7 @@ def parse_json
raise BadRequest,
"Invalid JSON input. Require object with parameters as keys."
end
indifferent_params(hash)
self.class.indifferent_params(hash)
end

def headers
Expand Down
5 changes: 3 additions & 2 deletions lib/committee/schema_validator/open_api_3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def initialize(router, request, validator_option)
def request_validate(request)
return unless link_exist?

path_params = validator_option.coerce_path_params ? coerce_path_params : {}
path_params = validator_option.coerce_path_params ? coerce_path_params : Committee::RequestUnpacker.indifferent_hash
request.env[validator_option.path_hash_key] = path_params

request_unpack(request)

Expand Down Expand Up @@ -58,7 +59,7 @@ def coerce_form_params(_parameter)
attr_reader :validator_option

def coerce_path_params
@operation_object.coerce_path_parameter(@validator_option)
Committee::RequestUnpacker.indifferent_params(@operation_object.coerce_path_parameter(@validator_option))
end

def request_schema_validation(request)
Expand Down
2 changes: 2 additions & 0 deletions lib/committee/schema_validator/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Option
attr_reader :headers_key,
:params_key,
:query_hash_key,
:path_hash_key,
:prefix

def initialize(options, schema, schema_type)
Expand All @@ -34,6 +35,7 @@ def initialize(options, schema, schema_type)
else
options.fetch(:query_hash_key)
end
@path_hash_key = options[:path_hash_key] || "committee.path_hash"

@prefix = options[:prefix]

Expand Down
26 changes: 26 additions & 0 deletions test/data/openapi3/normal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,32 @@ paths:
schema:
$ref: referee.yaml#/components/schemas/referred_schema

/parameter_option_test/{integer}:
post:
description: parameter option test
parameters:
- name: integer
in: path
required: true
schema:
type: integer
- name: integer
in: query
required: true
schema:
type: integer
requestBody:
content:
application/json:
schema:
type: object
properties:
integer:
type: integer
responses:
'204':
description: sample of remote schema reference

components:
schemas:
nested_array:
Expand Down
14 changes: 14 additions & 0 deletions test/middleware/request_validation_open_api_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,20 @@ def app
get "/coerce_path_params/1"
end

it "corce string and save path hash" do
@app = new_rack_app_with_lambda(lambda do |env|
assert_equal env['committee.params']['integer'], 21
assert_equal env['committee.params'][:integer], 21
assert_equal env['committee.path_hash']['integer'], 21
assert_equal env['committee.path_hash'][:integer], 21
[204, {}, []]
end, schema: open_api_3_schema)

header "Content-Type", "application/json"
post '/parameter_option_test/21'
assert_equal 204, last_response.status
end

it "OpenAPI3 raise not support method" do
@app = new_rack_app(schema: open_api_3_schema)

Expand Down

0 comments on commit f009322

Please sign in to comment.