openapi_parameters 0.1.0 → 0.2.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: 97605d112ff65a874f5fa63dc7ecd13d3ca37468e559dbf7980a1f6c55e37274
4
- data.tar.gz: 280fe6bc55862118dc65e77a6e29c6a3f27ee3c3d07f1361003c9cbd52fdc660
3
+ metadata.gz: ef334fe7ee42e62490cabedbb2d5172c710299e736db3154450d23db23dc4247
4
+ data.tar.gz: faa2d9c99a2a4f59ebe38b2ff797bcaae165277cc606075168e8cf3191a3722e
5
5
  SHA512:
6
- metadata.gz: e1cff00125ffb1ea81548a6e8f5bee73b1cedc29ccb2569296e0f869a9ac3412cc9c683afcba2f48bcc6cca06287a1fba251068b4c831e2d4933497268a58da8
7
- data.tar.gz: 6177d8425f16f678af64f4059ecc1fecf70e954ce0edd95ce2e38a7b5bbd1efe801951fffb9ca8932d7d4d258100af0070623c5c04672d1e539caee1cbc8204e
6
+ metadata.gz: 6bfe90c227761100d238dec3c51d7f680ce20d39a834b149cdf1426163685e99209b0a4f5b131ff58a9362419d50f5fec966b86747d176609de524d94e17421d
7
+ data.tar.gz: ec587259898ef7c0004b97a0ab849fb97624de1dd464159dcbd5eeff549e2877465195d94b67cba3fbc5a2f6f245fb0a5863198048a9ba6c04a09324bb1b420e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.1.0] - 2032-03-25
3
+ ## [0.2.0] - 2023-03-30
4
+
5
+ - Breaking: Path parameters are unpacked from a hash, which is usually available from the used Rack web framework. This is much simpler and more performant.
6
+
7
+ ## [0.1.0] - 2023-03-25
4
8
 
5
9
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,9 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- openapi_parameters (0.1.0)
4
+ openapi_parameters (0.2.0)
5
5
  rack (>= 2.2)
6
- uri_template (>= 0.7, < 2.0)
7
6
  zeitwerk (~> 2.6)
8
7
 
9
8
  GEM
@@ -11,45 +10,47 @@ GEM
11
10
  specs:
12
11
  ast (2.4.2)
13
12
  diff-lcs (1.5.0)
14
- json (2.6.2)
13
+ json (2.6.3)
15
14
  parallel (1.22.1)
16
- parser (3.1.2.1)
15
+ parser (3.2.1.1)
17
16
  ast (~> 2.4.1)
18
- rack (3.0.0)
17
+ rack (3.0.7)
19
18
  rainbow (3.1.1)
20
19
  rake (13.0.6)
21
- regexp_parser (2.6.1)
20
+ regexp_parser (2.7.0)
22
21
  rexml (3.2.5)
23
22
  rspec (3.12.0)
24
23
  rspec-core (~> 3.12.0)
25
24
  rspec-expectations (~> 3.12.0)
26
25
  rspec-mocks (~> 3.12.0)
27
- rspec-core (3.12.0)
26
+ rspec-core (3.12.1)
28
27
  rspec-support (~> 3.12.0)
29
- rspec-expectations (3.12.0)
28
+ rspec-expectations (3.12.2)
30
29
  diff-lcs (>= 1.2.0, < 2.0)
31
30
  rspec-support (~> 3.12.0)
32
- rspec-mocks (3.12.0)
31
+ rspec-mocks (3.12.5)
33
32
  diff-lcs (>= 1.2.0, < 2.0)
34
33
  rspec-support (~> 3.12.0)
35
34
  rspec-support (3.12.0)
36
- rubocop (1.39.0)
35
+ rubocop (1.48.1)
37
36
  json (~> 2.3)
38
37
  parallel (~> 1.10)
39
- parser (>= 3.1.2.1)
38
+ parser (>= 3.2.0.0)
40
39
  rainbow (>= 2.2.2, < 4.0)
41
40
  regexp_parser (>= 1.8, < 3.0)
42
41
  rexml (>= 3.2.5, < 4.0)
