openapi_parameters 0.3.2 → 0.3.4

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: ce377792c0096d9aba455f83d129162c636f6cab951e43221de7dfbfb9e67227
4
- data.tar.gz: e8f320696d3af924bd25496c6f98e19222af638365bfd39d5199b0da27104002
3
+ metadata.gz: fa336f02dcfc58d656659606710c5fed70335b7a5e101f42228a24913e12325b
4
+ data.tar.gz: e81f400174546397953abe62d23c26f07fbf14f594ca2297ad129118847a4709
5
5
  SHA512:
6
- metadata.gz: 67bf514be29731af8936dbab3ec1dfcb61b1091332487f4602c56bab7a708dd9637925b8e58c819d9d39d75e4bf861800dfaf407608837f846f2ed3b09f6cf2d
7
- data.tar.gz: cf4f96c5a4e0f4c0b025eaa116dd3e38d040f6e1ef2be0cde8b3a2b1f871d5b027454ed1d57ca69ccb3310ae84162488c4111797985cf9ec3a0b321c2f30d717
6
+ metadata.gz: b30a7b111b652c9dbc12e0c19ef5f9dc74a5ba9b6eebc0a7392ba0d5070d7c19578017a64e8948e8dd235b57e8784202e37765c62be015c5520940278def939f
7
+ data.tar.gz: 1e91216f5bff01be794c5eec6a469cea42162472568c2d881afa3267aa036257d0afab0e97534edd57f074217122c96abac08e3e6d4a14c139348bb0ae81fd8e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.4] - 2024-06-14
4
+
5
+ - Fix handling invalid object query parameters (String vs Hash)
6
+
7
+ ## [0.3.3] - 2023-04-13
8
+
9
+ - Remove zeitwerk. It's awesome, but not needed here
10
+
3
11
  ## [0.3.2] - 2023-11-14
4
12
 
5
13
  - Assume that schemas with `properties` or `style: deepObject` describe Objects and therefore convert it's values.
data/README.md CHANGED
@@ -13,22 +13,20 @@ Note that OpenAPI supportes parameter definition on path and operation objects.
13
13
  ### Unpack query/path/header/cookie parameters from HTTP requests according to their OpenAPI definition
14
14
 
