openapi_first 2.8.0 → 2.9.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d5a5311b4ca774b9a0cb80e85f29243f46e1c5c762241b015090834971373a7b
4
- data.tar.gz: 640be1c8c02b8a86b5c9d562b7ac837b73881473c54cf9ea731779352e0cee2d
3
+ metadata.gz: c4ae98a82d67480a49874eaec264d200b53685f1fd7d28e09e4e3faf2fc86075
4
+ data.tar.gz: e8d1166f2876dc7f5e909fe9290a03f0f8c530bc6469822a3c88583fa0a29047
5
5
  SHA512:
6
- metadata.gz: 42ab4483c694d921c68fcb30099ecd779e789c5bfc9163f97a70327f5ef1aacdc7b7abae66c70341e2edd4ff5821d4fe6e9ab3e0d65b081963685272d094e5e2
7
- data.tar.gz: 7bbc02b4e061a012663b115758b1f43a6fd4642687d07711d90e7032a800fedb453e3b32a762658bd5185d9cea0f8859b2436d28f4bea12f0bebd4bb613aeeff
6
+ metadata.gz: dd927a3a8a9167e69b1fd2f795b333b232581cc1fb2ee805105f89d82125d58eddd951660fb06d300ac41555ef7ce6cfc2b680feda2d809c8fe64eebc8537b1b
7
+ data.tar.gz: 456f6f1ccffc43d20b3ab51cdd4d392ac4dacf00e403bb92f8829f88e97c63fd48ac1ba316d5dc89662e1d32509f2ee6a92c60b7f327b261d428125bf8e2ec99
data/CHANGELOG.md CHANGED
@@ -2,6 +2,19 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 2.9.0
6
+
7
+ - OpenapiFirst::Test now raises an error for unknown requests. You can deactivate with:
8
+
9
+ ```ruby
10
+ OpenapiFirst::Test.setup do |test|
11
+ # ...
12
+ test.ignore_unknown_request = true
13
+ end
14
+ ```
15
+
16
+ - NotFoundError#message now includes the requested path
17
+
5
18
  ## 2.8.0
6
19
 
7
20
  ### OpenapiFirst::Test is now stricter and more configurable
data/README.md CHANGED
@@ -77,6 +77,14 @@ Here is how to set it up:
77
77
 
78
78
  (✷1): It does not matter what method of openapi_first you use to validate requests/responses. Instead of using `OpenapiFirstTest.app` to wrap your application, you could also use the [middlewares](#rack-middlewares) or [test assertion method](#test-assertions), but you would have to do that for all requests/responses defined in your API description to make coverage work.
79
79
 
80
+ OpenapiFirst' request validation raises an error when a request is not defined. You can deactivate this during testing:
81
+
82
+ ```ruby
83
+ OpenapiFirst::Test.setup do |test|
84
+ test.ignore_unknown_requests = true
85
+ end
86
+ ```
87
+
80
88
  ## Rack Middlewares
81
89
 
82
90
  ### Request validation
@@ -5,7 +5,7 @@ module OpenapiFirst
5
5
  # This returned in ValidatedRequest#error and ValidatedResponse#error.
6
6
  class Failure
7
7
  TYPES = {
8
- not_found: [NotFoundError, 'Request path is not defined.'],
8
+ not_found: [NotFoundError, 'Not found.'],
9
9
  method_not_allowed: [RequestInvalidError, 'Request method is not defined.'],
10
10
  unsupported_media_type: [RequestInvalidError, 'Request content type is not defined.'],
11
11
  invalid_body: [RequestInvalidError, 'Request body invalid:'],
@@ -54,7 +54,10 @@ module OpenapiFirst
54
54
  # Return all request objects that match the given path and request method
55
55
  def match(request_method, path, content_type: nil)
56
56
  path_item, params = find_path_item(path)
57
- return NOT_FOUND unless path_item
57
+ unless path_item
58
+ message = "Request path #{path} is not defined in API description."
59
+ return NOT_FOUND.with(error: Failure.new(:not_found, message:))
60
+ end
58
61
 
59
62
  contents = path_item.dig(request_method, :requests)
60
63
  return NOT_FOUND.with(error: Failure.new(:method_not_allowed)) unless contents
@@ -12,6 +12,7 @@ module OpenapiFirst
12
12
  @response_raise_error = true
13
13
  @ignored_unknown_status = [404]
14
14
  @report_coverage = true
15
+ @ignore_unknown_requests = false
15
16
  @registry = {}
16
17
  @apps = {}
17
18
  end
@@ -28,7 +29,8 @@ module OpenapiFirst
28
29
  @apps[api] = app
29
30
  end
30
31
 
31
- attr_accessor :coverage_formatter_options, :coverage_formatter, :response_raise_error, :minimum_coverage
32
+ attr_accessor :coverage_formatter_options, :coverage_formatter, :response_raise_error, :minimum_coverage,
33
+ :ignore_unknown_requests
32
34
  attr_reader :registry, :apps, :report_coverage, :ignored_unknown_status
33
35
 
34
36
  # Configure report coverage
@@ -100,6 +100,10 @@ module OpenapiFirst
100
100
 
101
101
  OpenapiFirst.configure do |config|
102
102
  @after_request_validation = config.after_request_validation do |validated_request, oad|
103
+ raise validated_request.error.exception if raise_request_error?(validated_request)
104
+
105
+ configuration.ignore_unknown_requests && validated_request.known?
106
+
103
107
  Coverage.track_request(validated_request, oad)
104
108
  end
105
109
 
@@ -114,6 +118,13 @@ module OpenapiFirst
114
118
  @installed = true
115
119
  end
116
120
 
121
+ def self.raise_request_error?(validated_request)
122
+ return false if validated_request.valid?
123
+ return true if validated_request.known?
124
+
125
+ !configuration.ignore_unknown_requests
126
+ end
127
+
117
128
  def self.raise_response_error?(validated_response)
118
129
  configuration.response_raise_error && !configuration.ignored_unknown_status.include?(validated_response.status)
119
130
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiFirst
4
- VERSION = '2.8.0'
4
+ VERSION = '2.9.0'
5
5
  end
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: 2.8.0
4
+ version: 2.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Haller