openapi_first 4.3.0 → 4.5.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.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +7 -0
  3. data/CHANGELOG.md +12 -0
  4. data/README.md +92 -10
  5. data/lib/openapi_first/builder.rb +25 -24
  6. data/lib/openapi_first/child_configuration.rb +7 -2
  7. data/lib/openapi_first/configuration.rb +80 -2
  8. data/lib/openapi_first/definition.rb +13 -3
  9. data/lib/openapi_first/error_response.rb +18 -3
  10. data/lib/openapi_first/failure.rb +27 -7
  11. data/lib/openapi_first/file_loader.rb +1 -1
  12. data/lib/openapi_first/json.rb +1 -1
  13. data/lib/openapi_first/middlewares/request_validation.rb +9 -8
  14. data/lib/openapi_first/middlewares/response_validation.rb +2 -0
  15. data/lib/openapi_first/parameter/converter/array_converter.rb +1 -1
  16. data/lib/openapi_first/parameter/converter/object_converter.rb +1 -1
  17. data/lib/openapi_first/parameter/converter.rb +1 -1
  18. data/lib/openapi_first/parameter/unpackers.rb +1 -1
  19. data/lib/openapi_first/parameter.rb +3 -0
  20. data/lib/openapi_first/parameters_parser.rb +1 -1
  21. data/lib/openapi_first/parsed_request.rb +1 -0
  22. data/lib/openapi_first/path_template.rb +1 -1
  23. data/lib/openapi_first/plugins/x_public.rb +3 -0
  24. data/lib/openapi_first/query_string_parser.rb +1 -1
  25. data/lib/openapi_first/ref_resolver.rb +16 -11
  26. data/lib/openapi_first/registry.rb +2 -4
  27. data/lib/openapi_first/request_body_parsers.rb +1 -1
  28. data/lib/openapi_first/{request.rb → request_definition.rb} +27 -1
  29. data/lib/openapi_first/request_headers.rb +1 -1
  30. data/lib/openapi_first/request_validator.rb +1 -0
  31. data/lib/openapi_first/response_body_parsers.rb +1 -1
  32. data/lib/openapi_first/{response.rb → response_definition.rb} +19 -4
  33. data/lib/openapi_first/response_parser.rb +3 -1
  34. data/lib/openapi_first/response_validator.rb +1 -0
  35. data/lib/openapi_first/router/find_content.rb +1 -1
  36. data/lib/openapi_first/router/find_response.rb +1 -1
  37. data/lib/openapi_first/router/path_trie.rb +11 -2
  38. data/lib/openapi_first/router/segment_pattern.rb +1 -1
  39. data/lib/openapi_first/router.rb +1 -0
  40. data/lib/openapi_first/schema/hash.rb +15 -1
  41. data/lib/openapi_first/schema/validation_result.rb +1 -0
  42. data/lib/openapi_first/sinatra.rb +2 -2
  43. data/lib/openapi_first/test/app.rb +3 -0
  44. data/lib/openapi_first/test/callable.rb +1 -0
  45. data/lib/openapi_first/test/configuration.rb +6 -4
  46. data/lib/openapi_first/test/coverage/covered_request.rb +1 -0
  47. data/lib/openapi_first/test/coverage/covered_response.rb +1 -0
  48. data/lib/openapi_first/test/coverage/html_reporter/context.rb +1 -0
  49. data/lib/openapi_first/test/coverage/html_reporter.rb +2 -0
  50. data/lib/openapi_first/test/coverage/plan.rb +1 -0
  51. data/lib/openapi_first/test/coverage/request_task.rb +1 -1
  52. data/lib/openapi_first/test/coverage/response_task.rb +1 -1
  53. data/lib/openapi_first/test/coverage/route_task.rb +1 -0
  54. data/lib/openapi_first/test/coverage/skipped_summary.rb +1 -0
  55. data/lib/openapi_first/test/coverage/terminal_reporter.rb +2 -0
  56. data/lib/openapi_first/test/coverage/tracker.rb +1 -0
  57. data/lib/openapi_first/test/coverage.rb +11 -4
  58. data/lib/openapi_first/test/methods.rb +14 -1
  59. data/lib/openapi_first/test/minitest_helpers.rb +5 -2
  60. data/lib/openapi_first/test/observe.rb +2 -1
  61. data/lib/openapi_first/test/observer_middleware.rb +1 -0
  62. data/lib/openapi_first/test/plain_helpers.rb +7 -4
  63. data/lib/openapi_first/test.rb +30 -3
  64. data/lib/openapi_first/utils.rb +1 -1
  65. data/lib/openapi_first/validated_request.rb +4 -4
  66. data/lib/openapi_first/validated_response.rb +5 -3
  67. data/lib/openapi_first/validators/multipart_request_body.rb +1 -0
  68. data/lib/openapi_first/validators/request_body.rb +1 -0
  69. data/lib/openapi_first/validators/request_parameters.rb +1 -1
  70. data/lib/openapi_first/validators/response_body.rb +1 -0
  71. data/lib/openapi_first/validators/response_headers.rb +1 -0
  72. data/lib/openapi_first/version.rb +1 -1
  73. data/lib/openapi_first.rb +16 -6
  74. metadata +4 -3
