openapi_parser 0.1.8 → 0.1.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 01ffcf763ea2797df03cd91cd162d4031c504d5f7ece8327e4e0cbad989972c4
4
- data.tar.gz: 518c4c86cababa47d3a6f0520fbeeec28d953d40ea9e7e3e4ab26150eaf917b5
3
+ metadata.gz: dd2a5c6292d86e914f641bf9d2e99b6ef8de9cbc46e5b3196937fc1c691541df
4
+ data.tar.gz: 9a6c9a5400e1fffdf12cc0e43fbc89433b7e4b917bd61dfb1c34c613bc17e9f4
5
5
  SHA512:
6
- metadata.gz: a59505448fa9fdbe16e6117c86eb885d3343ece1f6e3ed18dffaa44727f6d99e71f81dc48661d311c8e1524d09c21f9c78e8a9a13d921b7478c3bdae0110891b
7
- data.tar.gz: ecdfd1fc6b2b159e8991b9d187decc834958f2970bcbef710739b36b09a44409dce0ac425129459f7d5f86236b7f72227bd06d108790ee6e2cfcff5a7a434736
6
+ metadata.gz: '0093301164855dba5aff9a87cdce0c8d99f85ac5c1c4738c011c850924704313b1fd6f9aee00f01f25e7a125b1d18c3d02659e8594eb6fa1bbd0da84e1bf3734'
7
+ data.tar.gz: 23b2105486befe12f39f90a9ea8f893b46a1dd36693782499b497ce1909ddef4a6b2d25da313cc62e3de8bf9a28246a5f140640fd9ff7869526ebfa05ea1a828
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  ## Unreleased
2
2
 
3
+ ## 0.1.9 (2019-01-01)
4
+ * add strict option (#17)
5
+
3
6
  ## 0.1.8 (2018-12-31)
4
7
  * add select_media_type method(#16)
5
8
 
@@ -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
- def validate_response_body(status_code, content_type, data)
50
- operation_object&.validate_response_body(status_code, content_type, data)
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
- def validate_response_body(status_code, content_type, data)
28
- responses&.validate_response_body(status_code, content_type, data)
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
- def validate_parameter(content_type, params)
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
- return nil unless media_type
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
- def validate_response_body(status_code, content_type, params)
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 = response[status_code.to_s]
23
- return res.validate_parameter(content_type, params) if res
23
+ if (res = find_response_object(status_code))
24
+ return res.validate_parameter(content_type, params, response_validate_options)
25
+ end
24
26
 
25
- wild_card = status_code_to_wild_card(status_code)
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
- default&.validate_parameter(content_type, params)
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
@@ -1,3 +1,3 @@
1
1
  module OpenAPIParser
2
- VERSION = '0.1.8'.freeze
2
+ VERSION = '0.1.9'.freeze
3
3
  end
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.8
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: 2018-12-31 00:00:00.000000000 Z
11
+ date: 2019-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler