openapi_parser 0.11.2 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd57f66bf94d3232c5f87e3d5ecf4acd3129dd7a3912970fa43dd9d2c32328c5
4
- data.tar.gz: 42cd168a8f8d218ee00fa9fb47fd5ec890c88cbd1d8b52c17dd0396889fa58e1
3
+ metadata.gz: 77010f8eb5e5b8dd1dadd5e365fb13cf11619215f19c38bcd8d6b46bb3927c80
4
+ data.tar.gz: f2b4a9f19eddc1fba4eefb6d74e6c5f0cd285712aa70b7b3970c5b6121701beb
5
5
  SHA512:
6
- metadata.gz: 6a975df0a46e822671a4c7ebdabf1d706947d11f08fe2843bbb75efb43e18f3596f2d9efea912807fde3d972b74202c76c23e25f5accfcd0bb84542a85a5a45d
7
- data.tar.gz: c158d3bd6cbd9eb4615d13f3f3813a0e4f79b12f5afc5c00acd0c98d7e0979bb14117cd5ef8ad7e4a3d599a0fbb1616c0852f77d40abe41a35ab8ed1ae9394d9
6
+ metadata.gz: 90d397850737878d78fd697ab6a60ec004b47496bad48a246226cce54c1835c7c8ee1725c02f09fe581827c76a16bd8af62d1b0310d8cefaca67b3ff480ab863
7
+ data.tar.gz: 4d227f8b4497cb297fcf73515914c15ecc05060f3e30d275bf5e493124e4b6bd23b47e5f6f95a80a519ba74d7da5f51612820fe52867d1da58c4a92cfc7283ff
data/.gitignore CHANGED
@@ -7,6 +7,9 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
 
10
+ # RubyMine
11
+ .idea
12
+
10
13
  # rspec failure tracking
11
14
  .rspec_status
12
- Gemfile.lock
15
+ Gemfile.lock
@@ -1,5 +1,9 @@
1
1
  ## Unreleased
2
2
 
3
+ * Find path by extracted params than path length #84
4
+ * Unescape ref URI before lookup in OpenAPIParser::Findable #85
5
+ * Improved path parameter matching code to allow file extensions, multiple parameters inside one path element, etc #90
6
+
3
7
  ## 0.11.2 (2020-05-23)
4
8
  * Allow date and time content in YAML #81
5
9
 
@@ -4,6 +4,7 @@ module OpenAPIParser::Findable
4
4
  # @param [String] reference
5
5
  # @return [OpenAPIParser::Findable]
6
6
  def find_object(reference)
7
+ reference = URI.unescape(reference)
7
8
  return self if object_reference == reference
8
9
  remote_reference = !reference.start_with?('#')
9
10
  return find_remote_object(reference) if remote_reference
@@ -38,7 +38,41 @@ class OpenAPIParser::PathItemFinder
38
38
  end
39
39
  end
40
40
 
41
+ def parse_path_parameters(schema_path, request_path)
42
+ parameters = path_parameters(schema_path)
43
+ return nil if parameters.empty?
44
+
45
+ # If there are regex special characters in the path, the regex will
46
+ # be too permissive, so escape the non-parameter parts.
47
+ components = []
48
+ unprocessed = schema_path.dup
49
+ parameters.each do |parameter|
50
+ parts = unprocessed.partition(parameter)
51
+ components << Regexp.escape(parts[0]) unless parts[0] == ''
52
+ components << "(?<#{param_name(parameter)}>.+)"
53
+ unprocessed = parts[2]
54
+ end
55
+ components << Regexp.escape(unprocessed) unless unprocessed == ''
56
+
57
+ regex = components.join('')
58
+ matches = request_path.match(regex)
59
+ return nil unless matches
60
+
61
+ # Match up the captured names with the captured values as a hash
62
+ matches.names.zip(matches.captures).to_h
63
+ end
64
+
41
65
  private
66
+ def path_parameters(schema_path)
67
+ # OAS3 follows a RFC6570 subset for URL templates
68
+ # https://swagger.io/docs/specification/serialization/#uri-templates
69
+ # A URL template param can be preceded optionally by a "." or ";", and can be succeeded optionally by a "*";
70
+ # this regex returns a match of the full parameter name with all of these modifiers. Ex: {;id*}
71
+ parameters = schema_path.scan(/(\{[\.;]*[^\{\*\}]+\**\})/)
72
+ # The `String#scan` method returns an array of arrays; we want an array of strings
73
+ parameters.collect { |param| param[0] }
74
+ end
75
+
42
76
  # check if there is a identical path in the schema (without any param)
43
77
  def matches_directly?(request_path, http_method)
44
78
  @paths.path[request_path]&.operation(http_method)
@@ -70,8 +104,9 @@ class OpenAPIParser::PathItemFinder
70
104
  splitted_request_path.zip(splitted_schema_path).reduce({}) do |result, zip_item|
71
105
  request_path_item, schema_path_item = zip_item
72
106
 
73
- if path_template?(schema_path_item)
74
- result[param_name(schema_path_item)] = request_path_item
107
+ params = parse_path_parameters(schema_path_item, request_path_item)
108
+ if params
109
+ result.merge!(params)
75
110
  else
76
111
  return if schema_path_item != request_path_item
77
112
  end
@@ -80,7 +115,7 @@ class OpenAPIParser::PathItemFinder
80
115
  end
81
116
  end
82
117
 
83
- # find all matching patchs with parameters extracted
118
+ # find all matching paths with parameters extracted
84
119
  # EXAMPLE:
85
120
  # [
86
121
  # ['/user/{id}/edit', { 'id' => 1 }],
@@ -94,7 +129,7 @@ class OpenAPIParser::PathItemFinder
94
129
  splitted_schema_path = path.split('/')
95
130
 
96
131
  next result if different_depth_or_method?(splitted_schema_path, splitted_request_path, path_item, http_method)
97
-
132
+
98
133
  extracted_params = extract_params(splitted_request_path, splitted_schema_path)
99
134
  result << [path, extracted_params] if extracted_params
100
135
  result
@@ -105,12 +140,12 @@ class OpenAPIParser::PathItemFinder
105
140
  # EXAMPLE: find_path_and_params('get', '/user/1') => ['/user/{id}', { 'id' => 1 }]
106
141
  def find_path_and_params(http_method, request_path)
107
142
  return [request_path, {}] if matches_directly?(request_path, http_method)
108
-
143
+
109
144
  matching = matching_paths_with_params(request_path, http_method)
110
145
 
111
146
  # if there are many matching paths, return the one with the smallest number of params
112
147
  # (prefer /user/{id}/action over /user/{param_1}/{param_2} )
113
- matching.min_by { |match| match[0].size }
148
+ matching.min_by { |match| match[1].size }
114
149
  end
115
150
 
116
151
  def parse_request_path(http_method, request_path)
@@ -1,3 +1,3 @@
1
1
  module OpenAPIParser
2
- VERSION = '0.11.2'.freeze
2
+ VERSION = '0.12.0'.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.11.2
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ota42y
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-23 00:00:00.000000000 Z
11
+ date: 2020-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler