openapi_first 4.4.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 +6 -0
  4. data/README.md +79 -10
  5. data/lib/openapi_first/builder.rb +25 -24
  6. data/lib/openapi_first/child_configuration.rb +1 -0
  7. data/lib/openapi_first/configuration.rb +80 -2
  8. data/lib/openapi_first/definition.rb +3 -2
  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 +2 -0
  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 +1 -1
  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 +7 -2
  74. metadata +4 -3
@@ -16,6 +16,9 @@ module OpenapiFirst
16
16
  autoload :TerminalReporter, 'openapi_first/test/coverage/terminal_reporter'
17
17
  autoload :HtmlReporter, 'openapi_first/test/coverage/html_reporter'
18
18
 
19
+ # The result of a coverage run, which is passed to the coverage reporter
20
+ # @!attribute [r] coverage
21
+ # @return [Float] The coverage in percent
19
22
  Result = Data.define(:plans, :coverage) do
20
23
  def skipped_requests_count
21
24
  plans.sum(&:skipped_requests_count)
@@ -27,6 +30,7 @@ module OpenapiFirst
27
30
  end
28
31
 
29
32
  class << self
33
+ # @api private
30
34
  def start(skip_response: nil, skip_route: nil)
31
35
  return if @drb_uri
32
36
 
@@ -48,6 +52,7 @@ module OpenapiFirst
48
52
  @drb_uri = nil
49
53
  end
50
54
 
55
+ # @api private
51
56
  def track_request(request, oad)
52
57
  return unless request.known?
53
58
 
@@ -55,9 +60,9 @@ module OpenapiFirst
55
60
  # the coverage collection.
56
61
  # To make this work we need to keep arguments trivial, which is the reason the request
57
62
  # is wrapped in a CoveredRequest data object.
58
- # :nocov:
63
+ # simplecov:disable
59
64
  return unless tracker
60
- # :nocov:
65
+ # simplecov:disable
61
66
 
62
67
  tracker.track_request(
63
68
  oad.key,
@@ -68,6 +73,7 @@ module OpenapiFirst
68
73
  )
69
74
  end
70
75
 
76
+ # @api private
71
77
  def track_response(response, _request, oad)
72
78
  return unless response.known?
73
79
 
@@ -75,9 +81,9 @@ module OpenapiFirst
75
81
  # the coverage collection.
76
82
  # To make this work we need to keep arguments trivial, which is the reason the response
77
83
  # is wrapped in a CoveredResponse data object.
78
- # :nocov:
84
+ # simplecov:disable
79
85
  return unless tracker
80
- # :nocov:
86
+ # simplecov:disable
81
87
 
82
88
  tracker.track_response(
83
89
  oad.key,
@@ -88,6 +94,7 @@ module OpenapiFirst
88
94
  )
89
95
  end
90
96
 
97
+ # @return [Result] The coverage of all registered API descriptions
91
98
  def result
92
99
  Result.new(plans:, coverage:)
93
100
  end
@@ -7,11 +7,24 @@ module OpenapiFirst
7
7
  module Test
8
8
  # Methods to use in integration tests
9
9
  module Methods
10
+ # @api private
10
11
  def self.included(base)
11
12
  base.include(DefaultApiMethod)
12
13
  base.include(AssertionMethod)
13
14
  end
14
15
 
16
+ # Returns a module to include into a test class. It adds `assert_api_conform` and, if you
17
+ # pass an app, an `app` method for rack-test that validates silently and tracks coverage.
18
+ # @example
19
+ # RSpec.configure do |config|
20
+ # config.include OpenapiFirst::Test::Methods[MyApp, api: :v1], type: :request
21
+ # end
22
+ # @param application_under_test [#call, nil] The Rack app to define `app` for
23
+ # @param api [Symbol, nil] The registered API description to use. Defaults to the `:api` metadata
24
+ # of the RSpec example group, else :default.
25
+ # @param validate_request_before_handling [Boolean] Validate the request before the app handles it
26
+ # instead of after
27
+ # @return [Module]
15
28
  def self.[](application_under_test = nil, api: nil, validate_request_before_handling: false)
16
29
  mod = Module.new do
17
30
  def self.included(base)
@@ -53,7 +66,7 @@ module OpenapiFirst
53
66
  end
54
67
  end
55
68
 
56
- # @visibility private
69
+ # @api private
57
70
  module AssertionMethod
58
71
  def self.included(base)
59
72
  if Test.minitest?(base)
@@ -4,7 +4,10 @@ module OpenapiFirst
4
4
  module Test
5
5
  # Assertion methods for Minitest
6
6
  module MinitestHelpers
