Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/committee/schema_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ def request_media_type(request)
def build_prefix_regexp(prefix)
return nil unless prefix

/\A#{Regexp.escape(prefix)}/.freeze
if prefix == "/" || prefix.end_with?("/")
/\A#{Regexp.escape(prefix)}/.freeze
else
/\A#{Regexp.escape(prefix)}(?=\/|\z)/.freeze
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/committee/schema_validator/hyper_schema/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class HyperSchema
class Router
def initialize(schema, validator_option)
@prefix = validator_option.prefix
@prefix_regexp = /\A#{Regexp.escape(@prefix)}/.freeze if @prefix
@prefix_regexp = ::Committee::SchemaValidator.build_prefix_regexp(@prefix)
@schema = schema

@validator_option = validator_option
Expand Down
8 changes: 8 additions & 0 deletions test/middleware/request_validation_open_api_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ def app
assert_equal 200, last_response.status
end

it "ignores similar prefix paths outside the prefix in strict mode" do
@app = new_rack_app(prefix: "/v1", schema: open_api_3_schema, strict: true)
params = { "string_post_1" => 1 }
header "Content-Type", "application/json"
post "/v11/characters", JSON.generate(params)
assert_equal 200, last_response.status
end

it "don't check prefix with no option" do
@app = new_rack_app(schema: open_api_3_schema)
params = { "string_post_1" => 1 }
Expand Down
7 changes: 7 additions & 0 deletions test/middleware/request_validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ def app
assert_equal 200, last_response.status
end

it "ignores similar prefix paths outside the prefix in strict mode" do
@app = new_rack_app(prefix: "/v1", schema: hyper_schema, strict: true)
header "Content-Type", "application/json"
post "/v11/apps", JSON.generate({ "name" => 1 })
assert_equal 200, last_response.status
end

it "routes to paths not in schema" do
@app = new_rack_app(schema: hyper_schema)
get "/not-a-resource"
Expand Down
5 changes: 5 additions & 0 deletions test/schema_validator/hyper_schema/router_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
refute_nil link
end

it "does not include a similar prefix path segment" do
link, _ = hyper_schema_router(prefix: "/kpi").includes?("/kpi2/apps")
assert_nil link
end

it "provides named parameters" do
link, param_matches = open_api_2_router.find_link("GET", "/api/pets/fido")
refute_nil link
Expand Down
8 changes: 8 additions & 0 deletions test/schema_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@
media_type = Committee::SchemaValidator.request_media_type(request)
assert_equal 'multipart/form-data', media_type
end

it "builds prefix regexp with a path segment boundary" do
regexp = Committee::SchemaValidator.build_prefix_regexp("/v1")

assert regexp.match?("/v1")
assert regexp.match?("/v1/characters")
refute regexp.match?("/v11/characters")
end
end