43
- rubocop-ast (>= 1.23.0, < 2.0)
42
+ rubocop-ast (>= 1.26.0, < 2.0)
44
43
  ruby-progressbar (~> 1.7)
45
- unicode-display_width (>= 1.4.0, < 3.0)
46
- rubocop-ast (1.23.0)
47
- parser (>= 3.1.1.0)
48
- rubocop-rspec (2.11.1)
49
- rubocop (~> 1.19)
50
- ruby-progressbar (1.11.0)
51
- unicode-display_width (2.3.0)
52
- uri_template (0.7.0)
44
+ unicode-display_width (>= 2.4.0, < 3.0)
45
+ rubocop-ast (1.28.0)
46
+ parser (>= 3.2.1.0)
47
+ rubocop-capybara (2.17.1)
48
+ rubocop (~> 1.41)
49
+ rubocop-rspec (2.19.0)
50
+ rubocop (~> 1.33)
51
+ rubocop-capybara (~> 2.17)
52
+ ruby-progressbar (1.13.0)
53
+ unicode-display_width (2.4.2)
53
54
  zeitwerk (2.6.7)
54
55
 
55
56
  PLATFORMS
data/README.md CHANGED
@@ -28,9 +28,8 @@ query_string = env['QUERY_STRING'] # => 'ids=1&ids=2'
28
28
  query_parameters.unpack(query_string) # => { 'ids' => [1, 2] }
29
29
  query_parameters.unpack(query_string, convert: false) # => { 'ids' => ['1', '2'] }
30
30
 
31
- path_parameters = OpenapiParameters::Path.new(parameters, '/pets/ids')
32
- path_info = env['PATH_INFO'] # => '/pets/1,2,3'
33
- path_parameters.unpack(path_info) # => { 'ids' => [1, 2, 3] }
31
+ path_parameters = OpenapiParameters::Path.new(parameters)
32
+ path_parameters.unpack(route_params) # => { 'ids' => [1, 2, 3] }
34
33
 
35
34
  header_parameters = OpenapiParameters::Header.new(parameters)
36
35
  header_parameters.unpack_env(env)
@@ -22,9 +22,7 @@ module OpenapiParameters
22
22
 
23
23
  private
24
24
 
25
- require 'json'
26
-
27
- def convert(value, schema)
25
+ def convert(value, schema) # rubocop:disable Metrics/*
28
26
  check_supported!(schema)
29
27
  return if value.nil?
30
28
  return value if schema.nil?
@@ -57,7 +55,7 @@ module OpenapiParameters
57
55
  end
58
56
  end
59
57
 
60
- REF = '$ref'.freeze
58
+ REF = '$ref'
61
59
  private_constant :REF
62
60
 
63
61
  def check_supported!(schema)
@@ -86,11 +86,10 @@ module OpenapiParameters
86
86
  }.freeze
87
87
  private_constant :DEFAULT_STYLE
88
88
 
89
-
90
89
  VALID_LOCATIONS = Set.new(%w[query header path cookie]).freeze
91
90
  private_constant :VALID_LOCATIONS
92
91
 
93
- REF = '$ref'.freeze
92
+ REF = '$ref'
94
93
  private_constant :REF
95
94
 
96
95
  def check_supported!(definition)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'uri_template'
3
+ require 'rack'
4
4
 
5
5
  module OpenapiParameters
6
6
  # Parses OpenAPI path parameters from path template strings and the request path.
@@ -8,65 +8,67 @@ module OpenapiParameters
8
8
  # @param parameters [Array<Hash>] The OpenAPI path parameters.
9
9
  # @param path [String] The OpenAPI path template string.
10
10
  # @param convert [Boolean] Whether to convert the values to the correct type.
11
- def initialize(parameters, path, convert: true)
11
+ def initialize(parameters, convert: true)
12
12
  @parameters = parameters
13
- @path = path
14
13
  @convert = convert
15
14
  end
16
15
 
17
16
  attr_reader :parameters, :path
18
17
 
19
- def unpack(path_info)
20
- parsed_path = URITemplate.new(url_template).extract(path_info) || {}
21
- parameters.each_with_object(parsed_path) do |param, result|
18
+ def unpack(path_params)
19
+ parameters.each_with_object({}) do |param, result|
22
20
  parameter = Parameter.new(param)