7
- # :nocov:
7
+ # simplecov:disable
8
+ # Asserts that the last request and response of rack-test conform to the API description.
9
+ # @param status [Integer, nil] The expected response status
10
+ # @param api [Symbol, Definition] The registered API description
8
11
  def assert_api_conform(status: nil, api: openapi_first_default_api)
9
12
  api = OpenapiFirst::Test[api]
10
13
  request = respond_to?(:last_request) ? last_request : @request
@@ -22,7 +25,7 @@ module OpenapiFirst
22
25
  assert validated_request.valid?, validated_request.error&.exception_message
23
26
  assert validated_response.valid?, validated_response.error&.exception_message
24
27
  end
25
- # :nocov:
28
+ # simplecov:disable
26
29
  end
27
30
  end
28
31
  end
@@ -6,10 +6,11 @@ module OpenapiFirst
6
6
  module Test
7
7
  class ObserveError < Error; end
8
8
 
9
- # @visibility private
9
+ # @api private
10
10
  module Observed; end
11
11
 
12
12
  # Inject silent request/response validation to observe rack apps during testing
13
+ # @api private
13
14
  module Observe
14
15
  def self.observe(app, api: :default)
15
16
  definition = OpenapiFirst::Test[api]
@@ -3,6 +3,7 @@
3
3
  module OpenapiFirst
4
4
  module Test
5
5
  # Middleware that observes requests and responses. This is used to trigger hooks added by OpenapiFirst::Tests.
6
+ # @api private
6
7
  class ObserverMiddleware
7
8
  def initialize(app, options = {})
8
9
  @app = app
@@ -5,12 +5,15 @@ module OpenapiFirst
5
5
  # Assertion methods to use when no known test framework was found
6
6
  # These methods just raise an exception if an error was found
7
7
  module PlainHelpers
8
+ # Validates the last request and response of rack-test against the API description.
9
+ # @param status [Integer, nil] The expected response status
10
+ # @param api [Symbol, Definition] The registered API description
8
11
  def assert_api_conform(status: nil, api: openapi_first_default_api)
9
12
  api = OpenapiFirst::Test[api]
10
- # :nocov:
13
+ # simplecov:disable
11
14
  request = respond_to?(:last_request) ? last_request : @request
12
15
  response = respond_to?(:last_response) ? last_response : @response
13
- # :nocov:
16
+ # simplecov:disable
14
17
 
15
18
  if status && status != response.status
16
19
  raise OpenapiFirst::Error,
@@ -19,12 +22,12 @@ module OpenapiFirst
19
22
  end
20
23
 
21
24
  validated = api.validate_request(request, raise_error: false)
22
- # :nocov:
25
+ # simplecov:disable
23
26
  raise validated.error.exception if validated.invalid?
24
27
 
25
28
  validated = api.validate_response(request, response, raise_error: false)
26
29
  raise validated.error.exception if validated.invalid?
27
- # :nocov:
30
+ # simplecov:disable
28
31
  end
29
32
  end
30
33
  end
@@ -70,39 +70,58 @@ module OpenapiFirst
70
70
  autoload :App, 'openapi_first/test/app'
71
71
  extend Registry
72
72
 
73
+ # Raised when the coverage of the API description is too low
73
74
  class CoverageError < Error; end
75
+ # Raised in tests when a request has a query parameter that the API description does not declare
74
76
  class UnknownQueryParameterError < Error; end
75
77
 
76
- # Inject request/response validation in a rack app class
78
+ # Injects silent request/response validation into a Rack app class or instance, so that its
79
+ # requests and responses are validated and tracked for coverage during tests.
80
+ # @example
81
+ # OpenapiFirst::Test.observe(Rails.application)
82
+ # @param app [#call, Class] A Rack app instance or a class with a `call` instance method
83
+ # @param api [Symbol, Definition] The name of a registered API description, or a Definition
84
+ # @return [void]
77
85
  def self.observe(app, api: :default)
78
86
  Observe.observe(app, api:)
79
87
  end
80
88
 
89
+ # @api private
81
90
  def self.minitest?(base)
82
91
  base.include?(::Minitest::Assertions)
83
92
  rescue NameError
84
93
  false
85
94
  end
86
95
 
96
+ # The API descriptions registered via {.register}, or the ones registered via
97
+ # {OpenapiFirst.register} if none were registered here.
98
+ # @return [Hash<Symbol, Definition>]
87
99
  def self.definitions
88
100
  super.empty? ? OpenapiFirst.definitions : super
89
101
  end
90
102
 
103
+ # @return [::Logger]
91
104
  def self.logger
92
105
  configuration.logger
