openapi_parser 0.1.8 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/lib/openapi_parser/config.rb +10 -0
- data/lib/openapi_parser/errors.rb +12 -0
- data/lib/openapi_parser/request_operation.rb +9 -2
- data/lib/openapi_parser/schema_validators/options.rb +15 -0
- data/lib/openapi_parser/schemas/operation.rb +5 -2
- data/lib/openapi_parser/schemas/response.rb +9 -2
- data/lib/openapi_parser/schemas/responses.rb +22 -7
- data/lib/openapi_parser/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd2a5c6292d86e914f641bf9d2e99b6ef8de9cbc46e5b3196937fc1c691541df
|
4
|
+
data.tar.gz: 9a6c9a5400e1fffdf12cc0e43fbc89433b7e4b917bd61dfb1c34c613bc17e9f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0093301164855dba5aff9a87cdce0c8d99f85ac5c1c4738c011c850924704313b1fd6f9aee00f01f25e7a125b1d18c3d02659e8594eb6fa1bbd0da84e1bf3734'
|
7
|
+
data.tar.gz: 23b2105486befe12f39f90a9ea8f893b46a1dd36693782499b497ce1909ddef4a6b2d25da313cc62e3de8bf9a28246a5f140640fd9ff7869526ebfa05ea1a828
|
data/CHANGELOG.md
CHANGED
@@ -15,10 +15,20 @@ class OpenAPIParser::Config
|
|
15
15
|
@config.fetch(:expand_reference, true)
|
16
16
|
end
|
17
17
|
|
18
|
+
def strict_response_validation
|
19
|
+
@config.fetch(:strict_response_validation, false)
|
20
|
+
end
|
21
|
+
|
22
|
+
# @return [OpenAPIParser::SchemaValidator::Options]
|
18
23
|
def request_validator_options
|
19
24
|
@request_validator_options ||= OpenAPIParser::SchemaValidator::Options.new(coerce_value: coerce_value, datetime_coerce_class: datetime_coerce_class)
|
20
25
|
end
|
21
26
|
|
22
27
|
alias_method :request_body_options, :request_validator_options
|
23
28
|
alias_method :path_params_options, :request_validator_options
|
29
|
+
|
30
|
+
# @return [OpenAPIParser::SchemaValidator::ResponseValidateOptions]
|
31
|
+
def response_validate_options
|
32
|
+
@response_validate_options ||= OpenAPIParser::SchemaValidator::ResponseValidateOptions.new(strict: strict_response_validation)
|
33
|
+
end
|
24
34
|
end
|
@@ -64,4 +64,16 @@ module OpenAPIParser
|
|
64
64
|
"#{@value} isn't include enum in #{@reference}"
|
65
65
|
end
|
66
66
|
end
|
67
|
+
|
68
|
+
class NotExistStatusCodeDefinition < OpenAPIError
|
69
|
+
def message
|
70
|
+
"don't exist status code definition in #{@reference}"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class NotExistContentTypeDefinition < OpenAPIError
|
75
|
+
def message
|
76
|
+
"don't exist response definition #{@reference}"
|
77
|
+
end
|
78
|
+
end
|
67
79
|
end
|
@@ -40,14 +40,21 @@ class OpenAPIParser::RequestOperation
|
|
40
40
|
operation_object&.validate_path_params(path_params, options)
|
41
41
|
end
|
42
42
|
|
43
|
+
# @param [String] content_type
|
44
|
+
# @param [Hash] params
|
45
|
+
# @param [OpenAPIParser::SchemaValidator::Options] options
|
43
46
|
def validate_request_body(content_type, params, options = nil)
|
44
47
|
options ||= config.request_body_options
|
45
48
|
operation_object&.validate_request_body(content_type, params, options)
|
46
49
|
end
|
47
50
|
|
51
|
+
# @param [String] content_type
|
48
52
|
# @param [Integer] status_code
|
49
|
-
|
50
|
-
|
53
|
+
# @param [Hash] data
|
54
|
+
# @param [OpenAPIParser::SchemaValidator::ResponseValidateOptions] response_validate_options
|
55
|
+
def validate_response_body(status_code, content_type, data, response_validate_options = nil)
|
56
|
+
response_validate_options ||= config.response_validate_options
|
57
|
+
operation_object&.validate_response_body(status_code, content_type, data, response_validate_options)
|
51
58
|
end
|
52
59
|
|
53
60
|
def validate_request_parameter(params, options = nil)
|
@@ -1,5 +1,9 @@
|
|
1
1
|
class OpenAPIParser::SchemaValidator
|
2
2
|
class Options
|
3
|
+
# @!attribute [r] coerce_value
|
4
|
+
# @return [Boolean] coerce value option on/off
|
5
|
+
# @!attribute [r] datetime_coerce_class
|
6
|
+
# @return [Object, nil] coerce datetime string by this Object class
|
3
7
|
attr_reader :coerce_value, :datetime_coerce_class
|
4
8
|
|
5
9
|
def initialize(coerce_value: nil, datetime_coerce_class: nil)
|
@@ -7,4 +11,15 @@ class OpenAPIParser::SchemaValidator
|
|
7
11
|
@datetime_coerce_class = datetime_coerce_class
|
8
12
|
end
|
9
13
|
end
|
14
|
+
|
15
|
+
# response body validation option
|
16
|
+
class ResponseValidateOptions
|
17
|
+
# @!attribute [r] strict
|
18
|
+
# @return [Boolean] validate by strict (when not exist definition, raise error)
|
19
|
+
attr_reader :strict
|
20
|
+
|
21
|
+
def initialize(strict: false)
|
22
|
+
@strict = strict
|
23
|
+
end
|
24
|
+
end
|
10
25
|
end
|
@@ -23,9 +23,12 @@ module OpenAPIParser::Schemas
|
|
23
23
|
request_body&.validate_request_body(content_type, params, options)
|
24
24
|
end
|
25
25
|
|
26
|
+
# @param [String] content_type
|
26
27
|
# @param [Integer] status_code
|
27
|
-
|
28
|
-
|
28
|
+
# @param [Hash] data
|
29
|
+
# @param [OpenAPIParser::SchemaValidator::ResponseValidateOptions] response_validate_options
|
30
|
+
def validate_response_body(status_code, content_type, data, response_validate_options)
|
31
|
+
responses&.validate_response_body(status_code, content_type, data, response_validate_options)
|
29
32
|
end
|
30
33
|
|
31
34
|
# @param [OpenAPIParser::SchemaValidator::Options] options
|
@@ -11,9 +11,16 @@ module OpenAPIParser::Schemas
|
|
11
11
|
# @return [Hash{String => MediaType}, nil] content_type to MediaType hash
|
12
12
|
openapi_attr_hash_object :content, MediaType, reference: false
|
13
13
|
|
14
|
-
|
14
|
+
# @param [String] content_type
|
15
|
+
# @param [Hash] params
|
16
|
+
# @param [OpenAPIParser::SchemaValidator::ResponseValidateOptions] response_validate_options
|
17
|
+
def validate_parameter(content_type, params, response_validate_options)
|
15
18
|
media_type = select_media_type(content_type)
|
16
|
-
|
19
|
+
unless media_type
|
20
|
+
raise ::OpenAPIParser::NotExistContentTypeDefinition, object_reference if response_validate_options.strict
|
21
|
+
|
22
|
+
return nil
|
23
|
+
end
|
17
24
|
|
18
25
|
options = ::OpenAPIParser::SchemaValidator::Options.new # response validator not support any options
|
19
26
|
media_type.validate_parameter(params, options)
|
@@ -16,21 +16,36 @@ module OpenAPIParser::Schemas
|
|
16
16
|
# @param [Integer] status_code
|
17
17
|
# @param [String] content_type
|
18
18
|
# @param [Hash] params
|
19
|
-
|
19
|
+
# @param [OpenAPIParser::SchemaValidator::ResponseValidateOptions] response_validate_options
|
20
|
+
def validate_response_body(status_code, content_type, params, response_validate_options)
|
20
21
|
return nil unless response
|
21
22
|
|
22
|
-
res =
|
23
|
-
|
23
|
+
if (res = find_response_object(status_code))
|
24
|
+
return res.validate_parameter(content_type, params, response_validate_options)
|
25
|
+
end
|
24
26
|
|
25
|
-
|
26
|
-
res = response[wild_card]
|
27
|
-
return res.validate_parameter(content_type, params) if res
|
27
|
+
raise ::OpenAPIParser::NotExistStatusCodeDefinition, object_reference if response_validate_options.strict
|
28
28
|
|
29
|
-
|
29
|
+
nil
|
30
30
|
end
|
31
31
|
|
32
32
|
private
|
33
33
|
|
34
|
+
# @param [Integer] status_code
|
35
|
+
# @return [Response]
|
36
|
+
def find_response_object(status_code)
|
37
|
+
if (res = response[status_code.to_s])
|
38
|
+
return res
|
39
|
+
end
|
40
|
+
|
41
|
+
wild_card = status_code_to_wild_card(status_code)
|
42
|
+
if (res = response[wild_card])
|
43
|
+
return res
|
44
|
+
end
|
45
|
+
|
46
|
+
default
|
47
|
+
end
|
48
|
+
|
34
49
|
# parse 400 -> 4xx
|
35
50
|
# OpenAPI3 allow 1xx, 2xx, 3xx... only, don't allow 41x
|
36
51
|
# @param [Integer] status_code
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openapi_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ota42y
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|