15
15
  ```ruby
16
- parameters = [{
16
+
17
+ query_parameters = OpenapiParameters::Query.new([{
17
18
  'name' => 'ids',
18
19
  'required' => true,
19
- 'in' => 'query', # or 'path', 'header', 'cookie'
20
+ 'in' => 'query', # Note that we only pass query parameters here
20
21
  'schema' => {
21
22
  'type' => 'array',
22
23
  'items' => {
23
24
  'type' => 'integer'
24
25
  }
25
26
  }
26
- }]
27
-
28
- query_parameters = OpenapiParameters::Query.new(parameters)
27
+ }])
29
28
  query_string = env['QUERY_STRING'] # => 'ids=1&ids=2'
30
29
  query_parameters.unpack(query_string) # => { 'ids' => [1, 2] }
31
- query_parameters.unpack(query_string, convert: false) # => { 'ids' => ['1', '2'] }
32
30
 
33
31
  path_parameters = OpenapiParameters::Path.new(parameters)
34
32
  route_params = env['route.params'] # This depends on the webframework you are using
@@ -1,12 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiParameters
4
- ##
5
4
  # Tries to convert a request parameter value (string) to the type specified in the JSON Schema.
6
5
  module Converter
7
6
  class << self
8
7
  ##
9
- # @param input [String, Hash, Array] the value to convert
8
+ # @param value [String, Hash, Array] the value to convert
10
9
  # @param schema [Hash] the schema to use for conversion.
11
10
  def convert(value, schema) # rubocop:disable Metrics
12
11
  return if value.nil?
@@ -44,9 +43,11 @@ module OpenapiParameters
44
43
  end
45
44
  end
46
45
 
47
- def convert_object(object, schema)
48
- object.each_with_object({}) do |(key, value), hsh|
49
- hsh[key] = convert(value, schema['properties']&.fetch(key, nil))
46
+ def convert_object(value, schema)
47
+ return value unless value.is_a?(Hash)
48
+
49
+ value.each_with_object({}) do |(key, val), hsh|
50
+ hsh[key] = convert(val, schema['properties']&.fetch(key, nil))
50
51
  end
51
52
  end
52
53
 
@@ -1,14 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rack'
4
-
5
3
  module OpenapiParameters
6
4
  # Cookie parses OpenAPI cookie parameters from a cookie string.
7
5
  class Cookie
8
6
  # @param parameters [Array<Hash>] The OpenAPI parameter definitions.
9
7
  # @param convert [Boolean] Whether to convert the values to the correct type.
10
8
  def initialize(parameters, convert: true)
11
- @parameters = parameters
9
+ @parameters = parameters.map { Parameter.new(_1) }
12
10
  @convert = convert
13
11
  end
14
12
 
@@ -16,7 +14,6 @@ module OpenapiParameters
16
14
  def unpack(cookie_string)
17
15
  cookies = Rack::Utils.parse_cookies_header(cookie_string)
18
16
  parameters.each_with_object({}) do |parameter, result|
19
- parameter = Parameter.new(parameter)
20
17
  next unless cookies.key?(parameter.name)
21
18
 
22
19
  result[parameter.name] = catch :skip do
@@ -6,14 +6,13 @@ module OpenapiParameters
6
6
  # @param parameters [Array<Hash>] The OpenAPI parameters
7
7
  # @param convert [Boolean] Whether to convert the values to the correct type.
8
8
  def initialize(parameters, convert: true)
9
- @parameters = parameters
9
+ @parameters = parameters.map { Parameter.new(_1) }
10
10
  @convert = convert
11
11
  end
12
12
 
13
13
  # @param headers [Hash] The headers from the request. Use HeadersHash to convert a Rack env to a Hash.
14
14
  def unpack(headers)
15
15
  parameters.each_with_object({}) do |parameter, result|
16
- parameter = Parameter.new(parameter)
17
16
  next unless headers.key?(parameter.name)
18
17
 
19
18
  result[parameter.name] = catch :skip do
@@ -6,17 +6,18 @@ module OpenapiParameters
6
6
  class Parameter
7
7
  # @param definition [Hash] The parameter definition. A string keyed Hash.
8
8
  def initialize(definition)
9
- check_supported!(definition)
10
9
  @definition = definition
10
+ @name = definition['name']
11
+ @is_deep_object = style == 'deepObject'
12
+ check_supported!
11
13
  end
12
14
 
13
- attr_reader :definition
15
+ attr_reader :definition, :name
14
16
 
15
- def name
16
- definition['name']
17
+ def deep_object?
18
+ @is_deep_object
17
19
  end
18
20
 
19
- ##
20
21
  # @return [String] The location of the parameter in the request, "path", "query", "header" or "cookie".
21
22
  def location
22
23
  definition['in']
@@ -90,8 +91,8 @@ module OpenapiParameters
90
91
  REF = '$ref'
91
92
  private_constant :REF
92
93
 
93
- def check_supported!(definition)
94
- return unless definition.values.any? { |v| v.is_a?(Hash) && v.key?(REF) }
94
+ def check_supported!
95
+ return unless schema&.key?(REF)
95
96
 
96
97
  raise NotSupportedError,
97
98
  "Parameter schema with $ref is not supported: #{definition.inspect}"
@@ -8,7 +8,7 @@ module OpenapiParameters
8
8
  # @param parameters [Array<Hash>] The OpenAPI path parameters.
9
9
  # @param convert [Boolean] Whether to convert the values to the correct type.
10
10
  def initialize(parameters, convert: true)
11
- @parameters = parameters
11
+ @parameters = parameters.map { Parameter.new(_1) }
12
12
  @convert = convert
13
13
  end
14
14
 
@@ -16,8 +16,7 @@ module OpenapiParameters
16
16
 
17
17
  # @param path_params [Hash] The path parameters from the Rack request. The keys are strings.
18
18
  def unpack(path_params)
19
- parameters.each_with_object({}) do |param, result|
20
- parameter = Parameter.new(param)
19
+ parameters.each_with_object({}) do |parameter, result|
21
20
  next unless path_params.key?(parameter.name)
22
21
 
23
22
  result[parameter.name] = catch :skip do
@@ -8,15 +8,14 @@ module OpenapiParameters
8
8
  # @param parameters [Array<Hash>] The OpenAPI query parameter definitions.
9
9
  # @param convert [Boolean] Whether to convert the values to the correct type.
10
10
  def initialize(parameters, convert: true)
11
- @parameters = parameters
11
+ @parameters = parameters.map { Parameter.new(_1) }
12
12
  @convert = convert
13
13
  end
14
14
 
15
15
  def unpack(query_string) # rubocop:disable Metrics/AbcSize
16
16
  parsed_query = Rack::Utils.parse_query(query_string)
17
17
  parameters.each_with_object({}) do |parameter, result|
18
- parameter = Parameter.new(parameter)
19
- if parameter.style == 'deepObject'
18
+ if parameter.deep_object?
20
19
  parsed_nested_query = Rack::Utils.parse_nested_query(query_string)
21
20
  next unless parsed_nested_query.key?(parameter.name)
22
21
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiParameters
4
- ## Unpacks a paramexter value from a string as defined in the OpenAPI specification.
4
+ # Unpacks a parameter value from a string as defined in the OpenAPI specification.
5
5
  # @visibility private
6
6
  module Unpacker
7
7
  class << self
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiParameters
4
- VERSION = '0.3.2'
4
+ VERSION = '0.3.4'
5
5
  end
@@ -1,9 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'zeitwerk'
3
+ require 'rack'
4
+ require_relative 'openapi_parameters/converter'
5
+ require_relative 'openapi_parameters/cookie'
6
+ require_relative 'openapi_parameters/error'
7
+ require_relative 'openapi_parameters/header'
8
+ require_relative 'openapi_parameters/headers_hash'
9
+ require_relative 'openapi_parameters/not_supported_error'
10
+ require_relative 'openapi_parameters/parameter'
11
+ require_relative 'openapi_parameters/path'
12
+ require_relative 'openapi_parameters/query'
13
+ require_relative 'openapi_parameters/unpacker'
4
14
 
5
15
  # OpenapiParameters is a gem that parses OpenAPI parameters from Rack
6
16
  module OpenapiParameters
7
- LOADER = Zeitwerk::Loader.for_gem
8
- LOADER.setup
9
17
  end
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.3.2
4
+ version: 0.3.4
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-11-14 00:00:00.000000000 Z
11
+ date: 2024-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.2'
27
- - !ruby/object:Gem::Dependency
28
- name: zeitwerk
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '2.6'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '2.6'
41
27
  description: This parses HTTP query/path/header/cookie parameters exactly as described
42
28
  in an OpenAPI API description.
43
29
  email:
@@ -46,15 +32,8 @@ executables: []
46
32
  extensions: []
47
33
  extra_rdoc_files: []
48
34
  files:
49
- - ".rspec"
50
- - ".rubocop.yml"
51
- - ".tool-versions"
52
35
  - CHANGELOG.md
53
- - Gemfile
54
- - Gemfile.lock
55
- - LICENSE
56
36
  - README.md
57
- - Rakefile
58
37
  - lib/openapi_parameters.rb
59
38
  - lib/openapi_parameters/converter.rb
60
39
  - lib/openapi_parameters/cookie.rb
@@ -67,8 +46,6 @@ files:
67
46
  - lib/openapi_parameters/query.rb
68
47
  - lib/openapi_parameters/unpacker.rb
69
48
  - lib/openapi_parameters/version.rb
70
- - openapi_parameters.gemspec
71
- - sig/openapi_parameters.rbs
72
49
  homepage: https://github.com/ahx/openapi_parameters
73
50
  licenses:
74
51
  - MIT
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/.rubocop.yml DELETED
@@ -1,19 +0,0 @@
1
- require:
2
- - rubocop-rspec
3
-
4
- Naming/MethodParameterName:
5
- Enabled: false
6
-
7
- AllCops:
8
- NewCops: enable
9
- SuggestExtensions: false
10
- TargetRubyVersion: 3.1
11
-
12
- RSpec/ExampleLength:
13
- Enabled: false
14
-
15
- RSpec/NestedGroups:
16
- Max: 4
17
-
18
- Metrics/MethodLength:
19
- Max: 20
data/.tool-versions DELETED
@@ -1 +0,0 @@
1
- ruby 3.2.1
data/Gemfile DELETED
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
4
-
5
- # Specify your gem's dependencies in OpenapiParameters.gemspec
6
- gemspec
7
-
8
- gem 'rack-test'
9
- gem 'rake', '~> 13.0'
10
- gem 'rspec', '~> 3.0'
11
- gem 'rspec-benchmark'
12
- gem 'rubocop'
13
- gem 'rubocop-rspec'
data/Gemfile.lock DELETED
@@ -1,89 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- openapi_parameters (0.3.2)
5
- rack (>= 2.2)
6
- zeitwerk (~> 2.6)
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- ast (2.4.2)
12
- benchmark-malloc (0.2.0)
13
- benchmark-perf (0.6.0)
14
- benchmark-trend (0.4.0)
15
- diff-lcs (1.5.0)
16
- json (2.6.3)
17
- language_server-protocol (3.17.0.3)
18
- parallel (1.23.0)
19
- parser (3.2.2.4)
20
- ast (~> 2.4.1)
21
- racc
22
- racc (1.7.3)
23
- rack (3.0.8)
24
- rack-test (2.1.0)
25
- rack (>= 1.3)
26
- rainbow (3.1.1)
27
- rake (13.1.0)
28
- regexp_parser (2.8.2)
29
- rexml (3.2.6)
30
- rspec (3.12.0)
31
- rspec-core (~> 3.12.0)
32
- rspec-expectations (~> 3.12.0)
33
- rspec-mocks (~> 3.12.0)
34
- rspec-benchmark (0.6.0)
35
- benchmark-malloc (~> 0.2)
36
- benchmark-perf (~> 0.6)
37
- benchmark-trend (~> 0.4)
38
- rspec (>= 3.0)
39
- rspec-core (3.12.2)
40
- rspec-support (~> 3.12.0)
41
- rspec-expectations (3.12.3)
42
- diff-lcs (>= 1.2.0, < 2.0)
43
- rspec-support (~> 3.12.0)
44
- rspec-mocks (3.12.6)
45
- diff-lcs (>= 1.2.0, < 2.0)
46
- rspec-support (~> 3.12.0)
47
- rspec-support (3.12.1)
48
- rubocop (1.57.2)
49
- json (~> 2.3)
50
- language_server-protocol (>= 3.17.0)
51
- parallel (~> 1.10)
52
- parser (>= 3.2.2.4)
53
- rainbow (>= 2.2.2, < 4.0)
54
- regexp_parser (>= 1.8, < 3.0)
55
- rexml (>= 3.2.5, < 4.0)
56
- rubocop-ast (>= 1.28.1, < 2.0)
57
- ruby-progressbar (~> 1.7)
58
- unicode-display_width (>= 2.4.0, < 3.0)
59
- rubocop-ast (1.30.0)
60
- parser (>= 3.2.1.0)
61
- rubocop-capybara (2.19.0)
62
- rubocop (~> 1.41)
63
- rubocop-factory_bot (2.24.0)
64
- rubocop (~> 1.33)
65
- rubocop-rspec (2.25.0)
66
- rubocop (~> 1.40)
67
- rubocop-capybara (~> 2.17)
68
- rubocop-factory_bot (~> 2.22)
69
- ruby-progressbar (1.13.0)
70
- unicode-display_width (2.5.0)
71
- zeitwerk (2.6.12)
72
-
73
- PLATFORMS
74
- arm64-darwin-21
75
- arm64-darwin-22
76
- x86_64-darwin-20
77
- x86_64-linux
78
-
79
- DEPENDENCIES
80
- openapi_parameters!
81
- rack-test
82
- rake (~> 13.0)
83
- rspec (~> 3.0)
84
- rspec-benchmark
85
- rubocop
86
- rubocop-rspec
87
-
88
- BUNDLED WITH
89
- 2.3.10
data/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 Andreas Haller
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/gem_tasks'
4
- require 'rspec/core/rake_task'
5
- require 'rubocop/rake_task'
6
-
7
- RSpec::Core::RakeTask.new(:spec)
8
- RuboCop::RakeTask.new
9
-
10
- task default: %i[spec rubocop]
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'lib/openapi_parameters/version'
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = 'openapi_parameters'
7
- spec.version = OpenapiParameters::VERSION
8
- spec.authors = ['Andreas Haller']
9
- spec.email = ['andreas.haller@posteo.de']
10
-
11
- spec.summary = 'openapi_parameters is an OpenAPI aware parameter parser'
12
- spec.description =
13
- 'This parses HTTP query/path/header/cookie parameters exactly as described in an OpenAPI API description.'
14
- spec.homepage = 'https://github.com/ahx/openapi_parameters'
15
- spec.required_ruby_version = '>= 3.1.0'
16
- spec.licenses = ['MIT']
17
-
18
- spec.metadata['homepage_uri'] = spec.homepage
19
- spec.metadata['source_code_uri'] = 'https://github.com/ahx/openapi_parameters'
20
- spec.metadata[
21
- 'changelog_uri'
22
- ] = 'https://github.com/ahx/openapi_parameters/blob/main/CHANGELOG.md'
23
-
24
- # Specify which files should be added to the gem when it is released.
25
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
- spec.files =
27
- Dir.chdir(__dir__) do
28
- `git ls-files -z`.split("\x0")
29
- .reject do |f|
30
- (f == __FILE__) ||
31
- f.match(
32
- %r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)}
33
- )
34
- end
35
- end
36
- spec.bindir = 'exe'
37
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
38
- spec.require_paths = ['lib']
39
-
40
- spec.add_dependency 'rack', '>= 2.2'
41
- spec.add_dependency 'zeitwerk', '~> 2.6'
42
-
43
- # For more information and examples about making a new gem, check out our
44
- # guide at: https://bundler.io/guides/creating_gem.html
45
- spec.metadata['rubygems_mfa_required'] = 'true'
46
- end
@@ -1,4 +0,0 @@
1
- module OpenapiParameters
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end