93
106
  end
94
107
 
108
+ # @return [Test::Configuration]
95
109
  def self.configuration
96
110
  @configuration ||= Configuration.new
97
111
  end
98
112
 
113
+ # @param oad [Definition]
114
+ # @return [Boolean] true if oad is one of the {.definitions}
99
115
  def self.registered?(oad)
100
116
  key = oad.key
101
117
  definitions.any? { |(_name, registered)| registered.key == key }
102
118
  end
103
119
 
104
120
  # Sets up OpenAPI test coverage and OAD registration.
121
+ # Reports coverage when the test process exits, and exits with status 2 if the coverage is below
122
+ # {Configuration#minimum_coverage}.
105
123
  # @yield [OpenapiFirst::Test::Configuration] configuration A configuration to setup test integration
124
+ # @return [void]
106
125
  def self.setup
107
126
  install
108
127
  yield configuration if block_given?
@@ -123,13 +142,14 @@ module OpenapiFirst
123
142
 
124
143
  main_process = Process.pid
125
144
  @setup ||= at_exit do
126
- # :nocov:
145
+ # simplecov:disable
127
146
  # Only handle exit once in the main process
128
147
  @exit_handler&.call if Process.pid == main_process
129
- # :nocov:
148
+ # simplecov:disable
130
149
  end
131
150
  end
132
151
 
152
+ # @api private
133
153
  def self.handle_exit
134
154
  return unless configuration.report_coverage
135
155
 
@@ -163,11 +183,17 @@ module OpenapiFirst
163
183
  # Returns the Rack app wrapped with silent request, response validation
164
184
  # You can use this if you want to track coverage via Test::Coverage, but don't want to use
165
185
  # the middlewares or manual request, response validation.
186
+ # @param app [#call] The Rack app
187
+ # @param spec [Definition, nil] The API description. Overrides api.
188
+ # @param api [Symbol] The name of a registered API description
189
+ # @param validate_request_before_handling [Boolean] Validate the request before the app handles it instead of after
190
+ # @return [#call] The wrapped Rack app
166
191
  def self.app(app, spec: nil, api: :default, validate_request_before_handling: false)
167
192
  spec ||= self[api]
168
193
  App.new(app, api: spec, validate_request_before_handling:)
169
194
  end
170
195
 
196
+ # @api private
171
197
  def self.install
172
198
  return if @installed
173
199
 
@@ -194,6 +220,7 @@ module OpenapiFirst
194
220
  @installed = true
195
221
  end
196
222
 
223
+ # @api private
197
224
  def self.uninstall
198
225
  configuration = OpenapiFirst.configuration
199
226
  configuration.after_request_validation.delete(@after_request_validation)
@@ -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 Utils
6
6
  module_function
7
7
 
@@ -21,7 +21,7 @@ module OpenapiFirst
21
21
  attr_reader :error
22
22
 
23
23
  # The request definition if this request is defined in the API description
24
- # @return [Request, nil]
24
+ # @return [RequestDefinition, nil]
25
25
  attr_reader :request_definition
26
26
 
27
27
  # @!method operation_id
@@ -46,16 +46,16 @@ module OpenapiFirst
46
46
  # @return [Hash<String, anything>]
47
47
  def parsed_query = @parsed_request&.query || {}
48
48
 
49
- # Parsed headers. This only returns the query parameters that are defined in the OpenAPI spec.
49
+ # Parsed headers. This only returns the headers that are defined in the OpenAPI spec.
50
50
  # @return [Hash<String, anything>]
51
51
  def parsed_headers = @parsed_request&.headers || {}
52
52
 
53
- # Parsed cookies. This only returns the query parameters that are defined in the OpenAPI spec.
53
+ # Parsed cookies. This only returns the cookies that are defined in the OpenAPI spec.
54
54
  # @return [Hash<String, anything>]
55
55
  def parsed_cookies = @parsed_request&.cookies || {}
56
56
 
57
57
  # Parsed body. This parses the body according to the content type.
58
- # Note that this returns the hole body, not only the fields that are defined in the OpenAPI spec.
58
+ # Note that this returns the whole body, not only the fields that are defined in the OpenAPI spec.
59
59
  # You can use JSON Schemas `additionalProperties` or `unevaluatedProperties` to
60
60
  # return a validation error if the body contains unknown fields.
61
61
  # @return [Hash<String, anything>, anything]
@@ -20,16 +20,18 @@ module OpenapiFirst
20
20
  attr_reader :error
21
21
 
22
22
  # The response definition if this response is defined in the API description
23
- # @return [Response, nil]
23
+ # @return [ResponseDefinition, nil]
24
24
  attr_reader :response_definition