23
- next unless parsed_path.key?(parameter.name)
21
+ next unless path_params.key?(parameter.name)
24
22
 
25
23
  result[parameter.name] = catch :skip do
26
- value = unpack_parameter(parameter, result)
24
+ value = unpack_parameter(parameter, path_params)
27
25
  @convert ? Converter.call(value, parameter.schema) : value
28
26
  end
29
27
  end
30
28
  end
31
29
 
30
+ private
31
+
32
32
  def unpack_parameter(parameter, parsed_path)
33
33
  value = parsed_path[parameter.name]
34
- if parameter.object? && value.is_a?(Array)
35
- throw :skip, value if value.length.odd?
36
- return Hash[*value]
37
- end
38
- value
34
+ return value if parameter.primitive? || value.nil?
35
+ return unpack_array(parameter, value) if parameter.array?
36
+ return unpack_object(parameter, value) if parameter.object?
39
37
  end
40
38
 
41
- def url_template
42
- @url_template ||=
43
- begin
44
- path = @path.dup
45
- parameters.each do |p|
46
- param = Parameter.new(p)
47
- next unless param.array? || param.object?
48
-
49
- path.gsub!(
50
- "{#{param.name}}",
51
- "{#{operator(param)}#{param.name}#{modifier(param)}}"
52
- )
53
- end
54
- path
55
- end
56
- end
39
+ def unpack_array(parameter, value)
40
+ return value if value.empty?
41
+ return unpack_matrix(parameter, value) if parameter.style == 'matrix'
57
42
 
58
- private
43
+ value = value[1..] if PREFIXED.key?(parameter.style)
44
+ value.split(ARRAY_DELIMITER[parameter.style])
45
+ end
59
46
 
60
- LIST_OPS = { 'simple' => nil, 'label' => '.', 'matrix' => ';' }.freeze
61
- private_constant :LIST_OPS
47
+ def unpack_matrix(parameter, value)
48
+ result = Rack::Utils.parse_query(value, ';')[parameter.name]
49
+ return result if parameter.explode?
62
50
 
63
- def operator(param)
64
- LIST_OPS[param.style]
51
+ result.split(',')
65
52
  end
66
53
 
67
- def modifier(param)
68
- return '*' if param.explode?
69
- return if param.style == 'matrix' && !param.explode?
54
+ def unpack_object(parameter, value)
55
+ return Rack::Utils.parse_query(value, ',') if parameter.explode?
56
+
57
+ array = unpack_array(parameter, value)
58
+ throw :skip, value if array.length.odd?
59
+
60
+ Hash[*array]
70
61
  end
62
+
63
+ PREFIXED = {
64
+ 'label' => '.',
65
+ 'matrix' => ';'
66
+ }.freeze
67
+
68
+ ARRAY_DELIMITER = {
69
+ 'label' => '.',
70
+ 'simple' => ','
71
+ }.freeze
72
+ private_constant :ARRAY_DELIMITER
71
73
  end
72
74
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiParameters
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -38,7 +38,6 @@ Gem::Specification.new do |spec|
38
38
  spec.require_paths = ['lib']
39
39
 
40
40
  spec.add_dependency 'rack', '>= 2.2'
41
- spec.add_dependency 'uri_template', '>= 0.7', '< 2.0'
42
41
  spec.add_dependency 'zeitwerk', '~> 2.6'
43
42
 
44
43
  # For more information and examples about making a new gem, check out our
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi_parameters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Haller
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-25 00:00:00.000000000 Z
11
+ date: 2023-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -24,26 +24,6 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.2'
27
- - !ruby/object:Gem::Dependency
28
- name: uri_template
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0.7'
34
- - - "<"
35
- - !ruby/object:Gem::Version
36
- version: '2.0'
37
- type: :runtime
38
- prerelease: false
39
- version_requirements: !ruby/object:Gem::Requirement
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- version: '0.7'
44
- - - "<"
45
- - !ruby/object:Gem::Version
46
- version: '2.0'
47
27
  - !ruby/object:Gem::Dependency
48
28
  name: zeitwerk
49
29
  requirement: !ruby/object:Gem::Requirement