committee 5.6.3 → 5.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/committee/drivers/open_api_2/driver.rb +16 -0
- data/lib/committee/drivers/open_api_2/link.rb +2 -0
- data/lib/committee/drivers/open_api_3/schema.rb +6 -1
- data/lib/committee/drivers.rb +15 -8
- data/lib/committee/middleware/options/response_validation.rb +4 -2
- data/lib/committee/middleware/response_validation.rb +37 -10
- data/lib/committee/middleware/stub.rb +1 -1
- data/lib/committee/request_unpacker.rb +19 -9
- data/lib/committee/schema_locations.rb +96 -0
- data/lib/committee/schema_validator/hyper_schema/request_validator.rb +1 -1
- data/lib/committee/schema_validator/hyper_schema/response_validator.rb +14 -4
- data/lib/committee/schema_validator/hyper_schema.rb +5 -4
- data/lib/committee/schema_validator/open_api_3/operation_wrapper.rb +76 -40
- data/lib/committee/schema_validator/open_api_3/parameter_deserializer.rb +66 -20
- data/lib/committee/schema_validator/open_api_3/request_validator.rb +2 -3
- data/lib/committee/schema_validator/open_api_3/response_validator.rb +2 -1
- data/lib/committee/schema_validator/open_api_3/router.rb +1 -3
- data/lib/committee/schema_validator/open_api_3.rb +26 -18
- data/lib/committee/schema_validator/option.rb +2 -1
- data/lib/committee/schema_validator.rb +1 -5
- data/lib/committee/test/except_parameter.rb +4 -4
- data/lib/committee/test/methods.rb +14 -1
- data/lib/committee/test/schema_coverage.rb +11 -2
- data/lib/committee/utils.rb +6 -0
- data/lib/committee/validation_error.rb +1 -1
- data/lib/committee/version.rb +1 -1
- data/lib/committee.rb +1 -0
- data/test/drivers/open_api_2/driver_test.rb +13 -0
- data/test/drivers_test.rb +39 -8
- data/test/middleware/options/response_validation_test.rb +15 -0
- data/test/middleware/request_validation_open_api_3_test.rb +670 -8
- data/test/middleware/request_validation_test.rb +50 -0
- data/test/middleware/response_validation_open_api_3_test.rb +162 -0
- data/test/middleware/response_validation_test.rb +134 -0
- data/test/middleware/stub_test.rb +10 -0
- data/test/request_unpacker_test.rb +34 -0
- data/test/schema_location_test.rb +111 -0
- data/test/schema_validator/hyper_schema/response_validator_test.rb +16 -0
- data/test/schema_validator/open_api_3/operation_wrapper_test.rb +95 -0
- data/test/schema_validator/open_api_3/parameter_deserializer_test.rb +145 -8
- data/test/schema_validator/open_api_3_test.rb +27 -0
- data/test/schema_validator_test.rb +10 -0
- data/test/test/methods_test.rb +200 -0
- data/test/test/schema_coverage_test.rb +34 -0
- data/test/test_helper.rb +23 -0
- data/test/utils_test.rb +28 -0
- data/test/validation_error_test.rb +1 -1
- metadata +22 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5dc1e1ab5d960d49c89179f8c7370ce7994dd2ea0dc3f36b4abe4c12c52cf917
|
|
4
|
+
data.tar.gz: 4590472b4729e21d29f57ca2981bd78f7b9618cad98e14ceb0af03fbb2f22445
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: aa069f74fbe38835a2cc1c9c7ef4c79f2f439735e9eba2b1384e3f9d0b450c774a675223b0e2872b1ff9ee2decd6773bb83323930708e7d87a1d54ef2b7eea21
|
|
7
|
+
data.tar.gz: cc51dedeb07dcd2f03f2a5eda8f6db4125abd1cdbc6886200a9f610b81ee68762cf4eee9c54193757dcc45d7e51faa4965f1427818a63336f87124b034603e23
|
|
@@ -127,6 +127,20 @@ module Committee
|
|
|
127
127
|
[schema, store]
|
|
128
128
|
end
|
|
129
129
|
|
|
130
|
+
def resolve_parameter_references(link_data, data)
|
|
131
|
+
return link_data unless link_data["parameters"]
|
|
132
|
+
|
|
133
|
+
parameters = link_data["parameters"].map do |parameter|
|
|
134
|
+
ref = parameter["$ref"]
|
|
135
|
+
next parameter unless ref
|
|
136
|
+
|
|
137
|
+
name = ref.delete_prefix("#/parameters/").gsub("~1", "/").gsub("~0", "~")
|
|
138
|
+
data.fetch("parameters").fetch(name)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
link_data.merge("parameters" => parameters)
|
|
142
|
+
end
|
|
143
|
+
|
|
130
144
|
def parse_routes!(data, schema, store)
|
|
131
145
|
routes = {}
|
|
132
146
|
|
|
@@ -152,6 +166,8 @@ module Committee
|
|
|
152
166
|
link.href = href
|
|
153
167
|
link.media_type = schema.produces
|
|
154
168
|
link.method = method
|
|
169
|
+
link_data = resolve_parameter_references(link_data, data)
|
|
170
|
+
link.response_definition = link_data['responses']
|
|
155
171
|
|
|
156
172
|
# Convert the spec's parameter pseudo-schemas into JSON schemas that
|
|
157
173
|
# we can use for some basic request validation.
|
|
@@ -6,6 +6,7 @@ module Committee
|
|
|
6
6
|
class Schema < ::Committee::Drivers::Schema
|
|
7
7
|
attr_reader :open_api
|
|
8
8
|
attr_reader :validator_option
|
|
9
|
+
attr_accessor :schema_locations
|
|
9
10
|
|
|
10
11
|
# @!attribute [r] open_api
|
|
11
12
|
# @return [OpenAPIParser::Schemas::OpenAPI]
|
|
@@ -30,10 +31,14 @@ module Committee
|
|
|
30
31
|
|
|
31
32
|
# OpenAPI3 only
|
|
32
33
|
def operation_object(path, method)
|
|
34
|
+
# 3.2 lookups keep the case to tell additionalOperations apart (`Get` vs `GET`);
|
|
35
|
+
# earlier documents only have lowercase method fields
|
|
36
|
+
method = method.to_s
|
|
37
|
+
method = method.downcase unless open_api.use_3_2_features?
|
|
33
38
|
request_operation = open_api.request_operation(method, path)
|
|
34
39
|
return nil unless request_operation
|
|
35
40
|
|
|
36
|
-
Committee::SchemaValidator::OpenAPI3::OperationWrapper.new(request_operation)
|
|
41
|
+
Committee::SchemaValidator::OpenAPI3::OperationWrapper.new(request_operation, schema_locations)
|
|
37
42
|
end
|
|
38
43
|
end
|
|
39
44
|
end
|
data/lib/committee/drivers.rb
CHANGED
|
@@ -31,7 +31,14 @@ module Committee
|
|
|
31
31
|
# @return [Committee::Driver]
|
|
32
32
|
def self.load_from_yaml(schema_path, parser_options: {})
|
|
33
33
|
data = YAML.respond_to?(:unsafe_load_file) ? YAML.unsafe_load_file(schema_path) : YAML.load_file(schema_path)
|
|
34
|
-
|
|
34
|
+
locations = Committee::SchemaLocations.new(schema_path, data)
|
|
35
|
+
schema = load_from_data(data, schema_path, parser_options: parser_options)
|
|
36
|
+
if schema.is_a?(Committee::Drivers::OpenAPI3::Schema)
|
|
37
|
+
schema.schema_locations = locations
|
|
38
|
+
elsif schema.is_a?(Committee::Drivers::OpenAPI2::Schema)
|
|
39
|
+
schema.routes.each_value { |routes| routes.each { |_, link| link.schema_locations = locations } }
|
|
40
|
+
end
|
|
41
|
+
schema
|
|
35
42
|
end
|
|
36
43
|
|
|
37
44
|
# load and build drive from file
|
|
@@ -55,7 +62,13 @@ module Committee
|
|
|
55
62
|
# @param [Hash] hash
|
|
56
63
|
# @return [Committee::Driver]
|
|
57
64
|
def self.load_from_data(hash, schema_path = nil, parser_options: {})
|
|
58
|
-
if hash['openapi']
|
|
65
|
+
if (version = hash['openapi'])
|
|
66
|
+
# release drops prerelease tags, so 3.3.0-rc1 counts as 3.3
|
|
67
|
+
version = Gem::Version.new(version).release
|
|
68
|
+
raise OpenAPI3Unsupported.new('Committee does not support OpenAPI 3.3+ yet') if version >= Gem::Version.new("3.3")
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
if version && version >= Gem::Version.new("3.0")
|
|
59
72
|
# From the next major version, we want to ensure `{ strict_reference_validation: true }`
|
|
60
73
|
# as a parser option here, but since it may break existing implementations, just warn
|
|
61
74
|
# if it is not explicitly set. See: https://github.com/interagent/committee/issues/343#issuecomment-997400329
|
|
@@ -68,12 +81,6 @@ module Committee
|
|
|
68
81
|
return Committee::Drivers::OpenAPI3::Driver.new.parse(openapi)
|
|
69
82
|
end
|
|
70
83
|
|
|
71
|
-
if (version = hash['openapi'])
|
|
72
|
-
if Gem::Version.new(version) >= Gem::Version.new("3.1")
|
|
73
|
-
raise OpenAPI3Unsupported.new('Committee does not support OpenAPI 3.1+ yet')
|
|
74
|
-
end
|
|
75
|
-
end
|
|
76
|
-
|
|
77
84
|
driver = if hash['swagger'] == '2.0'
|
|
78
85
|
Committee::Drivers::OpenAPI2::Driver.new
|
|
79
86
|
else
|
|
@@ -10,9 +10,10 @@ module Committee
|
|
|
10
10
|
attr_reader :parse_response_by_content_type
|
|
11
11
|
attr_reader :coerce_response_values
|
|
12
12
|
attr_reader :streaming_content_parsers
|
|
13
|
+
attr_reader :strict_response_content_type
|
|
13
14
|
|
|
14
15
|
# Default values
|
|
15
|
-
DEFAULTS = { strict: false, validate_success_only: true, parse_response_by_content_type: true, coerce_response_values: false }.freeze
|
|
16
|
+
DEFAULTS = { strict: false, validate_success_only: true, parse_response_by_content_type: true, coerce_response_values: false, strict_response_content_type: false }.freeze
|
|
16
17
|
|
|
17
18
|
def initialize(options = {})
|
|
18
19
|
super(options)
|
|
@@ -22,6 +23,7 @@ module Committee
|
|
|
22
23
|
@validate_success_only = options.fetch(:validate_success_only, DEFAULTS[:validate_success_only])
|
|
23
24
|
@parse_response_by_content_type = options.fetch(:parse_response_by_content_type, DEFAULTS[:parse_response_by_content_type])
|
|
24
25
|
@coerce_response_values = options.fetch(:coerce_response_values, DEFAULTS[:coerce_response_values])
|
|
26
|
+
@strict_response_content_type = options.fetch(:strict_response_content_type, DEFAULTS[:strict_response_content_type])
|
|
25
27
|
|
|
26
28
|
# Streaming
|
|
27
29
|
@streaming_content_parsers = options[:streaming_content_parsers] || {}
|
|
@@ -38,7 +40,7 @@ module Committee
|
|
|
38
40
|
end
|
|
39
41
|
|
|
40
42
|
def build_hash
|
|
41
|
-
super.merge(strict: @strict, validate_success_only: @validate_success_only, parse_response_by_content_type: @parse_response_by_content_type, coerce_response_values: @coerce_response_values, streaming_content_parsers: @streaming_content_parsers)
|
|
43
|
+
super.merge(strict: @strict, validate_success_only: @validate_success_only, parse_response_by_content_type: @parse_response_by_content_type, coerce_response_values: @coerce_response_values, streaming_content_parsers: @streaming_content_parsers, strict_response_content_type: @strict_response_content_type)
|
|
42
44
|
end
|
|
43
45
|
end
|
|
44
46
|
end
|
|
@@ -14,22 +14,47 @@ module Committee
|
|
|
14
14
|
|
|
15
15
|
def handle(request)
|
|
16
16
|
status, headers, response = @app.call(request.env)
|
|
17
|
+
# Rack 3 streams call-only bodies after the middleware returns.
|
|
18
|
+
return [status, headers, response] if response.respond_to?(:call) && !response.respond_to?(:each)
|
|
17
19
|
|
|
18
20
|
streaming_content_parser = retrieve_streaming_content_parser(headers)
|
|
19
21
|
|
|
20
22
|
if streaming_content_parser
|
|
23
|
+
original_response = response
|
|
24
|
+
streamed_response = String.new(encoding: Encoding::BINARY)
|
|
25
|
+
response = Enumerator.new do |yielder|
|
|
26
|
+
original_response.each do |chunk|
|
|
27
|
+
streamed_response << chunk.b
|
|
28
|
+
yielder << chunk
|
|
29
|
+
end
|
|
30
|
+
end
|
|
21
31
|
response = Rack::BodyProxy.new(response) do
|
|
22
32
|
begin
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
33
|
+
original_response.close if original_response.respond_to?(:close)
|
|
34
|
+
ensure
|
|
35
|
+
begin
|
|
36
|
+
validate(request, status, headers, streamed_response.force_encoding(Encoding::UTF_8), streaming_content_parser)
|
|
37
|
+
rescue => e
|
|
38
|
+
handle_exception(e, request.env)
|
|
26
39
|
|
|
27
|
-
|
|
40
|
+
raise e if @raise
|
|
41
|
+
end
|
|
28
42
|
end
|
|
29
43
|
end
|
|
30
44
|
else
|
|
45
|
+
validator = response_validator(request, status)
|
|
46
|
+
if validator
|
|
47
|
+
original_response = response
|
|
48
|
+
response = []
|
|
49
|
+
begin
|
|
50
|
+
original_response.each { |chunk| response << chunk }
|
|
51
|
+
ensure
|
|
52
|
+
original_response.close if original_response.respond_to?(:close)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
31
56
|
begin
|
|
32
|
-
|
|
57
|
+
validator&.response_validate(status, headers, response, @strict)
|
|
33
58
|
rescue Committee::InvalidResponse
|
|
34
59
|
handle_exception($!, request.env)
|
|
35
60
|
|
|
@@ -68,16 +93,18 @@ module Committee
|
|
|
68
93
|
end
|
|
69
94
|
|
|
70
95
|
def validate(request, status, headers, response, streaming_content_parser = nil)
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
96
|
+
response_validator(request, status)&.response_validate(status, headers, response, @strict, streaming_content_parser)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def response_validator(request, status)
|
|
100
|
+
validator = build_schema_validator(request)
|
|
101
|
+
validator if validator.link_exist? && self.class.validate?(status, validate_success_only)
|
|
75
102
|
end
|
|
76
103
|
|
|
77
104
|
def retrieve_streaming_content_parser(headers)
|
|
78
105
|
content_type_key = headers.keys.detect { |k| k.casecmp?('Content-Type') }
|
|
79
106
|
content_type = headers.fetch(content_type_key, nil)
|
|
80
|
-
@streaming_content_parsers[content_type]
|
|
107
|
+
@streaming_content_parsers[content_type] || @streaming_content_parsers[Rack::MediaType.type(content_type)]
|
|
81
108
|
end
|
|
82
109
|
|
|
83
110
|
def handle_exception(e, env)
|
|
@@ -24,7 +24,7 @@ module Committee
|
|
|
24
24
|
def handle(request)
|
|
25
25
|
link, _ = @router.find_request_link(request)
|
|
26
26
|
if link
|
|
27
|
-
headers = { "
|
|
27
|
+
headers = { "content-type" => "application/json" }
|
|
28
28
|
|
|
29
29
|
data, schema = cache(link) do
|
|
30
30
|
Committee::SchemaValidator::HyperSchema::ResponseGenerator.new.call(link)
|
|
@@ -24,6 +24,8 @@ module Committee
|
|
|
24
24
|
@allow_empty_date_and_datetime = options[:allow_empty_date_and_datetime]
|
|
25
25
|
@allow_form_params = options[:allow_form_params]
|
|
26
26
|
@allow_get_body = options[:allow_get_body]
|
|
27
|
+
@allow_raw_body = options[:allow_raw_body]
|
|
28
|
+
@allow_json_array = options[:allow_json_array]
|
|
27
29
|
@allow_query_params = options[:allow_query_params]
|
|
28
30
|
@allow_non_get_query_params = options[:allow_non_get_query_params]
|
|
29
31
|
@optimistic_json = options[:optimistic_json]
|
|
@@ -33,7 +35,8 @@ module Committee
|
|
|
33
35
|
def unpack_request_params(request)
|
|
34
36
|
# if Content-Type is empty or JSON, and there was a request body, try to
|
|
35
37
|
# interpret it as JSON
|
|
36
|
-
|
|
38
|
+
json_body = !request.media_type || request.media_type =~ %r{application/(?:.*\+)?json}
|
|
39
|
+
params = if json_body
|
|
37
40
|
parse_json(request)
|
|
38
41
|
elsif @optimistic_json
|
|
39
42
|
begin
|
|
@@ -45,16 +48,24 @@ module Committee
|
|
|
45
48
|
|
|
46
49
|
return [params, false] if params
|
|
47
50
|
|
|
48
|
-
|
|
51
|
+
form_body = %w[application/x-www-form-urlencoded multipart/form-data].include?(request.media_type)
|
|
52
|
+
if @allow_form_params && form_body
|
|
49
53
|
# Actually, POST means anything in the request body, could be from
|
|
50
54
|
# PUT or PATCH too. Silly Rack.
|
|
51
55
|
begin
|
|
52
56
|
return [request.POST, true] if request.POST
|
|
53
57
|
ensure
|
|
54
|
-
request.body.rewind
|
|
58
|
+
request.body.rewind if request.body.respond_to?(:rewind)
|
|
55
59
|
end
|
|
56
60
|
end
|
|
57
61
|
|
|
62
|
+
if @allow_raw_body && !json_body && !form_body && (!request.get? || @allow_get_body)
|
|
63
|
+
input = request.body
|
|
64
|
+
body = input ? input.read : ""
|
|
65
|
+
input.rewind if input.respond_to?(:rewind)
|
|
66
|
+
return [body, false]
|
|
67
|
+
end
|
|
68
|
+
|
|
58
69
|
[{}, false]
|
|
59
70
|
end
|
|
60
71
|
|
|
@@ -87,16 +98,15 @@ module Committee
|
|
|
87
98
|
def parse_json(request)
|
|
88
99
|
return nil if request.request_method == "GET" && !@allow_get_body
|
|
89
100
|
|
|
90
|
-
|
|
91
|
-
|
|
101
|
+
input = request.body
|
|
102
|
+
return nil if input.nil?
|
|
103
|
+
body = input.read
|
|
92
104
|
# if request body is empty, we just have empty params
|
|
93
105
|
return nil if body.length == 0
|
|
94
106
|
|
|
95
|
-
|
|
107
|
+
input.rewind if input.respond_to?(:rewind)
|
|
96
108
|
hash = JSON.parse(body)
|
|
97
|
-
|
|
98
|
-
# decoded properly, but we can't use them here.
|
|
99
|
-
if !hash.is_a?(Hash)
|
|
109
|
+
if !hash.is_a?(Hash) && !(@allow_json_array && hash.is_a?(Array))
|
|
100
110
|
raise BadRequest, "Invalid JSON input. Require object with parameters as keys."
|
|
101
111
|
end
|
|
102
112
|
self.class.indifferent_params(hash)
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Committee
|
|
4
|
+
class SchemaLocations
|
|
5
|
+
def initialize(path, data)
|
|
6
|
+
@path = path
|
|
7
|
+
@data = data
|
|
8
|
+
@lines = {}
|
|
9
|
+
@data_lines = {}
|
|
10
|
+
@external_locations = {}
|
|
11
|
+
document = YAML.parse_file(path)
|
|
12
|
+
anchors = {}
|
|
13
|
+
document.each do |node|
|
|
14
|
+
anchors[node.anchor] = node if node.respond_to?(:anchor) && node.anchor && !node.is_a?(Psych::Nodes::Alias)
|
|
15
|
+
end
|
|
16
|
+
record(document.root, data, '#', anchors)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def message(message, data, reference = nil)
|
|
20
|
+
line = @data_lines[data.object_id] || @lines[reference]
|
|
21
|
+
line ? "#{message} (#{@path}:#{line})" : message
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def open_api_3_message(message, response, reference)
|
|
25
|
+
schema = find_schema(response, reference, {})
|
|
26
|
+
return message unless schema
|
|
27
|
+
return self.message(message, schema.raw_schema, reference) if schema.root.raw_schema.equal?(@data)
|
|
28
|
+
|
|
29
|
+
# openapi_parser has no public reader for a document's source URI.
|
|
30
|
+
uri = schema.root.instance_variable_get(:@uri)
|
|
31
|
+
return message unless uri&.scheme == 'file' && ['.yml', '.yaml'].include?(File.extname(uri.path))
|
|
32
|
+
|
|
33
|
+
locations = @external_locations[schema.root] ||= self.class.new(uri.path, schema.root.raw_schema)
|
|
34
|
+
locations.message(message, schema.raw_schema, reference)
|
|
35
|
+
rescue SystemCallError, Psych::Exception
|
|
36
|
+
message
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def record(node, data, reference, anchors)
|
|
42
|
+
node = anchors[node.anchor] if node.is_a?(Psych::Nodes::Alias)
|
|
43
|
+
return unless node
|
|
44
|
+
|
|
45
|
+
@lines[reference] = node.start_line + 1
|
|
46
|
+
return unless data.is_a?(Hash) || data.is_a?(Array)
|
|
47
|
+
return if @data_lines.key?(data.object_id)
|
|
48
|
+
|
|
49
|
+
@data_lines[data.object_id] = node.start_line + 1
|
|
50
|
+
case node
|
|
51
|
+
when Psych::Nodes::Mapping
|
|
52
|
+
values = data.transform_keys(&:to_s)
|
|
53
|
+
mapping_nodes(node, anchors).each do |key, value|
|
|
54
|
+
escaped_key = key.gsub('~', '~0').gsub('/', '~1')
|
|
55
|
+
record(value, values[key], "#{reference}/#{escaped_key}", anchors)
|
|
56
|
+
end
|
|
57
|
+
when Psych::Nodes::Sequence
|
|
58
|
+
node.children.each_with_index { |child, index| record(child, data[index], "#{reference}/#{index}", anchors) }
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def mapping_nodes(node, anchors, visited = {})
|
|
63
|
+
node = anchors[node.anchor] if node.is_a?(Psych::Nodes::Alias)
|
|
64
|
+
return {} unless node && !visited[node.object_id]
|
|
65
|
+
|
|
66
|
+
visited = visited.merge(node.object_id => true)
|
|
67
|
+
if node.is_a?(Psych::Nodes::Sequence)
|
|
68
|
+
return node.children.reverse_each.each_with_object({}) { |child, result| result.merge!(mapping_nodes(child, anchors, visited)) }
|
|
69
|
+
end
|
|
70
|
+
return {} unless node.is_a?(Psych::Nodes::Mapping)
|
|
71
|
+
|
|
72
|
+
merged = {}
|
|
73
|
+
explicit = {}
|
|
74
|
+
node.children.each_slice(2) do |key, value|
|
|
75
|
+
if key.value == '<<'
|
|
76
|
+
merged.merge!(mapping_nodes(value, anchors, visited))
|
|
77
|
+
else
|
|
78
|
+
explicit[key.value] = value
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
merged.merge(explicit)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def find_schema(schema, reference, visited)
|
|
85
|
+
return unless schema && !visited[schema.object_id]
|
|
86
|
+
return schema if schema.object_reference == reference
|
|
87
|
+
|
|
88
|
+
visited[schema.object_id] = true
|
|
89
|
+
schema._openapi_all_child_objects.each_value do |child|
|
|
90
|
+
found = find_schema(child, reference, visited)
|
|
91
|
+
return found if found
|
|
92
|
+
end
|
|
93
|
+
nil
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -15,7 +15,7 @@ module Committee
|
|
|
15
15
|
@validators = {}
|
|
16
16
|
if link.is_a? Drivers::OpenAPI2::Link
|
|
17
17
|
link.target_schemas.each do |status, schema|
|
|
18
|
-
@validators[status] = JsonSchema::Validator.new(
|
|
18
|
+
@validators[status] = JsonSchema::Validator.new(schema)
|
|
19
19
|
end
|
|
20
20
|
else
|
|
21
21
|
@validator = JsonSchema::Validator.new(target_schema(link))
|
|
@@ -55,9 +55,15 @@ module Committee
|
|
|
55
55
|
begin
|
|
56
56
|
if validate?(status)
|
|
57
57
|
if @link.is_a?(Drivers::OpenAPI2::Link)
|
|
58
|
-
|
|
58
|
+
if @validators[status].nil?
|
|
59
|
+
message = "Invalid response.#{@link.href} status code #{status} definition does not exist"
|
|
60
|
+
message = @link.schema_locations.message(message, @link.response_definition) if @link.schema_locations
|
|
61
|
+
raise InvalidResponse, message
|
|
62
|
+
end
|
|
59
63
|
if !@validators[status].validate(data)
|
|
60
|
-
errors =
|
|
64
|
+
errors = @validators[status].errors.map do |error|
|
|
65
|
+
@link.schema_locations ? @link.schema_locations.message(error.to_s, error.schema.data) : error.to_s
|
|
66
|
+
end.join("\n")
|
|
61
67
|
raise InvalidResponse, "Invalid response.\n\n#{errors}"
|
|
62
68
|
end
|
|
63
69
|
else
|
|
@@ -100,7 +106,11 @@ module Committee
|
|
|
100
106
|
def check_content_type!(response)
|
|
101
107
|
if @link.media_type
|
|
102
108
|
unless Rack::Mime.match?(response_media_type(response), @link.media_type)
|
|
103
|
-
|
|
109
|
+
message = %{"Content-Type" response header must be set to "#{@link.media_type}".}
|
|
110
|
+
if @link.is_a?(Drivers::OpenAPI2::Link) && @link.schema_locations
|
|
111
|
+
message = @link.schema_locations.message(message, nil, '#/produces')
|
|
112
|
+
end
|
|
113
|
+
raise Committee::InvalidResponse, message
|
|
104
114
|
end
|
|
105
115
|
end
|
|
106
116
|
end
|
|
@@ -21,10 +21,11 @@ module Committee
|
|
|
21
21
|
def response_validate(status, headers, response, _test_method = false, custom_body_parser = nil)
|
|
22
22
|
return unless link_exist?
|
|
23
23
|
|
|
24
|
-
full_body =
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
full_body = if response.is_a?(String)
|
|
25
|
+
response
|
|
26
|
+
else
|
|
27
|
+
response.each_with_object(String.new(encoding: Encoding::BINARY)) { |chunk, body| body << chunk.b }.force_encoding(Encoding::UTF_8)
|
|
28
|
+
end
|
|
28
29
|
|
|
29
30
|
data = if custom_body_parser
|
|
30
31
|
custom_body_parser.call(full_body)
|
|
@@ -5,8 +5,9 @@ module Committee
|
|
|
5
5
|
class OpenAPI3
|
|
6
6
|
class OperationWrapper
|
|
7
7
|
# # @param request_operation [OpenAPIParser::RequestOperation]
|
|
8
|
-
def initialize(request_operation)
|
|
8
|
+
def initialize(request_operation, schema_locations = nil)
|
|
9
9
|
@request_operation = request_operation
|
|
10
|
+
@schema_locations = schema_locations
|
|
10
11
|
end
|
|
11
12
|
|
|
12
13
|
def path_params
|
|
@@ -17,13 +18,19 @@ module Committee
|
|
|
17
18
|
request_operation.original_path
|
|
18
19
|
end
|
|
19
20
|
|
|
21
|
+
# lowercase for standard methods; additionalOperations keep their case
|
|
20
22
|
def http_method
|
|
21
|
-
request_operation.http_method
|
|
23
|
+
method = request_operation.http_method
|
|
24
|
+
custom_operation? ? method : method.downcase
|
|
22
25
|
end
|
|
23
26
|
|
|
24
|
-
def coerce_path_parameter(validator_option)
|
|
27
|
+
def coerce_path_parameter(validator_option, path_params = nil)
|
|
25
28
|
options = build_openapi_parser_path_option(validator_option)
|
|
26
|
-
validated_path_params =
|
|
29
|
+
validated_path_params = if path_params
|
|
30
|
+
request_operation.operation_object.validate_path_params(path_params, options)
|
|
31
|
+
else
|
|
32
|
+
request_operation.validate_path_params(options)
|
|
33
|
+
end
|
|
27
34
|
return {} unless options.coerce_value
|
|
28
35
|
|
|
29
36
|
validated_path_params
|
|
@@ -32,25 +39,53 @@ module Committee
|
|
|
32
39
|
end
|
|
33
40
|
|
|
34
41
|
# @param [Boolean] strict when not content_type or status code definition, raise error
|
|
35
|
-
def validate_response_params(status_code, headers, response_data, strict, check_header, validator_options: {})
|
|
42
|
+
def validate_response_params(status_code, headers, response_data, strict, check_header, strict_response_content_type: false, validator_options: {})
|
|
36
43
|
response_body = OpenAPIParser::RequestOperation::ValidatableResponseBody.new(status_code, response_data, headers)
|
|
37
44
|
|
|
45
|
+
# When strict_response_content_type is enabled, reject responses whose Content-Type is not
|
|
46
|
+
# declared in the spec's content map for this status code. Responses with no content map
|
|
47
|
+
# (e.g. bare 204s) are skipped — openapi_parser's validate_response_body handles those.
|
|
48
|
+
if strict_response_content_type
|
|
49
|
+
response_object = find_response_object_for_status(request_operation.operation_object&.responses, status_code)
|
|
50
|
+
if response_object
|
|
51
|
+
content_type = Rack::MediaType.type(response_body.content_type)
|
|
52
|
+
matched = response_object.select_media_type(content_type)
|
|
53
|
+
if matched.nil? && response_object.content && !response_object.content.empty?
|
|
54
|
+
message = "Response Content-Type '#{content_type}' is not declared in the OpenAPI spec for this operation. Declared types: #{response_object.content.keys.join(', ')}"
|
|
55
|
+
message = @schema_locations.open_api_3_message(message, response_object, response_object.object_reference) if @schema_locations
|
|
56
|
+
raise Committee::InvalidResponse, message
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
38
61
|
return request_operation.validate_response_body(response_body, response_validate_options(strict, check_header, validator_options: validator_options))
|
|
39
62
|
rescue OpenAPIParser::OpenAPIError => e
|
|
40
|
-
|
|
63
|
+
responses = request_operation.operation_object&.responses
|
|
64
|
+
response = find_response_object_for_status(responses, status_code) || responses
|
|
65
|
+
# openapi_parser has no public reader for an error's schema reference.
|
|
66
|
+
message = @schema_locations ? @schema_locations.open_api_3_message(e.message, response, e.instance_variable_get(:@reference)) : e.message
|
|
67
|
+
raise Committee::InvalidResponse.new(message, original_error: e)
|
|
41
68
|
end
|
|
42
69
|
|
|
43
|
-
def validate_request_params(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
70
|
+
def validate_request_params(_path_params, query_params, body_params, headers, validator_option)
|
|
71
|
+
if body_method?
|
|
72
|
+
validate_post_request_params(query_params, body_params, headers, validator_option)
|
|
73
|
+
else
|
|
74
|
+
validate_get_request_params(query_params, headers, validator_option)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# query (3.2) carries a body like post. trace never does (RFC 9110 §9.3.8), so its
|
|
79
|
+
# requestBody is ignored. 3.2 additionalOperations do when they define a requestBody
|
|
80
|
+
def body_method?
|
|
81
|
+
case http_method
|
|
82
|
+
when 'get', 'delete', 'head', 'trace'
|
|
83
|
+
false
|
|
84
|
+
when 'post', 'put', 'patch', 'options', 'query'
|
|
85
|
+
true
|
|
86
|
+
else
|
|
87
|
+
!request_operation.operation_object&.request_body.nil?
|
|
88
|
+
end
|
|
54
89
|
end
|
|
55
90
|
|
|
56
91
|
def optional_body?
|
|
@@ -84,6 +119,10 @@ module Committee
|
|
|
84
119
|
|
|
85
120
|
private
|
|
86
121
|
|
|
122
|
+
def custom_operation?
|
|
123
|
+
request_operation.path_item&.additional_operations&.value?(request_operation.operation_object) || false
|
|
124
|
+
end
|
|
125
|
+
|
|
87
126
|
# @return [OpenAPIParser::SchemaValidator::Options]
|
|
88
127
|
def build_openapi_parser_body_option(validator_option)
|
|
89
128
|
build_openapi_parser_option(validator_option, validator_option.coerce_form_params)
|
|
@@ -112,42 +151,27 @@ module Committee
|
|
|
112
151
|
OpenAPIParser::SchemaValidator::Options.new(**parser_options)
|
|
113
152
|
end
|
|
114
153
|
|
|
115
|
-
def validate_get_request_params(
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
rescue OpenAPIParser::OpenAPIError => e
|
|
154
|
+
def validate_get_request_params(query_params, headers, validator_option)
|
|
155
|
+
validate_query_params(query_params, headers, validator_option)
|
|
156
|
+
rescue OpenAPIParser::OpenAPIError, ArgumentError => e
|
|
119
157
|
raise Committee::InvalidRequest.new(e.message, original_error: e)
|
|
120
158
|
end
|
|
121
159
|
|
|
122
|
-
def validate_post_request_params(
|
|
160
|
+
def validate_post_request_params(query_params, body_params, headers, validator_option)
|
|
123
161
|
content_type_key = headers.keys.detect { |k| k.casecmp?('Content-Type') }
|
|
124
162
|
content_type = Rack::MediaType.type(headers[content_type_key])
|
|
125
163
|
|
|
126
|
-
|
|
127
|
-
validate_path_and_query_params(path_params, query_params, headers, validator_option)
|
|
164
|
+
validate_query_params(query_params, headers, validator_option)
|
|
128
165
|
request_operation.validate_request_body(content_type, body_params, build_openapi_parser_body_option(validator_option))
|
|
129
166
|
rescue => e
|
|
130
167
|
raise Committee::InvalidRequest.new(e.message, original_error: e)
|
|
131
168
|
end
|
|
132
169
|
|
|
133
|
-
def
|
|
134
|
-
path_params ||= {}
|
|
170
|
+
def validate_query_params(query_params, headers, validator_option)
|
|
135
171
|
query_params ||= {}
|
|
136
172
|
|
|
137
|
-
#
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
path_keys = path_params.keys.to_set
|
|
141
|
-
query_keys = query_params.keys.to_set
|
|
142
|
-
|
|
143
|
-
merged_params = query_params.merge(path_params)
|
|
144
|
-
|
|
145
|
-
request_operation.validate_request_parameter(merged_params, headers, build_openapi_parser_request_parameter_option(validator_option))
|
|
146
|
-
|
|
147
|
-
merged_params.each do |k, v|
|
|
148
|
-
path_params[k] = v if path_keys.include?(k)
|
|
149
|
-
query_params[k] = v if query_keys.include?(k)
|
|
150
|
-
end
|
|
173
|
+
# Path parameters have already been validated by coerce_path_parameter.
|
|
174
|
+
request_operation.validate_request_parameter(query_params, headers, build_openapi_parser_request_parameter_option(validator_option))
|
|
151
175
|
|
|
152
176
|
validate_no_unknown_query_params(query_params) if validator_option.strict_query_params
|
|
153
177
|
end
|
|
@@ -163,6 +187,18 @@ module Committee
|
|
|
163
187
|
raise Committee::InvalidRequest.new("Unknown query parameter(s): #{unknown_params.join(', ')}")
|
|
164
188
|
end
|
|
165
189
|
|
|
190
|
+
def find_response_object_for_status(responses, status_code)
|
|
191
|
+
return nil unless responses&.response
|
|
192
|
+
|
|
193
|
+
response_hash = responses.response
|
|
194
|
+
return response_hash[status_code.to_s] if response_hash[status_code.to_s]
|
|
195
|
+
|
|
196
|
+
wild_card = "#{status_code.to_i / 100}XX"
|
|
197
|
+
return response_hash[wild_card] if response_hash[wild_card]
|
|
198
|
+
|
|
199
|
+
responses.default
|
|
200
|
+
end
|
|
201
|
+
|
|
166
202
|
def response_validate_options(strict, check_header, validator_options: {})
|
|
167
203
|
options = { strict: strict, validate_header: check_header }
|
|
168
204
|
|