25
25
 
26
- # The parsed headers
26
+ # The parsed headers. This only returns the headers that are defined in the OpenAPI spec.
27
+ # Returns nil if the response is not defined or could not be parsed.
27
28
  # @return [Hash<String,anything>, nil]
28
29
  def parsed_headers
29
30
  @parsed_values&.headers
30
31
  end
31
32
 
32
- # The parsed body
33
+ # The parsed body.
34
+ # Returns nil if the response is not defined or could not be parsed.
33
35
  # @return [Hash<String,anything>, nil]
34
36
  def parsed_body
35
37
  @parsed_values&.body
@@ -4,6 +4,7 @@ require_relative '../schema/validation_result'
4
4
 
5
5
  module OpenapiFirst
6
6
  module Validators
7
+ # @api private
7
8
  class MultipartRequestBody
8
9
  FILE_UPLOAD_PLACEHOLDER = String.new('', encoding: Encoding::BINARY).freeze
9
10
 
@@ -4,6 +4,7 @@ require_relative 'multipart_request_body'
4
4
 
5
5
  module OpenapiFirst
6
6
  module Validators
7
+ # @api private
7
8
  class RequestBody
8
9
  MULTIPART = %r{\Amultipart/}i
9
10
  private_constant :MULTIPART
@@ -2,7 +2,7 @@
2
2
 
3
3
  module OpenapiFirst
4
4
  module Validators
5
- # @visibility private
5
+ # @api private
6
6
  module RequestParameters
7
7
  Headers = Data.define(:schema) do
8
8
  def call(parsed_request)
@@ -4,6 +4,7 @@ require_relative '../schema/validation_result'
4
4
 
5
5
  module OpenapiFirst
6
6
  module Validators
7
+ # @api private
7
8
  class ResponseBody
8
9
  def initialize(schema)
9
10
  @schema = schema
@@ -2,6 +2,7 @@
2
2
 
3
3
  module OpenapiFirst
4
4
  module Validators
5
+ # @api private
5
6
  class ResponseHeaders
6
7
  def initialize(headers)
7
8
  @headers = headers
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiFirst
4
- VERSION = '4.4.0'
4
+ VERSION = '4.5.0'
5
5
  end
data/lib/openapi_first.rb CHANGED
@@ -19,9 +19,10 @@ module OpenapiFirst
19
19
 
20
20
  autoload :Test, 'openapi_first/test'
21
21
 
22
- # Key in rack to find instance of Request
22
+ # Key in the Rack env where the request validation middleware stores the {ValidatedRequest}
23
23
  REQUEST = 'openapi.request'
24
24
 
25
+ # @api private
25
26
  FAILURE = :openapi_first_validation_failure
26
27
 
27
28
  # @return [Configuration]
@@ -78,7 +79,11 @@ module OpenapiFirst
78
79
  end
79
80
 
80
81
  filepath = filepath_or_definition
81
- raise FileNotFoundError, "File not found: #{filepath}" unless File.exist?(filepath)
82
+ unless File.exist?(filepath)
83
+ raise FileNotFoundError,
84
+ "File not found: #{File.expand_path(filepath).inspect}." /
85
+ 'Pass a filepath or the Symbol name of an API description registered via OpenapiFirst.register.'
86
+ end
82
87
 
83
88
  contents = FileLoader.load(filepath)
84
89
  parse(contents, only:, filepath:, path_prefix:, &)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi_first
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.4.0
4
+ version: 4.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Haller
@@ -83,6 +83,7 @@ executables: []
83
83
  extensions: []
84
84
  extra_rdoc_files: []
85
85
  files:
86
+ - ".yardopts"
86
87
  - CHANGELOG.md
87
88
  - LICENSE.txt
88
89
  - README.md
@@ -114,12 +115,12 @@ files:
114
115
  - lib/openapi_first/query_string_parser.rb
115
116
  - lib/openapi_first/ref_resolver.rb
116
117
  - lib/openapi_first/registry.rb
117
- - lib/openapi_first/request.rb
118
118
  - lib/openapi_first/request_body_parsers.rb
119
+ - lib/openapi_first/request_definition.rb
119
120
  - lib/openapi_first/request_headers.rb
120
121
  - lib/openapi_first/request_validator.rb
121
- - lib/openapi_first/response.rb
122
122
  - lib/openapi_first/response_body_parsers.rb
123
+ - lib/openapi_first/response_definition.rb
123
124
  - lib/openapi_first/response_header.rb
124
125
  - lib/openapi_first/response_parser.rb
125
126
  - lib/openapi_first/response_validator.rb