@@ -31,6 +31,8 @@ module OpenapiFirst
31
31
  # @attr_reader [Proc] app The upstream Rack application
32
32
  attr_reader :app
33
33
 
34
+ # @param env [Hash] The Rack env
35
+ # @return [Array] A Rack response
34
36
  def call(env)
35
37
  status, headers, body = @app.call(env)
36
38
  @definition.validate_response(Rack::Request.new(env), Rack::Response[status, headers, body],
@@ -4,7 +4,7 @@ module OpenapiFirst
4
4
  class Parameter
5
5
  module Converter
6
6
  # Converts the items of an array parameter value
7
- # @visibility private
7
+ # @api private
8
8
  ArrayConverter = Data.define(:schema) do
9
9
  def call(value)
10
10
  return [] if value.nil? || value.empty?
@@ -4,7 +4,7 @@ module OpenapiFirst
4
4
  class Parameter
5
5
  module Converter
6
6
  # Converts the properties of an object parameter value
7
- # @visibility private
7
+ # @api private
8
8
  ObjectConverter = Data.define(:schema) do
9
9
  def self.get_properties(schema) # rubocop:disable Metrics
10
10
  return nil if schema.nil? || schema.empty?
@@ -6,7 +6,7 @@ require_relative 'converter/object_converter'
6
6
  module OpenapiFirst
7
7
  class Parameter
8
8
  # Converts a parameter value (string) to the type specified in the JSON Schema.
9
- # @visibility private
9
+ # @api private
10
10
  module Converter
11
11
  PASS_THROUGH = ->(value) { value }
12
12
 
@@ -9,7 +9,7 @@ module OpenapiFirst
9
9
  # Each unpacker is a callable that takes a raw string (or already-parsed
10
10
  # value) and returns the unpacked Ruby value, throwing :skip on unrecoverable
11
11
  # parse errors.
12
- # @visibility private
12
+ # @api private
13
13
  module Unpackers
14
14
  ARRAY_DELIMITER = {
15
15
  'label' => '.',
@@ -32,12 +32,14 @@ module OpenapiFirst
32
32
 
33
33
  attr_reader :name, :schema, :location, :media_type, :style
34
34
 
35
+ # @api private
35
36
  def unpack(value)
36
37
  return value if value.nil?
37
38
 
38
39
  @unpacker.call(value)
39
40
  end
40
41
 
42
+ # @api private
41
43
  def convert(value) = @converter.call(value)
42
44
 
43
45
  # Unpacks and converts a raw value.
@@ -57,6 +59,7 @@ module OpenapiFirst
57
59
 
58
60
  def deep_object? = @deep_object
59
61
 
62
+ # @return [String, nil] The JSON Schema type of the parameter
60
63
  def type = schema && schema['type']
61
64
 
62
65
  def array? = type == 'array'
@@ -2,7 +2,7 @@
2
2
 
3
3
  module OpenapiFirst
4
4
  # Unpacks parameters from a Hash of raw values, like path parameters, headers or cookies.
5
- # @visibility private
5
+ # @api private
6
6
  class ParametersParser
7
7
  # @param parameters [Array<Parameter>]
8
8
  # @param check_encoding [Boolean] Raise Rack::Utils::InvalidParameterError if a value has an invalid encoding.
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiFirst
4
+ # @api private
4
5
  ParsedRequest = Data.define(:path, :query, :headers, :body, :cookies)
5
6
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module OpenapiFirst
4
4
  # Path templating as described in https://spec.openapis.org/oas/v3.1.0#path-templating
5
- # @visibility private
5
+ # @api private
6
6
  module PathTemplate
7
7
  VARIABLE = /\{(?<name>[^{}]+)\}/
8
8
  SEGMENT_VARIABLE = /\A#{VARIABLE}\z/
@@ -16,6 +16,9 @@ module OpenapiFirst
16
16
  # OpenapiFirst.plugin :x_public, field: 'x-visible'
17
17
  # OpenapiFirst.plugin :x_public, if: ->(req) { req.host == 'api.example.com' }
18
18
  module XPublic
19
+ # @param config [Configuration]
20
+ # @param field [String] The OpenAPI extension field to check
21
+ # @return [void]
19
22
  def self.configure(config, field: 'x-public', **opts)
20
23
  condition = opts[:if]
21
24
  config.before_request_validation do |request, request_definition|
@@ -6,7 +6,7 @@ require_relative 'parameters_parser'
6
6
 
7
7
  module OpenapiFirst
8
8
  # Unpacks query parameters from a query string.
9
- # @visibility private
9
+ # @api private
10
10
  class QueryStringParser
11
11
  DEEP_PROP = '\[([\w-]+)\]$'
12
12
  private_constant :DEEP_PROP
@@ -4,7 +4,7 @@ require 'json_schemer'
4
4
 
5
5
  module OpenapiFirst
6
6
  # This is here to give traverse an OAD while keeping $refs intact
7
- # @visibility private
7
+ # @api private
8
8
  class RefResolver
9
9
  def initialize(file_loader:)
10
10
  @file_loader = file_loader
@@ -41,7 +41,7 @@ module OpenapiFirst
41
41
  self.for(value, filepath: filepath, context: file_contents)
42
42
  end
43
43
 
44
- # @visibility private
44
+ # @api private
45
45
  module Diggable
46
46
  def dig(*keys)
47
47
  keys.inject(self) do |result, key|
@@ -52,7 +52,7 @@ module OpenapiFirst
52
52
  end
53
53
  end
54
54
 
55
- # @visibility private
55
+ # @api private
56
56
  module Resolvable
57
57
  def initialize(value, ref_resolver:, context: value, filepath: nil)
58
58
  @value = value
@@ -84,7 +84,7 @@ module OpenapiFirst
84
84
  def resolve_ref(pointer)
85
85
  if pointer.start_with?('#')
86
86
  value = Hana::Pointer.new(pointer[1..]).eval(context)
87
- raise "Unknown reference #{pointer.inspect} in #{filepath || context}" unless value
87
+ raise "Unknown reference #{pointer.inspect} in #{ref_location}. Nothing exists at that pointer." unless value
88
88
 
89
89
  return ref_resolver.for(value, filepath:, context:)
90
90
  end
@@ -94,7 +94,10 @@ module OpenapiFirst
94
94
  return ref_resolver.load(full_path) unless file_pointer
95
95
 
96
96
  resolved = ref_resolver.file_at(full_path, file_pointer)
97
- raise "Unknown reference #{pointer.inspect} in #{filepath || context}" unless resolved
97
+ unless resolved
98
+ raise "Unknown reference #{pointer.inspect} in #{ref_location}. " \
99
+ "Nothing exists at #{"##{file_pointer}".inspect} in #{full_path.inspect}."
100
+ end
98
101
 
99
102
  resolved
100
103
  rescue OpenapiFirst::FileNotFoundError => e
@@ -105,10 +108,12 @@ module OpenapiFirst
105
108
 
106
109
  private
107
110
 
111
+ def ref_location = filepath ? File.absolute_path(filepath).inspect : 'the API description'
112
+
108
113
  def mark(visited) = (visited || []) + [value.object_id]
109
114
  end
110
115
 
111
- # @visibility private
116
+ # @api private
112
117
  class Simple
113
118
  include Resolvable
114
119
 
@@ -117,7 +122,7 @@ module OpenapiFirst
117
122
  def dereferenced(_visited = nil) = value
118
123
  end
119
124
 
120
- # @visibility private
125
+ # @api private
121
126
  class Hash
122
127
  include Resolvable
123
128
  include Diggable
@@ -171,7 +176,7 @@ module OpenapiFirst
171
176
  end
172
177
  end
173
178
 
174
- # @visibility private
179
+ # @api private
175
180
  # Defers initialization JSONSchemer::Schema, because that takes time.
176
181
  class Schema
177
182
  extend Forwardable
@@ -187,7 +192,7 @@ module OpenapiFirst
187
192
  # This class is here to monkey-patch JSONSchemer::Schema when we are passing
188
193
  # an instance to sub-schemas. It skips stringifying all keys of the passed object to
189
194
  # allocate fewer objects. We don't have to stringify the keys, because they are already stringified upstream.
190
- # @visibility private
195
+ # @api private
191
196
  class DocumentRootSchema < JSONSchemer::Schema
192
197
  private
193
198
 
@@ -204,7 +209,7 @@ module OpenapiFirst
204
209
 
205
210
  attr_reader :value, :context, :base_uri, :options
206
211
 
207
- def_delegators :schema, :validate, :valid?
212
+ def_delegators :schema, :validate, :valid?, :parsed
208
213
 
209
214
  def schema
210
215
  @schema ||= begin
@@ -228,7 +233,7 @@ module OpenapiFirst
228
233
  end
229
234
  end
230
235
 
231
- # @visibility private
236
+ # @api private
232
237
  class Array
233
238
  include Enumerable
234
239
  include Resolvable
@@ -4,15 +4,13 @@ module OpenapiFirst
4
4
  class NotRegisteredError < Error; end
5
5
  class AlreadyRegisteredError < Error; end
6
6
 
7
- # @visibility private
7
+ # @api private
8
8
  module Registry
9
+ # @return [Hash<Symbol, Definition>] The registered API descriptions by name
9
10
  def definitions
10
11
  @definitions ||= {}
11
12
  end
12
13
 
13
- # Register an OpenAPI definition for testing
14
- # @param path_or_definition [String, Definition] Path to the OpenAPI file or a Definition object
15
- # @param as [Symbol] Name to register the API definition as
16
14
  def register(path_or_definition, as: :default, &)
17
15
  if definitions.key?(as) && as == :default
18
16
  raise(
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiFirst
4
- # @!visibility private
4
+ # @api private
5
5
  module RequestBodyParsers
6
6
  DEFAULT = ->(request) { Utils.read_body(request) }
7
7
 
@@ -11,7 +11,8 @@ module OpenapiFirst
11
11
  # Note that this is not the same as an OpenAPI 3.x Operation.
12
12
  # An 3.x Operation object can accept multiple requests, because it can handle multiple content-types.
13
13
  # This class represents one of those requests.
14
- class Request
14
+ class RequestDefinition
15
+ # @api private
15
16
  def initialize(path:, request_method:, operation_object:, # rubocop:disable Metrics/MethodLength,Metrics/ParameterLists
16
17
  parameters:, content_type:, content_schema:, required_body:, key:, encoding: nil)
17
18
  @path = path
@@ -38,19 +39,40 @@ module OpenapiFirst
38
39
  )
39
40
  end
40
41
 
42
+ # @!attribute [r] content_type
43
+ # @return [String, nil] The request content type, or nil if the operation defines no request body
44
+ # @!attribute [r] content_schema
45
+ # @return [#value, nil] The schema of the request body. Its `value` is the JSON Schema Hash.
46
+ # @!attribute [r] operation
47
+ # @return [Hash] The raw OpenAPI 3 operation object
48
+ # @!attribute [r] request_method
49
+ # @return [String] The lowercase HTTP method, e.g. "get"
50
+ # @!attribute [r] path
51
+ # @return [String] The OpenAPI path template, e.g. "/pets/{petId}"
52
+ # @!attribute [r] key
53
+ # @return [String] A unique key of this request definition
54
+ # @!attribute [r] parameters
55
+ # @return [Array<Parameter>] The parameters in the order path, query, header, cookie
41
56
  attr_reader :content_type, :content_schema, :operation, :request_method, :path, :key, :parameters
42
57
  private attr_reader :query_parser
43
58
 
59
+ # @return [Boolean] true if a request without a body is valid
44
60
  def allow_empty_content?
45
61
  @allow_empty_content
46
62
  end
47
63
 
64
+ # @api private
65
+ # Use {Definition#validate_request} to validate a request.
66
+ # @param request [Rack::Request]
67
+ # @param route_params [Hash<String, String>] The path parameters extracted by the router
68
+ # @return [ValidatedRequest]
48
69
  def validate(request, route_params:)
49
70
  parsed_request, error = parse_request(request, route_params:)
50
71
  error ||= @validator.call(parsed_request) if parsed_request
51
72
  ValidatedRequest.new(request, parsed_request:, error:, request_definition: self, query_parser:)
52
73
  end
53
74
 
75
+ # @return [String, nil] The OpenAPI 3 operationId
54
76
  def operation_id
55
77
  @operation['operationId']
56
78
  end
@@ -92,4 +114,8 @@ module OpenapiFirst
92
114
  RequestBodyParsers[content_type, { encoding: encoding || {} }]
93
115
  end
94
116
  end
117
+
118
+ # @deprecated Use {RequestDefinition}
119
+ Request = RequestDefinition
120
+ deprecate_constant :Request
95
121
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module OpenapiFirst
4
4
  # A wrapper around the Rack env hash that allows accessing headers by header name
5
- # @visibility private
5
+ # @api private
6
6
  class RequestHeaders
7
7
  # This was copied from this Rack::Request PR: https://github.com/rack/rack/pull/1881
8
8
  def initialize(env)
@@ -6,6 +6,7 @@ require_relative 'validators/request_body'
6
6
 
7
7
  module OpenapiFirst
8
8
  # Validates a Request against a request definition.
9
+ # @api private
9
10
  class RequestValidator
10
11
  def initialize(
11
12
  content_schema:,
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiFirst
4
- # @visibility private
4
+ # @api private
5
5
  module ResponseBodyParsers
6
6
  DEFAULT = ->(body) { body }
7
7
 
@@ -8,7 +8,8 @@ module OpenapiFirst
8
8
  # Represents a response definition in the OpenAPI document.
9
9
  # This is not a direct reflecton of the OpenAPI 3.X response definition, but a combination of
10
10
  # status, content type and content schema.
11
- class Response
11
+ class ResponseDefinition
12
+ # @api private
12
13
  def initialize(status:, headers:, content_type:, content_schema:, key:)
13
14
  @status = status
14
15
  @content_type = content_type
@@ -19,15 +20,29 @@ module OpenapiFirst
19
20
  @validator = ResponseValidator.new(content_schema:, headers:)
20
21
  end
21
22
 
22
- # @attr_reader [Integer] status The HTTP status code of the response definition.
23
- # @attr_reader [String, nil] content_type Content type of this response.
24
- # @attr_reader [Schema, nil] content_schema the Schema of the response body.
23
+ # @!attribute [r] status
24
+ # @return [String] The status as written in the API description, e.g. "200", "4XX" or "default"
25
+ # @!attribute [r] content_type
26
+ # @return [String, nil] Content type of this response
27
+ # @!attribute [r] content_schema
28
+ # @return [#value, nil] The schema of the response body. Its `value` is the JSON Schema Hash.
29
+ # @!attribute [r] headers
30
+ # @return [Array<ResponseHeader>, nil] The response headers defined in the API description
31
+ # @!attribute [r] key
32
+ # @return [String] A unique key of this response definition
25
33
  attr_reader :status, :content_type, :content_schema, :headers, :key
26
34
 
35
+ # Validates a response against this response definition.
36
+ # @param response [Rack::Response]
37
+ # @return [ValidatedResponse]
27
38
  def validate(response)
28
39
  parsed_values, error = @parser.parse(response)
29
40
  error ||= @validator.call(parsed_values) if parsed_values
30
41
  ValidatedResponse.new(response, parsed_values:, error:, response_definition: self)
31
42
  end
32
43
  end
44
+
45
+ # @deprecated Use {ResponseDefinition}
46
+ Response = ResponseDefinition
47
+ deprecate_constant :Response
33
48
  end
@@ -4,9 +4,11 @@ require_relative 'parameters_parser'
4
4
  require_relative 'response_body_parsers'
5
5
 
6
6
  module OpenapiFirst
7
+ # @api private
7
8
  ParsedResponse = Data.define(:body, :headers)
8
9
 
9
10
  # Parse a response
11
+ # @api private
10
12
  class ResponseParser
11
13
  def initialize(headers:, content_type:)
12
14
  @headers_parser = build_headers_parser(headers)
@@ -34,7 +36,7 @@ module OpenapiFirst
34
36
  end
35
37
  rack_response.body
36
38
  rescue TypeError
37
- raise Error, "Cannot not read response body. Response is not string-like, but is a #{rack_response.body.class}."
39
+ raise Error, "Cannot read response body. Response is not string-like, but is a #{rack_response.body.class}."
38
40
  end
39
41
 
40
42
  def build_headers_parser(headers)
@@ -5,6 +5,7 @@ require_relative 'validators/response_body'
5
5
 
6
6
  module OpenapiFirst
7
7
  # Entry point for response validators
8
+ # @api private
8
9
  class ResponseValidator
9
10
  VALIDATORS = [
10
11
  Validators::ResponseHeaders,
@@ -2,7 +2,7 @@
2
2
 
3
3
  module OpenapiFirst
4
4
  class Router
5
- # @visibility private
5
+ # @api private
6
6
  module FindContent
7
7
  def self.call(contents, content_type)
8
8
  return contents[nil] if content_type.nil? || content_type.empty?
@@ -4,7 +4,7 @@ require_relative 'find_content'
4
4
 
5
5
  module OpenapiFirst
6
6
  class Router
7
- # @visibility private
7
+ # @api private
8
8
  module FindResponse
9
9
  Match = Data.define(:response, :error)
10
10
 
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'rack/utils'
3
4
  require_relative '../path_template'
4
5
  require_relative 'segment_pattern'
5
6
 
6
7
  module OpenapiFirst
7
8
  class Router
8
- # @visibility private
9
+ # @api private
9
10
  class PathTrie
10
11
  def initialize
11
12
  @static = {}
@@ -23,7 +24,10 @@ module OpenapiFirst
23
24
  def match(path)
24
25
  values = []
25
26
  value, names = find(path.split('/', -1), 0, values)
26
- [value, names.zip(values).to_h] if value
27
+ return unless value
28
+
29
+ values.map! { decode(_1) } if path.include?('%')
30
+ [value, names.zip(values).to_h]
27
31
  end
28
32
 
29
33
  def find(segments, index, values)
@@ -47,6 +51,11 @@ module OpenapiFirst
47
51
 
48
52
  private
49
53
 
54
+ def decode(value)
55
+ decoded = Rack::Utils.unescape_path(value).force_encoding(Encoding::UTF_8)
56
+ decoded.valid_encoding? ? decoded : value
57
+ end
58
+
50
59
  def find_partial(segments, index, values)
51
60
  @partial.each_value do |pattern, node|
52
61
  captures = pattern.match(segments[index])
@@ -5,7 +5,7 @@ require_relative '../path_template'
5
5
  module OpenapiFirst
6
6
  class Router
7
7
  # Matches one path segment against a segment of a path template that mixes literals and variables
8
- # @visibility private
8
+ # @api private
9
9
  class SegmentPattern
10
10
  def initialize(segment)
11
11
  @pattern = build_pattern(segment)
@@ -7,6 +7,7 @@ require_relative 'router/find_response'
7
7
 
8
8
  module OpenapiFirst
9
9
  # Router can map requests / responses to their API definition
10
+ # @api private
10
11
  class Router
11
12
  # Returned by {#match}
12
13
  RequestMatch = Data.define(:request_definition, :params, :error, :responses) do
@@ -5,6 +5,7 @@ require_relative 'validation_error'
5
5
  module OpenapiFirst
6
6
  module Schema
7
7
  # A hash of Schemas
8
+ # @api private
8
9
  class Hash
9
10
  # @param schemas Hash of schemas
10
11
  # @param required Array of required keys
@@ -19,7 +20,7 @@ module OpenapiFirst
19
20
  def validate(root_value)
20
21
  validation = @root_schema.validate(root_value)
21
22
  validations = @schemas.reduce(validation) do |enum, (key, schema)|
22
- root_value[key] = schema.value['default'] if schema.value.key?('default') && !root_value.key?(key)
23
+ insert_default(root_value, key, schema) unless root_value.key?(key)
23
24
  next enum unless root_value.key?(key)
24
25
 
25
26
  value = root_value[key]
@@ -33,6 +34,19 @@ module OpenapiFirst
33
34
  end
34
35
  ValidationResult.new(validations)
35
36
  end
37
+
38
+ private
39
+
40
+ def insert_default(root_value, key, schema)
41
+ default = default_keyword(schema.parsed)
42
+ root_value[key] = default.value if default
43
+ end
44
+
45
+ def default_keyword(parsed)
46
+ parsed.fetch('default') do
47
+ parsed.each_value.filter_map { default_keyword(_1.ref_schema.parsed) if _1.respond_to?(:ref_schema) }.first
48
+ end
49
+ end
36
50
  end
37
51
  end
38
52
  end
@@ -5,6 +5,7 @@ require_relative 'validation_error'
5
5
  module OpenapiFirst
6
6
  module Schema
7
7
  # Result of validating data against a schema. Return value of Schema#validate.
8
+ # @api private
8
9
  class ValidationResult
9
10
  def initialize(validation)
10
11
  @validation = validation
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # :nocov:
3
+ # simplecov:disable
4
4
  begin
5
5
  require 'sinatra/base'
6
6
  rescue LoadError
7
7
  raise LoadError, 'openapi_first/sinatra needs the `sinatra` gem. Add `gem "sinatra"` to your Gemfile.'
8
8
  end
9
- # :nocov:
9
+ # simplecov:disable
10
10
 
11
11
  require 'did_you_mean'
12
12
  require 'openapi_first'
@@ -4,11 +4,14 @@ require_relative 'observe'
4
4
 
5
5
  module OpenapiFirst
6
6
  module Test
7
+ # @api private
7
8
  REQUEST = 'openapi.test.request'
9
+ # @api private
8
10
  RESPONSE = 'openapi.test.response'
9
11
 
10
12
  # A wrapper of the original app
11
13
  # with silent request/response validation to track requests/responses.
14
+ # @api private
12
15
  class App < SimpleDelegator
13
16
  def initialize(app, api:, validate_request_before_handling:)
14
17
  super(app)
@@ -4,6 +4,7 @@ module OpenapiFirst
4
4
  module Test
5
5
  # Return a Module with a call method that wrapps silent request/response validation to monitor a Rack app
6
6
  # This is used by Openapi::Test.observe
7
+ # @api private
7
8
  module Callable
8
9
  # Returns a Module with a `call(env)` method that wraps super inside silent request/response validation
9
10
  # You can use `Application.prepend(OpenapiFirst::Test::Callable[oad])` to monitor your app during testing.
@@ -58,9 +58,9 @@ module OpenapiFirst
58
58
  # Ignore certain errors for certain requests
59
59
  # @param block A Proc that will be called with [OpenapiFirst::ValidatedRequest]
60
60
  def ignore_request_error(&block)
61
- # :nocov:
61
+ # simplecov:disable
62
62
  raise ArgumentError, 'You have to pass a block' unless block_given?
63
- # :nocov:
63
+ # simplecov:disable
64
64
 
65
65
  @ignore_request_error = block
66
66
  end
@@ -68,9 +68,9 @@ module OpenapiFirst
68
68
  # Ignore certain errors for certain responses
69
69
  # @param block A Proc that will be called with [OpenapiFirst::ValidatedResponse, Rack::Request]
70
70
  def ignore_response_error(&block)
71
- # :nocov:
71
+ # simplecov:disable
72
72
  raise ArgumentError, 'You have to pass a block' unless block_given?
73
- # :nocov:
73
+ # simplecov:disable
74
74
 
75
75
  @ignore_response_error = block
76
76
  end
@@ -82,6 +82,8 @@ module OpenapiFirst
82
82
  @skip_response_coverage = block
83
83
  end
84
84
 
85
+ # @param block A Proc that will be called with the path template and the request method of each route.
86
+ # Return true to exclude the route and its responses from coverage.
85
87
  def skip_coverage(&block)
86
88
  return @skip_coverage unless block_given?
87
89
 
@@ -3,6 +3,7 @@
3
3
  module OpenapiFirst
4
4
  module Test
5
5
  module Coverage
6
+ # @api private
6
7
  CoveredRequest = Data.define(:key, :error) do
7
8
  def valid? = error.nil?
8
9
  end
@@ -3,6 +3,7 @@
3
3
  module OpenapiFirst
4
4
  module Test
5
5
  module Coverage
6
+ # @api private
6
7
  CoveredResponse = Data.define(:key, :error) do
7
8
  def valid? = error.nil?
8
9
  end
@@ -7,6 +7,7 @@ module OpenapiFirst
7
7
  module Coverage
8
8
  class HtmlReporter
9
9
  # Provides the binding and helper methods for the ERB template.
10
+ # @api private
10
11
  class Context
11
12
  NO_REQUESTS_WARNING =
12
13
  'API Coverage did not detect any API requests for the registered ' \
@@ -16,6 +16,8 @@ module OpenapiFirst
16
16
  @logger = logger
17
17
  end
18
18
 
19
+ # Writes the HTML report to the output file
20
+ # @param coverage_result [Coverage::Result]
19
21
  def report(coverage_result)
20
22
  html = TEMPLATE.result(Context.new(coverage_result, @verbose).get_binding)
21
23
  FileUtils.mkdir_p(File.dirname(@output))